简体   繁体   English

如何从Microsoft.SharePoint.Client.ListItem检索所有属性?

[英]How to retrieve all properties from Microsoft.SharePoint.Client.ListItem?

Consider the following sample code from Microsoft: 考虑以下来自Microsoft的示例代码:

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveListItems
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
                "<Value Type='Number'>10</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";
            ListItemCollection collListItem = oList.GetItems(camlQuery);

            clientContext.Load(collListItem);

            clientContext.ExecuteQuery();

            foreach (ListItem oListItem in collListItem)
            {
                Console.WriteLine("ID: {0} \nTitle: {1} \nBody: {2}", oListItem.Id, oListItem["Title"], oListItem["Body"]);
            }
        }
    }
}

This assume you know very well that Title and Body are available in oListItem from SharePoint List Objects. 假设您非常了解SharePoint列表对象的oListItem中的Title和Body。 However, any easy way to retrieve all available properties just like JSON.stringify() in javascript? 但是,有什么简单的方法可以检索所有可用属性,例如javascript中的JSON.stringify()? thanks. 谢谢。

Moreover, I have tried: 此外,我尝试了:

foreach (ListItem oListItem in collListItem)
{
    foreach (KeyValuePair<String,String> kv in oListItem.FieldValuesAsText.FieldValues)
    {
        var value = kv.Key;
        Console.WriteLine("key=" + kv.Key + " value=" + kv.Value);
    }
}

But there is nothing printed. 但是没有印刷任何东西。

Check ListItem.FieldValuesAsText property. 检查ListItem.FieldValuesAsText属性。

Try this: 尝试这个:

namespace ConsoleApp
{
    public static class Extensions
    {
        public static string FromDictionaryToJson(this Dictionary<string, string> dictionary)
        {
            var kvs = dictionary.Select(kvp => string.Format("\"{0}\":\"{1}\"", kvp.Key, string.Concat(",", kvp.Value)));
            return string.Concat("{", string.Join(",", kvs), "}");
        }

        public static Dictionary<string, string> FromJsonToDictionary(this string json)
        {
            string[] keyValueArray = json.Replace("{", string.Empty).Replace("}", string.Empty).Replace("\"", string.Empty).Split(',');
            return keyValueArray.ToDictionary(item => item.Split(':')[0], item => item.Split(':')[1]);
        }
    }
    class Program
    {   
        static void Main(string[] args)
        {

            using (var clientContext = new ClientContext("http://sp:12001/"))
            {
                #region MyRegion
                List list = clientContext.Web.Lists.GetByTitle("Users");                
                CamlQuery query = new CamlQuery();
                query.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
                 "<Value Type='Number'>1</Value></Geq></Where></Query><RowLimit>100</RowLimit><ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/></ViewFields></View>";

                var items = list.GetItems(query);

                clientContext.Load(items, eachItem => eachItem.Include(
                item => item.FieldValuesAsText));               
                clientContext.ExecuteQuery();
                foreach (ListItem oListItem in items)
                {
                    var values = oListItem.FieldValuesAsText.FieldValues as Dictionary<string, string>;
                    Console.WriteLine(Extensions.FromDictionaryToJson(values));
                }
                Console.WriteLine(items.Count);
                #endregion
                Console.ReadKey();

            }
        }
    }
}

暂无
暂无

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

相关问题 如何将 Microsoft.Sharepoint.Client.ListItem 转换为 Microsoft.Sharepoint.Client.File? - How to convert from Microsoft.Sharepoint.Client.ListItem to Microsoft.Sharepoint.Client.File? 如何序列化Microsoft.SharePoint.Client.ListItem(Sharepoint-2013)? - How to Serialize Microsoft.SharePoint.Client.ListItem (Sharepoint-2013)? 如何从类Microsoft.SharePoint.Client.ListItem中获取字段类型(整数,日期时间,选择)? - how do you get the field type (integer, datetime, choice) from the class Microsoft.SharePoint.Client.ListItem? C# - 将文件作为附件添加到 Microsoft.SharePoint.Client.ListItem (SharePoint 2010) - C# - Add a File as Attachment to Microsoft.SharePoint.Client.ListItem (SharePoint 2010) 如何从 Microsoft.Sharepoint.Client.ZC6E190B28404933C48EZCE895E 中填充 Web class - How to shim the Web class from Microsoft.Sharepoint.Client.Web? 访问SharePoint 2010 ListItem中的属性以获取版本历史记录 - Accessing properties in a SharePoint 2010 ListItem for version history Microsoft Graph REST API v1.0 获取列表项(10.000.000 列表项来自 SharePoint 列表) - Microsoft Graph REST API v1.0 Get listItem(10.000.000 List Item from a SharePoint list) 如何使用 Microsoft Graph 获取 DriveItem 上的 Sharepoint ListItem - How to get Sharepoint ListItem on DriveItem with Microsoft Graph API c# SDK 来自 c# 的 Sharepoint 更改 listitem 字段 - Sharepoint change listitem field from c# 如何从数据库中检索搜索相关数据到listitem? - How to retrieve the search related data from the database to a listitem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM