Get list of folders inside a sharepoint document library

After doing a long search in net, I found a method to get the folder lists inside a document library. It cannot be achieved using a windows sharepoint webservice instead we should use frontpage RPC methods. List documents is one such method that will return the list of folders and documents inside a document library. But the return value will be in the HTML format. So i filtered only the folder names from it. Below i am giving the code snippet. This might be useful for you folks.

///
/// Gets the list of folders inside the given document library
///
/// Repository configuration parameter values.
/// Document library from which to get the folders list
/// returns the folders list
private ArrayList GetSharePointFolderList(string strRepositoryParamsXml, string selectedDocumentLibrary,string folderPath)
{
ArrayList foldersList = new ArrayList();
string strReplace = string.Empty;
string orgDocLibName = selectedDocumentLibrary;
string[] invalidChars = { “-”, “,”, “.”, “(“, “)” };
if (selectedDocumentLibrary.Length > 50)
{
selectedDocumentLibrary = selectedDocumentLibrary.Substring(0, 50);
selectedDocumentLibrary = selectedDocumentLibrary.Trim();
}
foreach (string invalidChar in invalidChars)
{
selectedDocumentLibrary = selectedDocumentLibrary.Replace(invalidChar, strReplace);
}
SharePointConnection shConn = new SharePointConnection(strRepositoryParamsXml, “”);
FrontPageRPCPublish fpRpcPublish = new FrontPageRPCPublish();
fpRpcPublish.AuthenticationType = shConn.AuthenticationType;
fpRpcPublish.Credentials = shConn.Credentials;
string resultList = fpRpcPublish.GetFolderList(selectedDocumentLibrary, shConn.Server);
foldersList = FormatRepositoryFolderList(resultList, orgDocLibName, folderPath);
return foldersList;
}

///
/// Gets the list of folders inside the given document library
///
/// Document library from which to get the folders list
/// sharepoint server url
/// folder list in html format
public string GetFolderList(string DocumentLibrary, string serverUrl)
{
string intUrl = DocumentLibrary;
string postBody = String.Format(
“method=list+documents:6.0.n.nnnn&service_name=/&listHiddenDocs=false&listExplorerDocs=false&listRecurse=true&listFiles=false&listFolders=true&listLinkInfo=true&listIncludeParent=true&listDerived=false&listBorders=false&listChildWebs=false&initialUrl={0}”,
intUrl);
string responseText = SendRequest(serverUrl + FrontPageRPCAuthorDLL, postBody);
return responseText;
}

///
/// creates the array of folder list from the html list.
///
/// html list of folder names and meta info
/// Document library from which to retrive the folder list
/// folder list array
private ArrayList FormatRepositoryFolderList(string resultList, string selectedDocumentLibrary, string folderPath)
{
int ind = resultList.IndexOf(“urldirs”);
int startIndex = 0;
int endIndex = 0;
string docLib = string.Empty;
ArrayList folders = new ArrayList();
if (ind > 0)
{
string resultFiltered = resultList.Substring(ind);

if (resultFiltered.Length > 0)
{
do
{
startIndex = resultFiltered.IndexOf(“url=”, endIndex) + 4;
endIndex = resultFiltered.IndexOf(” 50)
{
folder = folder.Replace(docLib, selectedDocumentLibrary);
}
if (!folder.Contains(“Forms”))
{
folder = folder.Replace(“/”, “\\”);
folders.Add(folder);
}
} while (startIndex != resultFiltered.LastIndexOf(“url=”) + 4);

}
}

return folders;
}

you can do the samething using one sitedata.asmx web service method called EnumerateFolde(). But it wont return the child folders of a folder inside doc lib. You may have to loop and call the methods several times.

SharePointConnection shConn = new SharePointConnection(strRepositoryParamsXml, “”);

SiteDataWS.SiteData srvSiteData = new SiteDataWS.SiteData ();

srvSiteData.Url = shConn.Server +“/_vti_bin/sitedata.asmx” ;
srvSiteData.Credentials = shConn.Credentials;
_sFPUrl [] enArray;

SiteDataWS.srvSiteData.EnumerateFolder(“Document Library”, out enArray);

Leave a Reply