簡體   English   中英

從 Sharepoint 列表中獲取所有項目,包括使用 CSOM 的查找值

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

我想從 Sharepoint 列表中檢索所有項目,包括查找字段的 ID 和值。 查找列表與查詢列表位於相同的站點/url。 目前我收到“屬性或字段尚未初始化”錯誤。

如何正確地將查找字段加載到客戶端上下文中,以便可以訪問該屬性?

在搜索和嘗試不同的事情但沒有成功之后,例如使用 list.GetRelatedFields() 獲取所有相關字段並通過迭代相關字段集合中的列表 ID 來加載所有列表中的所有字段,我需要幫助。

我不再只見樹木不見森林。 任何提示表示贊賞。 提前致謝。

更新

發生了兩件事。 首先,我也忘記將列表 object 加載到上下文中。 其次,我嘗試請求配置錯誤的屬性/列。

Sharepoint Online 上的列表是從另一個 Sharepoint 站點復制的,其中包括用於查找的列表的自動復制。

這種重復似乎破壞了查找列。 我必須手動刪除查找列並將其讀取到請求的列表中。

現在,它起作用了::)

    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);
            }

        }
    }

親切的問候,

西羅卡

您可以嘗試將列表項的所有字段作為文本加載到上下文中並僅顯示它們,我認為這是最簡單的方法,但可能不是最有效的方法。 在這種情況下,查找列也應該作為文本值(而不是 id)可見。

將字段值包含為文本您需要將其與加載項一起加載到上下文中。 因此,對於您的示例,它將類似於

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

之后,您應該能夠獲得查找列的值,例如:

item.FieldValuesAsText["LookupFieldName"]

我希望這會有所幫助

暫無
暫無

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

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