简体   繁体   English

迭代sharepoint 2013中的列表

[英]Iterate through a list in sharepoint 2013

I'm not familiar with sharepoint and I've been asked to do the following task. 我不熟悉sharepoint,我被要求做以下任务。 Im okay with writing c# code though. 我可以写c#代码了。 Does this question mean I need to create a class called Employee Cars, with these properties: Make, Model, Registration, Year of Production and create a list of that object and iterate through my list? 这个问题是否意味着我需要创建一个名为Employee Cars的类,其中包含以下属性:Make,Model,Registration,Production of Productions并创建该对象的列表并遍历我的列表? If that's the case I know how to do that but I'm not sure how this works in sharepoint. 如果是这种情况我知道如何做到这一点,但我不确定它在sharepoint中是如何工作的。 Could you please also send me some references about sharepoint so I get familiar with how to do this task on sharepoint. 您能否请您发送一些有关sharepoint的参考资料,以便我熟悉如何在sharepoint上执行此任务。 Here is the question: A SharePoint 2013 Custom List, Employee Cars, contains entries for each employee of the company. 以下是问题:SharePoint 2013自定义列表Employee Cars包含公司每位员工的条目。 Using the SharePoint Client Object Model (CSOM), Write a program to iterate through each car in the list, where the production year is greater than 2005. The List contains the following fields: Make (String), Model (String), Registration (String) and Year of Production (Int). 使用SharePoint客户端对象模型(CSOM),编写程序以遍历列表中的每辆汽车,其中生产年份大于2005.该列表包含以下字段:Make(String),Model(String),Registration(字符串)和生产年份(Int)。 Use the sheet provided to show your answer. 使用提供的表格来显示您的答案。

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();
        }
    }
}

Code sample: 代码示例:

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"]);
    }
 }

To get acquainted with SharePoint CSOM API please follow the article How to: Complete basic operations using SharePoint 2013 client library code 若要熟悉SharePoint CSOM API,请按照文章如何:使用SharePoint 2013客户端库代码完成基本操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM