2013-06-04 14 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

相關問題