簡體   English   中英

在沒有for循環的情況下獲取SharePoint中創建的前5個子站點

[英]Get top 5 created subsites in sharepoint without for loop

我需要一種簡單的方法來獲取最新創建的5個網站。

我知道每個網站都有一個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))
                {

您可以使用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;

並希望它們按降序按“已創建”排序:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM