Get List Items through Recurrsive Sub-Folder in Moss 2007 using Web Services


In this article I'm going to explain how to get the files under Document Library Folders and Sub Folders, for example If the folders are Recursive then how to get the items behind it. In this article I'm going to explain clearly how to get the items in Recursive Sub folders in Moss 2007 using Web services.

Get List Items through Recurrsive Sub-Folder in Moss 2007 using Web Services :




Index:



1. Description
2. Web Service Methods
3. Source Code
4. Output
5. Conclusion

1. Description:



In this article I'm going to explain how to get the files under Document Library Folders and Sub Folders, for example If the folders are Recursive then how to get the items behind it.

In this article I'm going to explain clearly how to get the items in Recursive Sub folders 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 their. To accomplish that we have to pass the web reference URL in the below format.

Web service Url Format : "http://Site:Port/_vti_bin/lists.asmx"


public DataTable GetItemsWithFolders(string serviceURL, string libName)
{
try
{
#region Retrieve Items & Folders under library
XmlDocument doc = new XmlDocument();

string query = GetQuery(libName);

doc.LoadXml(query);
XmlNode queryNode = doc.SelectSingleNode("//Query");
XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");


//connect to the web service
using (Lists listProxy = new Lists())
{
//pass the service Url
listProxy.Url = serviceURL;

// use default network credentials
listProxy.UseDefaultCredentials = true;

//get the list items from web services
XmlNode retNode = listProxy.GetListItems(libraryName, string.Empty, queryNode, viewNode, "5000", optionNode, "");

DataSet ds = new DataSet();

//read the xml data into dataset
using (StringReader sr = new StringReader(retNode.OuterXml))
ds.ReadXml(sr);


if (ds.Tables["Row"] != null && ds.Tables["Row"].Rows.Count > 0)
{
#region FileDownload with versions
FileDownload(ds,libName);
#endregion

#region retrieve sub folders & items

dtFolders = GetSubFolderInfo(ds);

#endregion
}

}
#endregion
}
catch (Exception ex)
{
throw;
}
return dtFolders;
}

private static string GetQuery(string parentFolder)
{
string viewFields = //your viewfields;
string query = string.Empty;
try
{
#region ViewFields
query = @"<mylistitemrequest>
<Query> < /Query>
<ViewFields> {0}</ViewFields>
<QueryOptions><Folder>{1}</Folder><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='FALSE' />
</QueryOptions></mylistitemrequest>";
query = string.Format(query, viewFields, parentFolder);
#endregion
}
catch (Exception ex)
{
throw;
}
return query;
}

Note: To target the each folder we have to pass the Folder path in QueryOptions.

private DataTable GetSubFolderInfo(DataSet ds)
{

DataTable dtFolders = null;
try
{
//get the folders from query
var rows = ds.Tables["Row"].AsEnumerable().Where(r => r.Field("ows_FSObjType").Contains(";#1"));

if (rows.Any())
{
dtFolders = rows.CopyToDataTable();
foreach (DataRow drFolder in dtFolders.Rows)
{
if (drFolder.Table.Columns.Contains("ows_EncodedAbsUrl") && Convert.ToString(drFolder["ows_EncodedAbsUrl"]) != string.Empty)
{
string parentFolder = Convert.ToString(drFolder["ows_EncodedAbsUrl"]);
GetListItemSubFolders(dtFolders, parentFolder);
}
}
}
}
catch (Exception ex)
{
throw;
}
return dtFolders;
}

private void GetListItemSubFolders(DataTable retTable, string parentFolder)
{
try
{
string query = GetQuery(parentFolder);


using (Lists listProxy = new Lists())
{
listProxy.Url = siteURL;
listProxy.UseDefaultCredentials = true;

XmlDocument doc = new XmlDocument();
doc.LoadXml(query);

XmlNode queryNode = doc.SelectSingleNode("//Query");
XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");

XmlNode retNode = listProxy.GetListItems(libraryName, string.Empty, queryNode, viewNode, string.Empty, optionNode, "");


DataSet ds = new DataSet();
using (StringReader sr = new StringReader(retNode.OuterXml))
ds.ReadXml(sr);


DataTable dtFolders = null;

if (ds.Tables["Row"] != null && ds.Tables["Row"].Rows.Count > 0)
{

#region FileDownload with versions
FileDownloadWithVersions(ds, parentFolder);
#endregion

#region retrieve sub folders
dtFolders = GetSubFolderInfo(ds);
#endregion
}
}
}
catch (Exception ex)
{
throw;
}
}
Private void FileDownloadWithVersions(DataSet ds, string parentFolder)
{
try
{
var rows = ds.Tables["Row"].AsEnumerable().Where(r => r.Field("ows_FSObjType").Contains(";#0"));

if (rows.Any())
{
//download files
}
}
Catch(Exception ex)
{
throw;
}

}


4. Output:



The output of this program is files to be downloaded in to the destination folder.

5. Conclusion:



This article help us to download the files under Recurrsive sub Folders in SharePoint Document Library.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: