Traverse the file library folders and files under SPWeb
Background:
Recently, we are working on a MOSS Farm management report. We need to create a management report By Farm, By Site, By Web, By Libray, By Document.
The lifecycle of the retrieved file. During development, Farm can be manually maintained (no more than 10 pieces of data), and site/web can
The web table is obtained, and Libray can be written to the C # Console program by using siteid and webid.
The data of the file library.
Finally, a problem occurs: how to program the SP object model to obtain the Document data.
Ideas:
1. Using the SPFold model, Feach traverses the SubFolders object to query the document model objects in the SubFolders.
2. Use spfolder. Files to obtain the document model objects in the current file directory.
3. The above ideas must be solved based on the known file library object SPDocumentLibray, and how to Coding to get the SPFolder object of this file library.
Solutions to previous problems,
Suppose we already know the siteid and webid information of the website,
The following describes how to traverse and construct the SPFolder object of all the file libraries on the website.
The Code test is as follows:
Namespace TestWeb
{
Public partial class WebForm1: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
String br = System. Environment. NewLine;
Guid siteid = new Guid ("5E601555-DD68-4518-889B-95D162A2A348 ");
Guid webid = new Guid ("5A7EF298-C322-4C41-9360-FE68AC63B23B ");
SPSite site = null;
SPWeb web = null;
SPListCollection lists = null;
SPFolder folder = null;
SPFolderCollection folders = null;
String s = "";
Try
{
Site = new SPSite (siteid );
Web = site. OpenWeb (webid );
Lists = web. GetListsOfType (SPBaseType. DocumentLibrary );
Foreach (SPList list in lists)
{
Try
{
Folder = list. RootFolder; // web. GetFolder (list. DefaultViewUrl); // failure: AllItems. aspx
S + = folder. Name + ";" + list. Title + "<br/> ";
}
Catch (Exception xx)
{
S + = xx. Message + ";" + list. Title + "<br/> ";
}
}
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
Site = null;
Web = null;
}
Response. Write (s );
}
}
}
}
Test Results
The rest is to use SPForder to traverse and collect information about the current file.
And the file information in the current subfolders. It is not described in detail.