2013-06-04 13 views
0

我需要一种简单的方法来获取最新创建的5个站点。获取排名前5位的SharePoint站点,无需循环使用

我知道每个网站都有一个创建日期属性。

我想这样做没有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)) 
       { 
+0

Linq?排序方式创建日期desc并做一个采取5. – Bearcat9425

+0

语法请。 –

+0

添加了下面的语法让我们看看它是如何工作的。 – Bearcat9425

回答

1

您可以使用Linq。这将是类似的东西。

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

这将重复进入数据库来打开每一个'SPWeb',而不是处理它们中的任何一个。同样,对于LINQ来说,如果你想要一个列表,可以使用ToList来调用你最后的方法。 – Rawling

+0

使用包含在using语句中的SPSite,并且每当完成查询后,每个SPWeb都是该对象的属性,SPSite对象将与所有SPWeb对象一起处理。 – Bearcat9425

+0

@打开另一种方法? –

0

我建议你建立针对SharePoint搜索,在那里你指定你只想“的SPWeb”作为搜索结果并通过管理属性“创建”作为查询条件的KeywordQuery。事情是这样的:

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

大厦KeywordQuery:http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2013/01/03/how-to-use-the-sharepoint-2013-search-keywordquery-class.aspx

此外,还配置KeywordQuery你只想要5个结果:

myQuery.RowLimit = 5; 

以及您希望他们通过 “创建” 分类中降序排列:

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

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

相关问题