[英]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.