Basics: Interact with SharePoint 2010 Data (post 1 – Web Services & CAML)

Working with the SharePoint platform you will quickly realise that the easiest and probably best and most direct way to interact programmatically with the SharePoint data are the various web services that are shipped with the platform in order to easily leverage what is already available.

For a quick intro to Web Services we will create a Windows Presentation Fountation application that updates a SharePoint list with a new item.

First off you need to create a list. (to create a list, visit a SharePoint site where you have adequate permissions, click on “Site Actions”, then “View All Site Content”, then “Create” and finally, in the SilverLight application that loads up, locate and select “Custom List”)
Lets make a list called “WPF Data”. You will need to keep track of the URL of the site, where you create the list. You need this to pass it as a parameter to the instantiation command of the web service. You may add fields to the list. Rename or play with them however you like. You might, for example, change the “Title” field to “Name”. In case you don’t know how to do that, you will need to access the “List Settings” (it’s on the ribbon, on the far right, 3rd icont from the end).

Now, start a new WPF project in Visual Studio 2010 (name it WPFDataToSharePointList). You will probably be presented with a canvas, onto which you can add controls from the toolbox on the left hand side column (or wherever else you might have placed the toolbox). At the very least, you need to place a textbox onto the canvas and a button (one textbox for each column you created in your custom list in SharePoint). Rename the textbox to something sensible (for example, txtboxName). Change the text that is displayed on the button from “Button” to “Add” and rename to something sensibe (for example, btnAdd).

On the lower part of the screen, you will notice the code that is being built as you add elelements to the canvas. Locate the line that describes your button and in the end, before the closing “>” press the spacebar button and select the “Click” option from the dropdrown menu that appears. Select the “Create new event handler“.

When you reach that point, change view to your code to add a refernce to the Web Service that we will be using to access the List data in SharePoint. Right click on your project name (in the solution explorer) and select “Add Service Reference“. On the window that appears, select “Advanced” on the lower left hand corner. In the following dialog window select “Add Web Reference“. In the next window select “Web Services on the local machine“. That option will scan for all the Web Services that are running on your SharePoint system. Scroll down the list of web services and select “Lists”. Give your reference a name and click on “Add Reference”. For the purposes of this example, I’ll name the reference: ListsWebServiceReference.

As soon as you add the Web Service Reference, you should add a “using” reference to both XML and XML.Linq (just add the lines “using System.Xml;” and “using System.Xml.Linq;” under the list of “using” statements – omit the question marks).

You will need a text variable to store the value from the textbox (string strName=””;). In your event handler, assign the value from the textbox to the variable (strName=txtboxName.Text;)
You will also need a couple of text variables to store your list ID and list view ID. (strng strListID, string strViewID = “”;)

You will then need to instantiate your web service reference (WPFDataToSharePointList.ListsWebServiceReference.Lists ListWebReference = new ListsWebServiceReference.Lists();) pass it a URL (ListWebReference.Url=”the full URL of the site collection where your custom list is stored with /_vti_bin/Lists.asmx in the end“;) and define running credentials (ListWebReference.UseDefaultCredentials= true;)

In order to update the list, you will need to pass a “Batch” XML statement to the system. In order to do that, you will first need to get the ListAndView of the list you want to update.

XmlNode myListXmlNode = ListWebReference.GetListAndView(“WPF Data”, “”);

You can then assign values to the remaining two string variables strListID and strViewID.

strListID = myListXmlNode.ChildNodes[0].Attributes[“Name”].Value;
strViewID = myListXmlNode.ChildNodes[1].Attributes[“Name”].Value;

Notice that both values are taken from the child nodes of myListXmlNode. myListXmlNode is an XmlNode element represented by GetListAndView which is a method of the Lists web service. It gives two child. A string representing the list, a GUID, and another string representing the list’s view.

In order to create the “Batch” Xml that will be responsible for updating the list, you will create an XmlDocument. Then, you will use the CreateElement method of the XmlDocument in order to create the “Batch” Xml.

XmlDocument myListDoc = new XmlDocument();
XmlElement batchXml = myListDoc.CreateElement(“Batch”);

The batchXml will be a CAML statement that will be used in the update method of the XlmDocument to update the list with values from the custom fields:

batchXML.InnerXml = “<Method ID = ‘1’ Cmd=’New’><Field Name=’Title’>” + strName + “</Field> + “</Method>”;

Once the statement is complete (make sure you only add the fields that are relevant to your own implementation and that you pass in the correct variable values) you will pass it as an argument to the UpdateListItems() method of the Lists Web Service. You do so in the following line:

XmlNode myListReturn = ListWebReference.UpdateListItems(“Customers”, batchXML);

Actually, it is not necessary to map the result of the UpdateListItems to an XmlNode. Just by calling the methoc, the list is updated.

Note: It is important to note that the XML argument you build needs to be correctly typed. For example, typing “cmd” instead of “Cmd” will give you  a SOAP error.

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

2 thoughts on “Basics: Interact with SharePoint 2010 Data (post 1 – Web Services & CAML)

  1. […] is the third tutorial about accessing and manipulating data on SharePoint. The first one leveraged web services and the second used the server object model. There’s a client object model as well, but […]

  2. […] you worked though the first attempt to interact with SharePoint data through web services (here) you must have noticed the batchXML.innerXml line: batchXML.InnerXml = “<Method ID = ’1′ […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.