在SharePoint中通過SPQuery.Folder屬性, 可以很方便的查詢列表下檔案夾中的資料, 下面介紹如何使用:
- SPQuery.Folder: Gets or sets the folder within a document library from which to return items in the query.
using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb){ oWebsiteRoot.Lists.IncludeRootFolder = true; SPList oList = oWebsiteRoot.Lists["Document_Library_Name"]; SPFolder oFolder = oList.RootFolder.SubFolders["Folder_Name"]; SPQuery oQuery = new SPQuery(); oQuery.Folder = oFolder; SPListItemCollection collListItems = oList.GetItems(oQuery); foreach (SPListItem oListItem in collListItems) { Response.Write(SPEncode.HtmlEncode(oListItem.File.Name) + "<BR>"); }}
SPSite oSiteCollection = SPContext.Current.Site;SPWebCollection collWebsites = oSiteCollection.AllWebs;foreach (SPWeb oWebsite in collWebsites){ SPFolderCollection collFolders = oWebsite.Folders; foreach (SPFolder oFolder in collFolders) { SPFileCollection collFiles = oFolder.Files; long lngTotalFileSize = 0; for (int intIndex = 0; intIndex < collFiles.Count; intIndex++) { lngTotalFileSize += collFiles[intIndex].Length; } Label1.Text += " Web: " + SPEncode.HtmlEncode(oWebsite.Name) + " Folder: " + SPEncode.HtmlEncode(oFolder.Name) + " Number: " + oFolder.Files.Count + " Size: " + lngTotalFileSize + "<BR>"; } oWebsite.Dispose();
}
文章來源: