Writing to a SharePoint List using Web Services leveraging Batch Updates

 

It is possible to write to a SharePoint List using SharePoint web Services. This is very useful in scenarios where you might want to integrate 3 party data into your portal (through a scheduled task) and when you do not have access to running scripts on the SharePoint server itself [thus not allowing you to make use of the SharePoint Object model). I am using VS 2008 and .Net 3.5 SP1. The reason I am mentioning the environment is because of the different ways I had to configure my web service reference (as opposed to VS 2005 and .NET 2.0). Also the SharePoint portal I am writing to is configured with windows authentication.

  1. The first step is to add the Lists web service. You can add a reference to the Project in Visual Studio by right clicking Project Properties and saying Add Service Reference. (Called Add Web service in VS 2005 and .net 2.0). The URL for your Lists web service will be usually : http://your server/site/_vti_bin/lists.asmx
  2. Once you have the web service, the next thing you need is the "List ID" of the List that you are going to write to. This ID is a GUID and can be obtained from your portal as follows: Go to the list settings page. Paste the URL in the Browser into notepad and copy everything after "List=" in the string. That's your URL Encoded GUID for the list. To get the LIST ID without the weird (also known as HTML encodedJ) characters, you can use this tool: http://www.albionresearch.com/misc/urlencode.php Copy the ID in the "Encoded" text box and click URLDecode. Copy the ID generated in the Plain text box. This is the List GUID.
  3. By default when you add a web service reference in VS 2008, it DOES not use Ntlm authentication which (most) of the SharePoint servers are configured with. So in order to make it work, you need to configure the "security" element under <binding>. This is what I have after the changes

    <security mode="TransportCredentialOnly">

<transport clientCredentialType="Ntlm" proxyCredentialType="Ntlm" realm=""/>

<message clientCredentialType="UserName" algorithmSuite="Default" />

</security>

  1. Now you are ready to write some C# code. Here is the class I wrote for adding an item with some random "Title" to a List in SharePoint

     

using System;

using System.Security.Principal;

using System.ServiceModel;

using System.Text;

using System.Xml;

using WriteToSharePointList.ListService;

 

namespace WriteToSharePointList

{

class Program
{
static void Main(string[] args)
{
 
try
{
const string listGuid = "{B98831FA-DFC1-4743-8E7E-D952A77D7BD9}";
//Construct teh required batch XML element
XmlElement batch = GetBatchElement();
 
//Set the content of the Batch elemnt to elements we need to update (in required XML Format)
batch.InnerXml = GetElementsToUpdate();
 
//Get the List Web Service Client
ListsSoapClient client = GetWebServiceClient();
 
//Make the call to insert the elements
var status = client.UpdateListItems(listGuid, batch);
 
//Write results..
Console.Out.WriteLine(status.InnerXml);
}
catch (Exception e)
{
Console.Out.WriteLine("Error trying to insert items..");
Console.WriteLine(e);
}
Console.Out.WriteLine("End of program..press enter to exit");
Console.ReadLine();

}

private static XmlElement GetBatchElement()
{
XmlDocument doc = new XmlDocument();
XmlElement batch = doc.CreateElement("Batch");
batch.SetAttribute("OnError", "Continue");
batch.SetAttribute("ListVersion", "1");
return batch;
}
 
private static String GetElementsToUpdate()
{
StringBuilder methodBuilder = new StringBuilder();
const string methodFormat = "<Method ID='1' Cmd='New'>" +
"<Field Name='ID'>New</Field>" +
"<Field Name='Title'>{0}</Field>" +
"</Method>";
for (int i = 0; i < 5; i++)
{
methodBuilder.AppendFormat(methodFormat,"Title " +i);
}
return methodBuilder.ToString();
}
 
private static ListsSoapClient GetWebServiceClient()
{
ListsSoapClient client = new ListsSoapClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
//client.Endpoint.Address = new EndpointAddress("http://your server/site/_vti_bin/lists.asmx");
return client;
}
 
}

}

 

 

As you can see, it is possible to change the server name, List GUID as highlighted above.

If you need to add a new column value to the List, you just need to include an extra Field element as shown below(where we also add a status element)

const string methodFormat = "<Method ID='1' Cmd='New'>" +

                             "<Field Name='ID'>New</Field>" +
                             "<Field Name='Title'>{0}</Field>" +

                              "<Field Name='Status'>{1}</Field>" +

                              "</Method>";

 

And then in loop

for (int i = 0; i < 5; i++)

{

    methodBuilder.AppendFormat(methodFormat,"Title " +I, "Status " +i);

}

 

 

Happy coding,

Hari

 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Comments
  • No comments exist for this entry.
Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.