Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Add items to a SharePoint list using managed code

Add items to a SharePoint list using managed code

Rate This
  • Comments 16

In this video demo, Phil Newman shows how you can write managed code in an InfoPath form to add items to a SharePoint list, and then publish this form as a sandboxed solution to SharePoint.

In InfoPath 2010, forms with code can now be published directly to SharePoint without requiring the farm administrator to approve and upload them! These forms run in a sandboxed environment which protects other resources on the SharePoint server from malicious code.

Get Microsoft Silverlight

For more details on publishing InfoPath forms with code as sandboxed solutions to SharePoint, see Phil’s earlier post Introduction to Sandboxed Solutions - Sort data in repeating tables using managed code.

Here’s the code that was used in this demo:

NOTE: To develop code using the SharePoint object model, you need to add a reference to Microsoft.SharePoint.dll to your VSTA project. This DLL is installed in %CommonProgramFiles%\Microsoft Shared\Web Server Extensions\14\ISAPI with your licensed copy of Microsoft SharePoint Server.

public void school_Changed(object sender, XmlEventArgs e)
{
  using (SPSite FormSite = new SPSite(ServerInfo.SharePointSiteUrl.ToString()))
  {
    using (SPWeb FormWeb = FormSite.OpenWeb())
    {
      //Get the list and query for the item - "Schools" is the name of the list I am retrieving
      SPList LookupList = FormWeb.Lists["Schools"];
      SPQuery MyQuery = new SPQuery();
      //"Title" is the field where I keep the school name
      ///my:myFields/my:school is the xpath to the school field
      MyQuery.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + GetDomValue("/my:myFields/my:school") + "</Value></Eq></Where>";
      SPListItemCollection ReturnedItems = LookupList.GetItems(MyQuery);
      //Add the item to the lookup list if no items were returned in the query
      if (ReturnedItems.Count == 0)
      {
        SPListItem NewItem = LookupList.Items.Add();
        ///my:myFields/my:school is the xpath to the school field
        NewItem["Title"] = GetDomValue("/my:myFields/my:school");
                        
        //Set AllowUnsafeUpdates = true to update the database
        FormWeb.AllowUnsafeUpdates = true;
        NewItem.Update();
        FormWeb.AllowUnsafeUpdates = false;
      }
    }
  }
}
private string GetDomValue(string XpathToGet)
{
  return this.CreateNavigator().SelectSingleNode(XpathToGet, this.NamespaceManager).Value;
}
Leave a Comment
  • Please add 8 and 3 and type the answer here:
  • Post
  • This is valuable stuff for me! But, unfortunately, there is no sound! )-:

  • The video plays with sound. Have you tried this on another machine? Do other videos play with sound for you?

  • Great stuff! Can we download this video from MSDN?

  • Unfortuantely on my machine there's no sound either.. I think there are some problems with silverlight. I tried other machines (all dell notebooks) and browsers as well..

    Colud you maybe post a download link?

  • We still cannot reproduce this issue. Can you please let us know if the issue also occurs when viewing the video from Channel9 - channel9.msdn.com/.../InfoPath-2010-Add-items-to-a-SharePoint-list-using-form-code

  • I hear also no sound and the channel9 video

  • Unfortunately i am gettuing the following error while accessing the site

    "The Web application at http://lhis072:2435/ 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 mapping to the intended application."

  • hi,

    how to move the selected listboxcontrol item into the another listbox control using infopath programing..

    Any Idea.'

    Regards

    mank

  • Hi Mansoor,

    With InfoPath, *all* data is stored in the main data source nodes. As such, if you want to simply copy what was selected in one list box to another, you simply need to copy the data in the node for the first list box to the node that is bound to the 2nd list box.

    Scott

  • Hey everybody,

    I do not know if you noticed, but the fact there is no sound is not on your end. The person who recorded the video did not realize the sound was mute. I figured it out by watching the presenter desktop (look for the small speaker in the screen and you will notice is muted!) no wonder we cannot hear any sound!

    P.S: Always test your video and sound before publishing. Thanks.

  • Wow..no wonder Microsoft products are so buggy...they do not even test a simple video!!!!

  • Great post, works perfectly. However, I did have a hard time inserting the code. A little bit more clarification would make it easier for people with little to none VS experience.

    Thanks!

  • 2012, still no audio... facepalm :-)

  • Hi

    Can you translate this code to VBA for Infopath 2010 ?

    thancks

  • where can we find an equivalent VB.Net code ?

Page 1 of 2 (16 items) 12