Get List Items from List / Library with Pagination in Moss 2007 using Web Services
In this article I'm going to explain how to get the files under Document Library / List with pagination, assume that in our library or list we have lakhs of records in SharePoint we can't get all the items at single shot. If we did that we got service error, to overcome that and got the records we have to implement the pagination concept.
Get List Items from List / Library with Pagination in Moss 2007 using Web Services :
Index :
1. Description
2. Web Service Methods
3. Source Code
4. Output
5. Conclusion1. Description :
In this article I'm going to explain how to get the files under Document Library / List with pagination, assume that in our library or list we have lakhs of records in SharePoint we can't get all the items at single shot. If we did that we got service error, to overcome that and got the records we have to implement the pagination concept.
In this article I'm going to explain clearly how to get the items using pagination in Moss 2007 using Web services.2. Web Service Methods :
GetListItems: This method help us to get the list or library items from Moss 2007 using Web Service.
Syntax: GetListItems (string listName, string viewName, XmlNode query, XmlNode viewFields, string rowLimit, XmlNode queryOptions, string webID);3. Source Code :
I just create the Web reference to the project for accessing the SharePoint site and get the list of files over there. To accomplish that we have to pass the web reference URL in the below format.
Web service Uri Format: "http://Site:Port/_vti_bin/lists.asmx"
using statements;
namespace SPWebservices
{
public class SPServices
{
static string pageCnt = string.Empty;
public void GetListItems(string serviceURL, string libName)
{
try
{
#region Retrieve Items & Folders under library
XmlDocument doc = new XmlDocument();
//open the web service connection
using (Lists listProxy = new Lists())
{
//check the page count
while (pageCnt != null)
{
string query = GetQuery();
doc.LoadXml(query);
XmlNode queryNode = doc.SelectSingleNode("//Query");
XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");
//pass the web service url to get the list of methods over there
listProxy.Url = serviceURL;
//pass the default credentials to access the same.
listProxy.UseDefaultCredentials = true;
//get the items from server and set the row limit of 5,000, in SP that is the max limit to get the items without any issue's.
XmlNode retNode = listProxy.GetListItems(libraryName, string.Empty, queryNode, viewNode, "5000", optionNode, "");
//the below line help us to get the next set of records and to pass this in CAML query we can get next set of records
XmlNode xmlPageCnt = retNode.SelectSingleNode("//@ListItemCollectionPositionNext");
//assign current page count to the varriable, this help us to get the next set of records.
pageCnt = xmlPageCnt != null ? xmlPageCnt.InnerXml : null;
DataSet ds = new DataSet();
using (StringReader sr = new StringReader(retNode.OuterXml))
ds.ReadXml(sr);
if (ds.Tables["Row"] != null && ds.Tables["Row"].Rows.Count > 0)
{
//perform action as you want here
}
}
}
#endregion
}
catch (Exception ex)
{
throw;
}
}
private static string GetViewFields()
{
string query = string.Empty;
try
{
#region ViewFields
query = @"<mylistitemrequest>
<Query>
</Query>
<ViewFields>
</ViewFields>
<QueryOptions>
<Paging ListItemCollectionPositionNext='{0}' />
<ViewAttributes Scope='RecursiveAll' IncludeRootFolder='FALSE' />
</QueryOptions>
</mylistitemrequest>";
query = string.Format(query, pageCnt);
#endregion
}
catch (Exception ex)
{
throw;
}
return query;
}
}
}
Note: To get the next set of records in paging we have to set the ListItemCollectionPositionNext property in Paging attribute under QueryOption.4. Output :
The output of this program is we can get all the items (there is no limit) under Document Library with Paging. 5. Conclusion :
This article helps you to get all the items from library/ list without any interruption by passing paging as QueryOption attribute and get the set of records using RowLimit property while Get the list of items from web service.