使用客户端对象模型从SP列表获取列表项字段值

7

如何使用客户端对象模型(Client Object Model)以最佳方式获取SharePoint列表项及其属性?

以下是我正在使用的代码。

        string server = "http://localhost";
        ClientContext context = new ClientContext(server);
        Web web = context.Web;
        var spList = web.Lists.GetByTitle("Contact");
        CamlQuery query = new CamlQuery();
        var items = spList.GetItems(query);
        context.Load(items, 
            itema => itema.Include(
                item => item,
                item => item["CustomerId"]));
        context.ExecuteQuery();

        Console.WriteLine("Items");
        foreach (var item in items.ToList())
        {                              
               context.Load(item);
        }

        context.ExecuteQuery();
        foreach (var item in items)
        {
             foreach (var a in item.FieldValues)
             {
                 Console.WriteLine(a.Key + ":" + a.Value.ToString());
             }
         }

我希望能够移除在上下文中用于加载列表项的单行foreach,并且如果可能的话,在第一个Execute Query中就加载项目字段值。

我尝试使用以下代码:

 context.Load(items, 
            itema => itema.Include(
                item => item,
                item=> item.FieldValues,
                item => item["CustomerId"]));

这个方案不起作用。

有没有人能提供一个更简单的解决方案?

4个回答

5

查询SharePoint最有效的方法是使用CAML查询。通过调用(SP)List.GetItems(camlQuery),您将始终收到(SP)ListItemCollection的实例。

因此,最有效的查询将如下所示:

string server = "http://localhost";
var ctx = new ClientContext(server);
var web = ctx.Web;
var list = web.Lists.GetByTitle("Contacts");
var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());

// always use QueryTrimming to minimize size of 
// data that has to be transfered

ctx.Load(listItemCollection,
           eachItem => eachItem.Include(
            item => item,
            item => item["CustomerId"]));
// ExecuteQuery will pull all data from SharePoint
// which has been staged to Load()
ctx.ExecuteQuery();

foreach(ListItem listItem in listItemCollection)
{
   Console.WriteLine(listItem["CustomerId"]);
}

Thorsten


0

我不确定您想从这些字段中获取哪些属性,但您可以尝试以下操作:

SPSite oSite = new SPSite("http://localhost");
SPWeb oWeb = oSite.OpenWeb();

SPList oList = oWeb.Lists["LIST NAME"];
SPFieldCollection oFields = oList.Fields;

foreach (SPField oField in oFields)
{
    Console.WriteLine("Property: " + oField.GetProperty("PROPERTY"));
}

OR

你要查找的属性可能实际上在SPField对象下。请查看此处可用的属性和方法:http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield_members(v=office.12).aspx

希望这可以帮助你。如果不行,提供一些关于您想从列表字段中获取的实际属性的更多信息可能有助于提供更准确的解决方案?


当使用客户端对象模型时,我们需要明确地请求我们要查找的属性。在这种特殊情况下,我需要ListItem的FieldValues集合,除非我对每个列表项都执行context.Load,否则该集合不会被填充。 - Sandeep Singh Rawat
你的回答涉及服务器对象模型,而不是问题所询问的客户端对象模型。 - airmanx86
@airmanx86 - 是的,说得好...抱歉我完全误读了这个! - Luke

0

将您的代码更改为以下内容。

IQueryable<ListItem> items = spList.GetItems(query);

然后调用LoadQuery()而不是Load()

context.LoadQuery(items);

您可以添加其他表达式来读取所需的ListItem属性,例如:

context.LoadQuery(items.Include(item => item["CustomerId"]));

我在尝试使用item => item.FieldValues时遇到了问题,但是item => item.FieldValuesAsText却能工作。我不确定为什么会这样 :( 但是只要执行context.LoadQuery(items)就能达到你想要的效果。


0
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">0</Value></Eq></Where></Query><RowLimit>100</RowLimit></View>";
ListItemCollection listItemCollection = srcList.GetItems(camlQuery);
srcContext.Load(listItemCollection);
await srcContext.ExecuteQueryAsync();

查询FSObjType=0将返回所有字段填充的项目!请参见: https://blog.velingeorgiev.com/sharepoint-online-csom-all-list-items-content-caml-query

该查询以100个一组获取所有列表项。您可以添加一个AND子句到查询中以获取特定的项目。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接