Share via


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;
        NewItem.Update();
        FormWeb.AllowUnsafeUpdates = false;
      }
    }
  }
}
private string GetDomValue(string XpathToGet)
{
  return this.CreateNavigator().SelectSingleNode(XpathToGet, this.NamespaceManager).Value;
}

Comments

  • Anonymous
    April 02, 2010
    This is valuable stuff for me! But, unfortunately, there is no sound! )-:

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

  • Anonymous
    June 07, 2010
    Great stuff! Can we download this video from MSDN?

  • Anonymous
    June 27, 2010
    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?

  • Anonymous
    June 28, 2010
    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

  • Anonymous
    July 10, 2010
    I hear also no sound and the channel9 video

  • Anonymous
    August 05, 2010
    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."

  • Anonymous
    November 22, 2010
    hi, how to move the selected listboxcontrol item into the another listbox control using infopath programing.. Any Idea.' Regards mank

  • Anonymous
    November 29, 2010
    The comment has been removed

  • Anonymous
    December 28, 2010
    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.

  • Anonymous
    June 08, 2011
    Wow..no wonder Microsoft products are so buggy...they do not even test a simple video!!!!

  • Anonymous
    November 15, 2011
    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!

  • Anonymous
    July 26, 2012
    2012, still no audio... facepalm :-)

  • Anonymous
    May 01, 2013
    Hi Can you translate this code to VBA for Infopath 2010 ? thancks

  • Anonymous
    June 18, 2013
    where can we find an equivalent VB.Net code ?

  • Anonymous
    June 18, 2013
    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. Scott

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