SharePoint: Calling Web Services Using WCF

This article describes how to call a SharePoint web service using WCF. In the example that follows, the Lists.asmx GetListItem webmethod is called from a console application that resides off a SharePoint server.

The first step is to add a service reference to your console application (simply right click on your console application project and select “Add Service Reference”. Set the address to the lists webservice at the root of your SharePoint webapp (this can be dynamically altered later). In my example, I call the service namespace “ListsWS”.


Note that for the code example below to work, you should ensure that the “Generate Asynchronous Operations” checkbox is unchecked in the advanced dialog.


On adding a service reference, some entries are automatically add to the app.config / web.config file for your project. By default the security mode for the webservice binding is set to “None” (meaning that your SharePoint webservice fails because it is called anonymously). To work around this, you must change the security block from:


<security mode=”None”>
    <transport clientCredentialType=”None” proxyCredentialType=”None” realm=”” />
    <message clientCredentialType=”UserName” algorithmSuite=”Default” />



<security mode=”TransportCredentialOnly”>
  <transport clientCredentialType=”Ntlm”/>

Now you can call the webservice as follows using the following code (ensure that you class references the System.ServiceModel, System.Net and System.Xml namespaces…

// Set up the webservice
ListsWS.ListsSoapClient lists = new ListsWS.ListsSoapClient();
// The line below forces the credentials passed to the webservice. If this is not set,
// the credentials that this console app is running as will be passed through
lists.ClientCredentials.Windows.ClientCredential = new NetworkCredential(“user”, “password”, “domain”);
lists.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

// Dynamically set the endpoint to the appropriate spweb               
lists.Endpoint.Address = new EndpointAddress(“

// Create xml nodes containing params to be passed to the webservice
// In this example, we are going to filter results to only show the item where ID = 1
XmlDocument xmlDoc = new XmlDocument();
XmlElement viewFields = xmlDoc.CreateElement(“ViewFields”);
viewFields.InnerXml = “”;
XmlElement queryOptions = xmlDoc.CreateElement(“QueryOptions”);
queryOptions.InnerXml = “”;
XmlElement query = xmlDoc.CreateElement(“Query”);
query.InnerXml = “<Where><Eq><FieldRef Name=\”ID\” /><Value Type=\”String\”>1</Value></Eq></Where>”;

// Call the webmethod
XmlElement nodeListItems = lists.GetListItems(“Shared Documents”, null, query, viewFields, “1”, queryOptions, null);

// Parse out the items
XmlDocument resultsDoc = new XmlDocument();

// An XmlNameSpaceManager is required to pass out rs and z elements
XmlNamespaceManager listsNsManager = new XmlNamespaceManager(resultsDoc.NameTable);
listsNsManager.AddNamespace(“rs”, “urn:schemas-microsoft-com:rowset”);
listsNsManager.AddNamespace(“z”, “#RowsetSchema”);

// Display the fileref of the first returned item
XmlNodeList items = nodeListItems.SelectNodes(“/rs:data/z:row”, listsNsManager);


Key points to note on the above code are that:

  1. lists.ClientCredentials.Windows.ClientCredential can be omitted – in which case, the credentials that your app is running under will be passed through to the SharePoint webservice.
  2. The URL to the webservice can be overridden using lists.Endpoint.Address.
  3. In the example above, an xmlnamespacemanager is required to perform xpath on the results.

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=""> <strike> <strong>