获得前5名创建的子网站在sharepoint没有for循环

本文关键字:sharepoint 没有 for 循环 网站 5名 创建 | 更新日期: 2023-09-27 18:16:47

我需要一个简单的方法来获得最新创建的5个web。

我知道每个网页都有一个Created Date属性。

我想这样做没有foreach,因为可能有1000个子站点。

任何想法?

 using (SPSite clientSiteCollection = new SPSite(currentUrl))
            {
                foreach (SPWeb web in clientSiteCollection.AllWebs.Select(c => c.Properties["WebTemplate"] == "xx").OrderByDescending(d => d.Created).Take(5))
                {

获得前5名创建的子网站在sharepoint没有for循环

您可以使用Linq。它应该是类似于这个。

var newestSites = clientSiteCollection.AllWebs.OrderByDescending(p=>p.CreatedDate).ToList().Take(5);
// Now NewestSites is a collection of your top 5 newest created Sites.

我建议您针对SharePoint搜索构建KeywordQuery,其中指定您只希望"SPWeb"作为搜索结果,并将托管属性"Created"作为查询中的标准。像这样:

myQuery.QueryText = "contentclass:STS_web Created>6/1/2013"

建筑关键词查询:http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2013/01/03/how-to-use-the-sharepoint-2013-search-keywordquery-class.aspx

另外,你配置KeywordQuery,你只需要5个结果:

myQuery.RowLimit = 5;

并且您希望它们按"Created"按降序排序:

myQuery.SortList.Add("Created", SortDirection.Descending);

排序搜索查询:http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2013/01/03/how-to-sort-search-queries-in-sharepoint-2013.aspx