正在按内容类型获取网站集中的所有文件

本文关键字:集中 文件 网站 获取 类型 | 更新日期: 2023-09-27 18:26:33

是否可以按内容类型获取整个网站集中的所有文件?这是我目前拥有的代码:

//Get list of subsites to traverse
            List<string> subsites = getSubSiteList();
            foreach (string siteUrl in subsites)
            {
                using (SPWeb web = new SPSite(siteUrl).OpenWeb())
                {
                    if (web.GetFolder("Pages").Exists)
                    {
                        //Get Pages Folder from subsite                    
                        SPFolder folderCol = web.Folders["Pages"];
                        //Set files in Pages folder
                        SPFileCollection fileCol = folderCol.Files;
                        foreach (SPFile file in fileCol)
                        {
                            //Debug.WriteLine("File in site: " + siteUrl + " file: " + file.Name);
                        }
                    }
                    web.Dispose();
                }
            }

在这段代码中,我遍历了所有的子网站,并转到"页面"文件夹来获取某个页面,但我希望能够提取所有这些文件,而不必遍历每个子网站,按内容类型获取,而不是进入"页面"文件夹并从中进行分析。代码会是什么样子?谢谢

正在按内容类型获取网站集中的所有文件

您要查找的是站点数据查询。

其他选项是以编程方式利用搜索功能,但我认为这不如一个选项好;站点数据查询正是为此而设计的。

您可以尝试这样的方法来获取所有站点以及子站点有关可以使用的不同选项的列表,请参阅此MSDN站点。如何:枚举网站和网站集

SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsite = oSiteCollection.AllWebs;
for (int i = 0; i < collWebsite.Count; i++)
{
    using (SPWeb oWebsite = collWebsite[i])
    {
        SPListCollection collList = oWebsite.Lists;
        for (int j = 0; j < collList.Count; j++)
        {
            Label1.Text += SPEncode.HtmlEncode(collWebsite[i].Title) + "   "
                + SPEncode.HtmlEncode(collList[j].Title) + "<BR>";
        }
    }