簡體   English   中英

如何從Microsoft.SharePoint.Client.ListItem檢索所有屬性?

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

考慮以下來自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);
    }
}

但是沒有印刷任何東西。

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

            }
        }
    }
}

暫無
暫無

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

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