简体   繁体   English

迭代SPListItem / SPList

[英]Iterating through a SPListItem/SPList

I'm making a checklist for people to do tasks on our company website. 我正在制作一份清单,供人们在我们公司的网站上完成任务。 The feature I'm working on right now is that once they complete a task and mark it as done, I want our sharepoint webpart to send an email to them containing a list of what they still need to finish. 我现在正在处理的功能是,一旦他们完成任务并将其标记为已完成,我希望我们的sharepoint webpart向他们发送一封电子邮件,其中包含他们仍需要完成的内容列表。

Since the powers that be want to be able to add and subtract items to the list at will, it needs to be modular, therefore checking the list needs to be modular and written in modular terms as well. 由于希望能够随意向列表添加和减去项目的权力,它需要是模块化的,因此检查列表需要是模块化的并且也以模块化术语编写。

The problem that I have is that all the tasks are stored in a SPList on the existing code. 我遇到的问题是所有任务都存储在现有代码的SPList中。 I'm not exactly sure how to iterate over the list to find out whether that person has the done the following task already. 我不确定如何遍历列表以查明该人是否已完成以下任务。 I'm not seeing any next, previous, head or tail operations (as I would expect from a list). 我没有看到任何next,previous,head或tail操作(正如我所期望的那样)。 Since indexing uses a string, it behaves more like an associative data structure (map) or primary key from a DB. 由于索引使用字符串,因此其行为更像是关联数据结构(映射)或来自数据库的主键。

Help please and thanks everyone! 请帮助,谢谢大家!

Cheers, 干杯,

-Jeremiah Tantongco -Jeremiah Tantongco

A SharePoint list is not a list in the Computer Science way, but is what an End-User would expect a list to be: a listing of things SharePoint列表不是计算机科学方式的列表,而是最终用户期望列表的列表:事物列表

You should more look at a list as being similar to a database table. 您应该将列表视为与数据库表类似。 If you want to find records (Items) in a table (List) based on conditions then you should use a query. 如果要根据条件在表(List)中查找记录(Items),则应使用查询。

So the right way to find SPListItems in a SPList is to use an SPQuery where you specify your criteria in a nice XML syntax (CAML), then pass the SPQuery to SPList.GetItems and thereby get a collection of SPListItems back. 因此,在SPList中查找SPListItem的正确方法是使用SPQuery,您可以在其中以精美的XML语法(CAML)指定条件,然后将SPQuery传递给SPList.GetItems,从而获得SPListItems的集合。

This should give you some terms to google for. 这应该给你一些谷歌的条款。 If you need more help with the syntax of the query then use U2U Caml Query Builder or give us some more specific infomations. 如果您需要有关查询语法的更多帮助,请使用U2U Caml查询生成器或给我们一些更具体的信息。

Whether you should iterate through the list or not may be a matter of debate, but if it's unavoidable (as it was in my case) the following code can be used. 是否应该遍历列表可能是一个有争议的问题,但如果它是不可避免的(就像我的情况一样),可以使用以下代码。

        SPSite siteCollection = new SPSite("http://localhost/");
        SPWeb site = siteCollection.RootWeb;
        SPList myList = site.Lists["My List"];
        SPListItemCollection itemCollection = myList.Items;

        foreach (SPListItem item in itemCollection)
        {
            //Do something with each item
        }

This may solve your problem 这可以解决您的问题

SPList interviewList = myWeb.Lists["listtoiterate"];
foreach (SPListItem interview  in interviewList)
{
// Do Something
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM