获得第一个 &Sharepoint 列表中的最后一个 ListItem [英] Get the First & Last ListItem in Sharepoint List

查看:39
本文介绍了获得第一个 &Sharepoint 列表中的最后一个 ListItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pageLoad 事件中,我想转到两个不同的 SharePoint 列表,并从一个列表中获取第一个 ListItem,在另一个列表中获取最后一个 ListItem.我不知道任何 ListItem ID,所以我想这需要通过列表索引来完成 - 只是不知道如何实现这一点.掌握了 List 后,有人可以建议(使用 c#)获取上述场景的第一个和最后一个项目的最佳方法吗?

On a pageLoad event I want to go to two different SharePoint Lists and get the first ListItem from one list and an the last ListItem in another. I don't know any of the ListItem ID's so I guess this needs to be done through the list index - just not sure how to implement this. Having got a handle on the List can someone advise (using c#) the best way to get the first and last items for the scenario outlined above?

推荐答案

条件first item"和last item"不是很清楚.假设您要检索第一个和最新的创建项.需要注意的是,对于大型列表,通过 SPListItemCollection 的索引检索项目可能会很慢,因为访问 SPList.Items 会返回列表的所有项目.这就是我建议使用 CAML 查询的原因:

The condition "first item" and "last item" is not very clear. Assuming you want to retrieve the first and the latest created item. Important to note is that retrieving items via the index of the SPListItemCollection can be slow for large lists since accessing SPList.Items returns all items of the list. That's why I suggest using a CAML query:

SPList list = // some list;
SPQuery query = new SPQuery();
query.RowLimit = 1;
query.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";

return list.GetItems(query).Cast<SPListItem>().FirstOrDefault();

对于最后一项",您必须颠倒顺序:

For the "last item" you have to reverse the ordering:

<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>

如果您想根据另一个条件获取最后一个和第一个项目,您只需更改字段的顺序.

If you want to get the last and the first item based on another condition you just have to change the order by field.

更新:
更好的是按 ID 字段排序.这样你就可以达到同样的结果.正如@Kobi 指出的那样,不仅更好,创建的字段还可以通过代码进行更改.

Update:
Even better would be to order by the ID field. With that you could achieve the same result. Not only better, the created field could have been changed through code as pointed out by @Kobi.

这篇关于获得第一个 &amp;Sharepoint 列表中的最后一个 ListItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆