迭代sharepoint 2013中的列表

时间:2014-07-04 11:36:16

标签: c# sharepoint-2013

我不熟悉sharepoint,并且我被要求执行以下任务。我可以写c#代码了。这个问题是否意味着我需要创建一个名为Employee Cars的类,其中包含以下属性:Make,Model,Registration,Production of Productions并创建该对象的列表并遍历我的列表?如果是这种情况我知道如何做到这一点,但我不确定它在sharepoint中是如何工作的。您能否请您发送一些有关sharepoint的参考资料,以便我熟悉如何在sharepoint上执行此任务。 这是一个问题: SharePoint 2013自定义列表Employee Cars包含公司每位员工的条目。使用SharePoint客户端对象模型(CSOM),编写程序以遍历列表中的每辆汽车,其中生产年份大于2005.该列表包含以下字段:Make(String),Model(String),Registration(字符串)和生产年份(Int)。使用提供的表格来显示您的答案。

using Microsoft.SharePoint.Client;
using System;
using System.Text;


namespace EmployeeCarsApplication
{
    class Program
    {
        static void Main(string[] args)
        {
         ClientContext spContext = new ClientContext("http://ExampleSharePointURL");

            Console.ReadLine();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

代码示例:

using (var ctx = new ClientContext("<site url>"))
{

    var list = ctx.Web.Lists.GetByTitle("Employee Cars"); //get List by its title
    var qry = new CamlQuery { ViewXml = "<View><Query><Where><Gt><FieldRef Name='Year_x0020_of_x0020_Production' /><Value Type='Integer'>2005</Value></Gt></Where></Query></View>" };  //construct the query: [Production Year] > 2005, Year_x0020_of_x0020_Production is the internal name for a field 
    var items = list.GetItems(qry);  //get items using the specified query
    ctx.Load(items); // tell SharePoint to return list items  
    ctx.ExecuteQuery(); //submit query to the server 

    //print results
    foreach (var item in items)
    {
       Console.WriteLine(item.FieldValues["Model"]);
    }
 }

要熟悉SharePoint CSOM API,请按照文章How to: Complete basic operations using SharePoint 2013 client library code

进行操作