Add items to a SharePoint list using managed code

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;
        FormWeb.AllowUnsafeUpdates = false;

private string GetDomValue(string XpathToGet)
  return this.CreateNavigator().SelectSingleNode(XpathToGet, this.NamespaceManager).Value;
Comments (17)
  1. rich_s says:

    This is valuable stuff for me! But, unfortunately, there is no sound! )-:

  2. infopath says:

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

  3. Jack02459 says:

    Great stuff! Can we download this video from MSDN?

  4. Eric says:

    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?

  5. infopath says:

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

  6. Stefan says:

    I hear also no sound and the channel9 video

  7. Codename "Santosh" says:

    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."

  8. mansoor says:


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

    Any Idea.'



  9. Scott Heim says:

    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.


  10. Carlos says:

    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.

  11. CarlosM says: wonder Microsoft products are so buggy…they do not even test a simple video!!!!

  12. CF says:

    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.


  13. Marco says:

    2012, still no audio… facepalm 🙂

  14. olivier P says:


    Can you translate this code to VBA for Infopath 2010 ?


  15. Santosh Balan says:

    where can we find an equivalent VB.Net code ?

  16. Scott Heim says:

    Hi Santosh,

    You will need to convert this to VB.NET – I don't believe we have the same sample code in VB.NET – just C#.NET.


  17. Salam says:

    The video shows a black screen and nothing happens no activity, I have IE 11, tried with Chrome and Firefox, noway

Comments are closed.

Skip to main content