如何从多个列表中对 SPListItems 列表进行最佳排序?

发布于 2024-10-20 06:12:17 字数 1858 浏览 4 评论 0原文

我的问题更多的是“有更好的方法吗”问题,而不是“我该怎么做”问题。我的代码如下:

private List<SPListItem> GetListItemsFromSite(SPSite spSite)
{
    List<SPListItem> listItems = new List<SPListItem>();

    List<SPList> documentLibrariesInSite = GetCustomDocumentLibrariesInSite(spSite);
    foreach (SPList spList in documentLibrariesInSite)
    {
        SPListItemCollection itemCollection = GetLatestDocumentsFromSPList(spList);
        foreach(SPListItem item in itemCollection)
        {
            listItems.Add(item);
        }
    }
    listItems.Sort((x, y) => ((DateTime) x[SPBuiltInFieldId.Created]).CompareTo(((DateTime) y[SPBuiltInFieldId.Created])));
    listItems.Reverse();
    return listItems;
}

private SPListItemCollection GetLatestDocumentsFromSPList(SPList spList)
{
    SPQuery query = new SPQuery();
    query.Query = "<Where><Gt><FieldRef Name=\"Created\" /><Value Type=\"DateTime\"><Today OffsetDays=\"-7\"</Value></Gt></Where>";
    query.ViewAttributes = "Scope=\"Recursive\"";
    query.RowLimit = NumberOfDocumentsToGet;
    SPListItemCollection listItemCollection = spList.GetItems(query);
    return listItemCollection;
}

private List<SPList> GetCustomDocumentLibrariesInSite(SPSite spSite)
{
    List<SPList> listCollection = new List<SPList>();
    foreach (SPWeb spWeb in spSite.AllWebs)
    {
        foreach (SPList spList in spWeb.Lists)
        {
            if (spList.TemplateFeatureId == CustomContentTypeFeatureGuid)
            {
                listCollection.Add(spList);
            }
        }
        spWeb.Dispose();
    }
    return listCollection;
}

由于我希望在返回项目之前按创建日期对列表项进行排序,因此我根据 SPListItem 上的 CreatedDate 字段进行排序。我也不使用 SPSiteDataQuery 因为我想操作我得到的 SPListItems。

现在的问题是我是否有任何方法可以更有效或更“干净”地做到这一点?欢迎对此事有任何想法。

My question is more of a "Is there a better way to do it" question then a "How do I do it" question. My code is as follows:

private List<SPListItem> GetListItemsFromSite(SPSite spSite)
{
    List<SPListItem> listItems = new List<SPListItem>();

    List<SPList> documentLibrariesInSite = GetCustomDocumentLibrariesInSite(spSite);
    foreach (SPList spList in documentLibrariesInSite)
    {
        SPListItemCollection itemCollection = GetLatestDocumentsFromSPList(spList);
        foreach(SPListItem item in itemCollection)
        {
            listItems.Add(item);
        }
    }
    listItems.Sort((x, y) => ((DateTime) x[SPBuiltInFieldId.Created]).CompareTo(((DateTime) y[SPBuiltInFieldId.Created])));
    listItems.Reverse();
    return listItems;
}

private SPListItemCollection GetLatestDocumentsFromSPList(SPList spList)
{
    SPQuery query = new SPQuery();
    query.Query = "<Where><Gt><FieldRef Name=\"Created\" /><Value Type=\"DateTime\"><Today OffsetDays=\"-7\"</Value></Gt></Where>";
    query.ViewAttributes = "Scope=\"Recursive\"";
    query.RowLimit = NumberOfDocumentsToGet;
    SPListItemCollection listItemCollection = spList.GetItems(query);
    return listItemCollection;
}

private List<SPList> GetCustomDocumentLibrariesInSite(SPSite spSite)
{
    List<SPList> listCollection = new List<SPList>();
    foreach (SPWeb spWeb in spSite.AllWebs)
    {
        foreach (SPList spList in spWeb.Lists)
        {
            if (spList.TemplateFeatureId == CustomContentTypeFeatureGuid)
            {
                listCollection.Add(spList);
            }
        }
        spWeb.Dispose();
    }
    return listCollection;
}

Since I want the list items sorted by created date before i return the items i do a sort based on the CreatedDate field on the SPListItem. I also dont use the SPSiteDataQuery because i want to manipulate the SPListItems that i get.

Now the question is if i have any ways of doing this more efficient or "cleaner"? Any thoughts on the matter are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-10-27 06:12:17

之后向查询添加 orderby 子句

您可以在 where 子句升序

<OrderBy> 
   <FieldRef Name="Created" Ascending="True" />
</OrderBy>

降序

<OrderBy> 
   <FieldRef Name="Created" Ascending="false" />
</OrderBy>

you could add an orderby clause to your query AFTER the where clause

Ascending

<OrderBy> 
   <FieldRef Name="Created" Ascending="True" />
</OrderBy>

Descending

<OrderBy> 
   <FieldRef Name="Created" Ascending="false" />
</OrderBy>
拒绝两难 2024-10-27 06:12:17

如果自定义列表中的项目具有公共托管属性或属性集,您可以为它们构建自定义搜索范围并查询此范围,而不是单独遍历每个列表。但是,如果您需要实时结果,这将不起作用,但如果您将增量爬网设置为足够频繁地运行,则可以更快、更轻松地获取项目,特别是如果您有许多自定义列表。

If the items in your custom lists have a common managed property or set of properties, you could build a custom search scope for them and query this scope instead of traversing each list individually. However, this will not work if you need real time results, but if you set up your incremental crawls to run frequently enough, you could get your items much faster and easier, especially if you have many of those custom lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文