View Single Post
  #1 (permalink)  
Old May 7th, 2011, 06:27 PM
cooltechie cooltechie is offline
Authorized User
Points: 134, Level: 2
Points: 134, Level: 2 Points: 134, Level: 2 Points: 134, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default SharePoint as secondary data source

Hi,

I am using Infopath and what I am trying to do is : I have a repeating table that should get its data from a secondary data source. In this case, the secondary data source is a SharePoint List.

But, we cannot retrieve multiple items from a secondary data source and copy them into a repeating table in Main data Source.

So I am trying to achieve this through C# code. I am getting an "Invalid Operation Exception" . Can anyone help, where I am doing wrong?

Code:
public void InternalStartup()
        {
            ((ButtonEvent)EventManager.ControlEvents["get_Items"]).Clicked += new ClickedEventHandler(get_Items_Clicked);
        }

        public void get_Items_Clicked(object sender, ClickedEventArgs e)
        {
            // Write your code here.
            XPathNavigator secDSNav = DataSources["mydatasource"].CreateNavigator();
            //Retrieve the rows of the secondary data sources.

      XPathNodeIterator rows = secDSNav.Select("/dfs:myFields/dfs:dataFields/dfs:Items",NamespaceManager);

      int counter = 1;

      while (rows.MoveNext())
      {
          string title = GetValueOrEmptyString(rows.Current.SelectSingleNode("@Field1", NamespaceManager));

          string status = GetValueOrEmptyString(rows.Current.SelectSingleNode("@Field2", NamespaceManager));

          DateTime myDate = GetValueOrEmptyString(rows.Current.SelectSingleNode("@Field3", NamespaceManager));

          // Increment the counter

          counter++;

          // Add the item to the repeating table

          AddItem(title, status, myDate);

      }


      // Remove the first empty item from the repeating table

      DeleteFirstEmptyItem();
        }



        public static string GetValueOrEmptyString(XPathNavigator node)
        {
            return node == null ? string.Empty : node.Value;
        }







        private void AddItem(string title, string status, DateTime mydate)
        {

            XmlDocument doc = new XmlDocument();
            XmlNode group = doc.CreateElement("p:Item", "http://www.xxxx.com/myform/2009");
            XmlNode field = doc.CreateElement("p:Field1", "http://www.xxxx.com/myform/2009"); 
            XmlNode node = group.AppendChild(field);
            node.InnerText = title;

            field = doc.CreateElement("p:Field2", "http://www.xxxx.com/myform/2009");
            node = group.AppendChild(field);
            node.InnerText = status;

            field = doc.CreateElement("p:Field3", "http://www.xxxx.com/myform/2009");
            node = group.AppendChild(field);
            node.InnerText = mydate.ToString();
            doc.AppendChild(group);

           MainDataSource.CreateNavigator().SelectSingleNode("/p:myrootElement/p:Items", NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

        }



        private void DeleteFirstEmptyItem()

        {

        XPathNavigator domNav = MainDataSource.CreateNavigator();

        XPathNavigator itemNav = domNav.SelectSingleNode("/p:myrootElement/p:Items/p:Item[1]", NamespaceManager);

        if (itemNav != null)

       itemNav.DeleteSelf();
            
    }







    
    
    
    }




}
Thanks in advance,
Reply With Quote