Pertanyaan infopath2010 code get data from sharepoint list

  • 11 Juli 2012 14:47
     
     

    hello

    when i write code in infopath 2010 to get list data 

    this is code

      SPSite site = new SPSite("http://sptest:100");
                        SPWeb web = site.OpenWeb();

                        SPList list = web.Lists["Countries"];
                        SPListItemCollection listitems = list.Items;

                        XPathNavigator nav = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Countries", this.NamespaceManager);

                        foreach (SPListItem li in listitems)
                        {
                            XPathNavigator newNode = null;
                            newNode = nav.Clone();

                            newNode.SelectSingleNode("/my:myFields/my:Countries/my:Displayname", this.NamespaceManager).SetValue(li["Title"].ToString());
                            newNode.SelectSingleNode("/my:myFields/my:Countries/my:Value", this.NamespaceManager).SetValue(li["Title"].ToString());

                            nav.InsertAfter(newNode);

                            newNode = null;
                        }

                        nav.DeleteSelf();

                        nav = null;

    i get this error

    The Web application at http://sptest:100 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL 


    Nour

Semua Balasan

  • 11 Juli 2012 15:45
     
      Memiliki Kode

    Hi,

    Can you try to use spcontext.current.site object? Also this code, will returns current site url dynamically so you no need to change the form code whenever you deployed the form in different site colletions.

    SPSecurity.RunwithElevatedPrivilage(delegate{
    using(SPSite site = SPContext.Current.Site){
    using(SPWeb web = site.OpenWeb())
    {
                        SPList list = web.Lists["Countries"];
                        SPListItemCollection listitems = list.Items;
                        XPathNavigator nav = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Countries", this.NamespaceManager);
                        foreach (SPListItem li in listitems)
                        {
                            XPathNavigator newNode = null;
                            newNode = nav.Clone();
                            newNode.SelectSingleNode("/my:myFields/my:Countries/my:Displayname", this.NamespaceManager).SetValue(li["Title"].ToString());
                            newNode.SelectSingleNode("/my:myFields/my:Countries/my:Value", this.NamespaceManager).SetValue(li["Title"].ToString());
                            nav.InsertAfter(newNode);
                            newNode = null;
                        }
                        nav.DeleteSelf();
                        nav = null;
    }
    }
    });

    Thanks,


    Senthil



  • 11 Juli 2012 16:24
     
     
    i  try this code but i doesn't run

    Nour