简体   繁体   English

从 Sharepoint 列表中获取所有项目,包括使用 CSOM 的查找值

[英]Get all items from Sharepoint list including lookup values using CSOM

I want to retrieve all items from a Sharepoint list including the IDs and values of a lookup field.我想从 Sharepoint 列表中检索所有项目,包括查找字段的 ID 和值。 The lookup list resides on the same site/url as the queried list.查找列表与查询列表位于相同的站点/url。 Currently I'm getting a "property or field has not been initialized" error.目前我收到“属性或字段尚未初始化”错误。

How to load the lookup field into the client context correctly, so that the property can be accessed?如何正确地将查找字段加载到客户端上下文中,以便可以访问该属性?

After searching and trying different things without success, eg getting all related fields with list.GetRelatedFields() and loading all fields in all lists by iterating the list IDs in the collection of related fields, I need help.在搜索和尝试不同的事情但没有成功之后,例如使用 list.GetRelatedFields() 获取所有相关字段并通过迭代相关字段集合中的列表 ID 来加载所有列表中的所有字段,我需要帮助。

I can't see the forest for the trees anymore.我不再只见树木不见森林。 Any hint appreciated.任何提示表示赞赏。 Thanks in advance.提前致谢。

UPDATE更新

Two things happened.发生了两件事。 First, I've forgot to load the list object into the context too.首先,我也忘记将列表 object 加载到上下文中。 Second, I've tried to request a property/column with faulty configuration.其次,我尝试请求配置错误的属性/列。

The list on Sharepoint Online was duplicated from another Sharepoint site which includes the automatic duplication of lists used for lookups. Sharepoint Online 上的列表是从另一个 Sharepoint 站点复制的,其中包括用于查找的列表的自动复制。

It seems that this duplication has corrupted the lookup column.这种重复似乎破坏了查找列。 I had to delete and readd the lookup column manually to the requested list.我必须手动删除查找列并将其读取到请求的列表中。

Now, it works: :)现在,它起作用了::)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using SP = Microsoft.SharePoint.Client;

    namespace Theroka {

        class Sharepoint {

            private private SP.ClientContext context;

            // credential handling, authorization, context creation, etc. ....
            
            public void getItems() {
                SP.List list = this.context.Web.Lists.GetByTitle("NameOfList");
                this.context.Load(list); // UPDATE: I've forgot to load the list too
                this.context.Load(list.Fields, c => c.Where(e => e.Hidden != true));
                this.context.ExecuteQuery();

                ListItemCollectionPosition position = null;
                do {
                    CamlQuery query = CamlQuery.CreateAllItemsQuery(5000);
                    query.ListItemCollectionPosition = position;
                    SP.ListItemCollection items = list.GetItems(query);
                    this.context.Load(items);
                    this.context.ExecuteQuery();
                    position = items.ListItemCollectionPosition;
                    foreach(SP.ListItem item in items) {
                        // this works now
                        SP.FieldLookupValue lv = (item["LookupFieldName"] as SP.FieldLookupValue);
                        Console.WriteLine("{0}\t{1}\t{2}", item.Id, item["Title"], lv.LookupValue.ToString());
                    }
                } while (position != null);
            }

        }
    }

Kind regards,亲切的问候,

theroka西罗卡

You could try to load all fields of list items as text to the context and the just display them, I think this is the easiest way but maybe not the most effective one.您可以尝试将列表项的所有字段作为文本加载到上下文中并仅显示它们,我认为这是最简单的方法,但可能不是最有效的方法。 The lookup columns should also be visible as text value (not id) in this case.在这种情况下,查找列也应该作为文本值(而不是 id)可见。

to include field values as text You need to load it to the context together with loading items.将字段值包含为文本您需要将其与加载项一起加载到上下文中。 So for Your example it would be something like因此,对于您的示例,它将类似于

this.context.Load(items, items => items.Include(item => item.FieldValuesAsText));

after that You should be able to get the value of the lookup column like:之后,您应该能够获得查找列的值,例如:

item.FieldValuesAsText["LookupFieldName"]

I hope this will be of any help我希望这会有所帮助

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

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