C# 使用C仅从SharePoint列表中提取值#

C# 使用C仅从SharePoint列表中提取值#,c#,sharepoint,C#,Sharepoint,我正在尝试提取SharePoint列表的值,以便与Revit一起使用,以更新某些图元的状态参数。经过多次尝试后,如果我知道每个ListItem中字典的键,我可以连接并获取这些值,但这种方法存在许多问题。 第一个是需要知道密钥,有时密钥会因为编码而更改,一次获取所有列表值对我来说会更有效率。我试着像一些教程一样使用GetDataTable,但它似乎不适用于客户端。 第二个问题是,有时我无法获取列表的值,但可以获取值的描述,如“Microsoft.SharePoint.Client.FieldLoo

我正在尝试提取SharePoint列表的值,以便与Revit一起使用,以更新某些图元的状态参数。经过多次尝试后,如果我知道每个ListItem中字典的键,我可以连接并获取这些值,但这种方法存在许多问题。 第一个是需要知道密钥,有时密钥会因为编码而更改,一次获取所有列表值对我来说会更有效率。我试着像一些教程一样使用GetDataTable,但它似乎不适用于客户端。 第二个问题是,有时我无法获取列表的值,但可以获取值的描述,如“Microsoft.SharePoint.Client.FieldLookupValue”

有人能帮我解决这个问题吗?下面是我正在使用的代码

            using Microsoft.SharePoint.Client;
            using System;
            using System.Security;

            namespace ConsoleTESTES
            {
                class Program
                {
                    static void Main(string[] args)
                    {

                        string username = "USERNAME";
                        string siteURL = "SITEURL";
                        SecureString password = GetPassword();
                        GetAllWebProperties(siteURL, username, password);
                    }

                    public static void GetAllWebProperties(string siteURL, string username, SecureString password)
                    {
                        using (var context = new ClientContext(siteURL))
                        {
                            context.Credentials = new SharePointOnlineCredentials(username, password);
                            Web web = context.Web;
                            context.Load(web);
                            context.ExecuteQuery();
                            Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);

                            // Assume the web has a list named "Announcements".
                            //List lista = context.Web.Lists.GetByTitle("Lista teste");
                            List lista = context.Web.Lists.GetByTitle("LIST");

                            // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
                            // so that it grabs all list items, regardless of the folder they are in.
                            CamlQuery query = CamlQuery.CreateAllItemsQuery();
                            ListItemCollection items = lista.GetItems(query);

                            // Retrieve all items in the ListItemCollection from List.GetItems(Query).
                            context.Load(items);
                            context.ExecuteQuery();

                            //GET VALUES FROM LISTITEM
                            foreach (ListItem listItem in items)
                            {
                                Console.WriteLine(listItem["Setor"] + "   " + "|" + "   "
                                    + listItem["LocalServico"] + "   " + "|" + "   "
                                    + listItem["Equipe"] + "   " + "|" + "   "
                                    + listItem["Confeccao"]);
                            }

                            Console.ReadLine();
                        }
                    }
                    public static SecureString GetPassword()
                    {
                        ConsoleKeyInfo info;
                        SecureString securePassword = new SecureString();
                        do
                        {
                            info = Console.ReadKey();
                            if (info.Key != ConsoleKey.Enter)
                            {
                                securePassword.AppendChar(info.KeyChar);
                            }
                        }

                        while (info.Key != ConsoleKey.Enter);
                        return securePassword;

                    }
                }
            }

您可以从fields集合获取值,但请注意,某些特殊类型的字段可能需要对值进行特殊处理,并且您可能不需要从服务器获取所有值(您可能会减少负载):

有一些默认情况下可能不加载的字段,您可能不需要,因此我添加了
字段stoignore
,以简化测试。

经过一些搜索,我找到了FieldLookUpTable的解决方案,为了避免项为空时出错,我添加了一条if语句,但我可以使用(listItem[“Setor”]访问值作为FieldLookupValue)。LookupValue。这里是我的凌乱代码,用于检查是否为查找值并获取值。现在我需要实现Pedro的解决方案,以获得所有的值,而无需编写每个值

                String setor = "";
                String localServico = "";
                String confeccao = "";

                if (listItem["Setor"] != null && listItem["Setor"].ToString() == "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    setor = (listItem["Setor"] as FieldLookupValue).LookupValue;
                }
                else if (listItem["Setor"] != null && listItem["Setor"].ToString() != "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    setor = listItem["Setor"].ToString();
                }

                if (listItem["LocalServico"] != null && listItem["LocalServico"].ToString() == "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    localServico = (listItem["LocalServico"] as FieldLookupValue).LookupValue;
                }
                else if (listItem["LocalServico"] != null && listItem["LocalServico"].ToString() != "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    localServico = listItem["LocalServico"].ToString();
                }

                if (listItem["Confeccao"] != null && listItem["Confeccao"].ToString() == "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    confeccao = (listItem["Confeccao"] as FieldLookupValue).LookupValue;
                }
                else if (listItem["Confeccao"] != null && listItem["Confeccao"].ToString() != "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    confeccao = listItem["Confeccao"].ToString();
                }


                Console.WriteLine(setor + "   " + "|" + "   "
                    + localServico + "   " + "|" + "   "
                    + confeccao);

感谢Pedro的帮助,我尝试了您的解决方案,但遇到了一个异常:“属性或字段尚未初始化”,但设法避免在最后一个foreach中添加以下代码“context.Load(field);context.ExecuteQuery();”,不幸的是,它似乎只对第一个ListItem有效,我在他之后也遇到了同样的异常。@winderSoares,我认为这可能是因为当您使用“CamlQuery.CreateAllItemsQuery()”时,默认情况下没有加载某些字段,因此您可能只需检查哪些字段失败,然后决定是否要忽略这些字段,或者根据需要创建您自己的CAML。谢谢@Pedro Lorentz!
                String setor = "";
                String localServico = "";
                String confeccao = "";

                if (listItem["Setor"] != null && listItem["Setor"].ToString() == "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    setor = (listItem["Setor"] as FieldLookupValue).LookupValue;
                }
                else if (listItem["Setor"] != null && listItem["Setor"].ToString() != "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    setor = listItem["Setor"].ToString();
                }

                if (listItem["LocalServico"] != null && listItem["LocalServico"].ToString() == "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    localServico = (listItem["LocalServico"] as FieldLookupValue).LookupValue;
                }
                else if (listItem["LocalServico"] != null && listItem["LocalServico"].ToString() != "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    localServico = listItem["LocalServico"].ToString();
                }

                if (listItem["Confeccao"] != null && listItem["Confeccao"].ToString() == "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    confeccao = (listItem["Confeccao"] as FieldLookupValue).LookupValue;
                }
                else if (listItem["Confeccao"] != null && listItem["Confeccao"].ToString() != "Microsoft.SharePoint.Client.FieldLookupValue")
                {
                    confeccao = listItem["Confeccao"].ToString();
                }


                Console.WriteLine(setor + "   " + "|" + "   "
                    + localServico + "   " + "|" + "   "
                    + confeccao);