如何从Microsoft.SharePoint.Client.ListItem检索所有属性?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从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}
Title: {1}
Body: {2}", oListItem.Id, oListItem["Title"], oListItem["Body"]);
}
}
}
}
这假设您非常清楚标题和正文在SharePoint列表对象的oListItem中可用。但是,任何简单的方法来检索所有可用的属性就像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();
}
}
}
}
以上是关于如何从Microsoft.SharePoint.Client.ListItem检索所有属性?的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据从回收器适配器发送到片段 |如何从 recyclerview 适配器调用片段函数
如何从服务器获取和设置 android 中的 API(从服务器获取 int 值)?如何绑定和实现这个