GetListItems in the lists Webservice

Introduction:

In this article I am going to explain you that How to get list item using sharepoint web service.
To do this we will be using Lists.asmx web service and GetListItems web method.
GetListItems Returns information about items in the list based on the specified query.

The Code:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <summary>
/// This function return items based on query
/// </summary>
/// <param name="strListName">list name</param>
/// <param name="queryXml">query xml</param>
/// <param name="folder path">folder path e.g "Source/folder1" </param>
/// <returns>XmlNode</returns>
public System.Xml.XmlNode GetItems(string strListName, string queryXml, string destinationFolderPath)
{
	Lists.Lists listReference = new Lists.Lists();
	string lookupId = string.Empty;
	try
	{
		listReference.Credentials = credentials;
		listReference.Url = settings.SiteUri.ToString().TrimEnd('/') + 
                                    "/_vti_bin/lists.asmx";
		System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
		System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
		System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
		System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
		query.InnerXml = queryXml;
		viewFields.InnerXml = "<FieldRef Name="ID" />";
		queryOptions.InnerXml = "<Folder>" + destinationFolderPath + "</Folder>";
		System.Xml.XmlNode items;
		if (String.IsNullOrEmpty(destinationFolderPath))
			items = listReference.GetListItems(strListName, 
                                string.Empty, query, viewFields, string.Empty, null, null);
		else
			items = listReference.GetListItems(strListName, 
                                string.Empty, query, viewFields, string.Empty, queryOptions, null);
 
	   return items;
	}
	catch (Exception ex)
	{
		//exception
	}
	finally
	{
		if (listReference != null)
			listReference.Dispose();
	}
	return lookupId;
}

This function fetch item for specific folder or fetch items of list except folder.
But if you wish to receive all items of all folders using the Lists.asmx Web Service then you may need to use below line:-

queryOptions.InnerXml = ““;

References:-

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

Thanks!
Avinash

calendarMarch 16, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , , , , ,  · Posted in: MOSS, SharePoint, Web Service

Leave a Reply

Spam Protection: , required

myworldmaps infoyen