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

时间:2018-03-01 09:18:16

标签: c# sharepoint

请考虑以下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"]);
            }
        }
    }
}

这假设您非常清楚SharePoint列表对象中的oListItem中可以使用Title和Body。但是,任何简单的方法来检索所有可用的属性就像javascript中的JSON.stringify()?感谢。

此外,我尝试过:

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

但是没有任何印刷品。

1 个答案:

答案 0 :(得分:4)

检查ListItem.FieldValuesAsText属性。

试试这个:

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

            }
        }
    }
}