Reader Level:
ARTICLE

SharePoint 2010 - Web Services

Posted by Jean Paul Articles | SharePoint June 05, 2012
SharePoint Web Services provides us a good feature that allows us to access or change the SharePoint items remotely.
  • 0
  • 0
  • 59006
Download Files:
 

SharePoint Web Services provides us a good feature that allows us to access or change the SharePoint items remotely. We can use the Web Services to add more power to our application.

The following are the core features of SharePoint Web Services:

  • Foundation and Server Web Services
  • More Interoperability compared with Server Object Model
  • Above 25 Web Services
  • Manage Sites, Lists, Libraries, and Picture Libraries etc.
  • Run queries against the server
  • Custom Web Service creation possible

The following table contains some of the web services inside SharePoint 2010:

Service

Description

ASMX

WebSvcAdmin

Creation and Deletion of sites

Admin.asmx

WebSvcLists

List Management service

Lists.asmx

WebSvcAlerts

List Item Events service

Alerts.asmx

WebSvcAuthentication

Authentication based service

Authentication.asmx

WebSvcsites

Web sites management service

Sites.asmx

WebSvcspsearch

Searching service

Search.asmx

A more detailed list can be found here:

http://msdn.microsoft.com/en-us/library/ee705814.aspx

How to find the URL of a Service?

The URL of a service can be located in the _vti_bin folder of the site or site collection. Each site or site collection will have this folder with all the web services inside it with a .asmx extension.

Example: To use the search service use the following URL:

http://YOURPCNAME/_vti_bin/search.asmx

WebShare1.jpg

Using http://appes-pc/_vti_bin/Lists.asmx

WebShare2.jpg

Adding Reference to Project

Now we can try adding a reference to the service inside the Visual Studio application. Create a new Ordinary Console Application (please note that we are not using a SharePoint Console Application):

WebShare3.jpg

Now we need to Add a Web Reference. In Visual Studio 2010 this option is available under the Add Service Reference option as shown below:

WebShare4.jpg


In the dialog that appears click the Advanced button.

WebShare5.jpg


In the next dialog, choose the Add Web Reference option.

WebShare6.jpg

You will get the following dialog. This dialog is suitable for adding web service references.

WebShare7.jpg

Enter the URL of the Lists.asmx as shown above. You need to replace the PC name with yours.

Set a Web reference name in the second highlighted box. This serves as the namespace.

Click the Add Reference button and your proxy class and related classes will be ready within a while. Inside the Program.cs, enter the namespace and you can see the classes inside it as shown below:

WebShare8.jpg


Invoking the Proxy Class

Our class of interest is the Lists proxy. You can create an instance of it using the following code:

SPReference.Lists client = new SPReference.Lists();

Setting Credentials

Whenever we open a SharePoint site (without anonymous login enabled) the site will prompt with user credentials. As we are invoking through a web service the prompt dialog won't work. We need to pass the credentials using the property named Credentials.

client.Credentials = new NetworkCredential("appes", "password"); / // using System.Net;

Getting a Reference to a List

We can try getting a reference to the Manager List inside our site. The Managers list contains items with columns:

  • Title
  • Name
  • Address

WebShare9.jpg

Use the following code to fetch the data and display it to the console:

SPReference.Lists client = new SPReference.Lists();
client.Credentials = new NetworkCredential("appes", "PWD");
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
viewFields.InnerXml = "<FieldRef Name=\"Title\" />" +
    "<FieldRef Name=\"Name\" />" +
    "<FieldRef Name=\"Address\" />";

XmlNode listItems = client.GetListItems("Manager", null, null, viewFields, null, null, null);
foreach (XmlNode node in listItems)
    if (node.Name == "rs:data")
        for (int f = 0; f < node.ChildNodes.Count; f++)
        {
            if (node.ChildNodes[f].Name == "z:row")
            {
                string title = node.ChildNodes[f].Attributes["ows_Title"].Value;
                string name = node.ChildNodes[f].Attributes["ows_Name"].Value;
                string address = node.ChildNodes[f].Attributes["ows_Address"].Value;

                Console.WriteLine(title + " " + name + " " + address);
            }
        }

Console.ReadKey(false);

On executing the application we can see the following result:

WebShare10.jpg

The following are the steps involved in getting list item data:

  1. Create List Proxy client

  2. Set Credentials

  3. Set the View Columns

  4. Invoke GetListItems() method

  5. Iterate through the nodes

  6. Get value of node having name z:row

Note

Using web services, XML is used to represent the data. For advanced programming using properties, please refer to the Server Object Model and SharePoint LINQ articles.

References

http://msdn.microsoft.com/en-us/library/ee705814.aspx
http://msdn.microsoft.com/en-us/library/ms583494.aspx
http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems%28v=office.12%29.aspx

Summary

In this article we have seen the Web Services feature of SharePoint 2010. It provides the flexibility of interoperability compared with the Server Object Model. Developers can access and manage a SharePoint site from non .Net platforms like Java. The attached source code contains the example we discussed.
 

COMMENT USING

Trending up