Sharepoint Client Object Model 之 开发基于.NET的应用程序

前提:引用using Microsoft.SharePoint.Client;和using Microsoft.SharePoint.Client.Runtime两个类库

1、客户端Create a List

            ListCreationInformation listCreationInfo =
                new ListCreationInformation();
            listCreationInfo.Title = "Client API Test List";
            listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;
            List list = clientContext.Web.Lists.Add(listCreationInfo);
            // Add fields to the list.
            Field field1 = list.Fields.AddFieldAsXml(
                @"<Field Type='Choice'
                     DisplayName='Category'
                     Format='Dropdown'>
                <Default>Specification</Default>
                <CHOICES>
                  <CHOICE>Specification</CHOICE>
                  <CHOICE>Development</CHOICE>
                  <CHOICE>Test</CHOICE>
                  <CHOICE>Documentation</CHOICE>
                </CHOICES>
              </Field>",
                true, AddFieldOptions.DefaultValue);
            Field field2 = list.Fields.AddFieldAsXml(
                @"<Field Type='Number'
                     DisplayName='Estimate'/>",
                true, AddFieldOptions.DefaultValue);
clientContext.ExecuteQuery();

2、客户端Add ListItem

            // Add some data.
            ListItemCreationInformation itemCreateInfo =
                new ListItemCreationInformation();
            ListItem listItem = list.AddItem(itemCreateInfo);
            listItem["Title"] = "Write specs for user interface.";
            listItem["Category"] = "Specification";
            listItem["Estimate"] = "20";
            listItem.Update();
            listItem = list.AddItem(itemCreateInfo);
            listItem["Title"] = "Develop proof-of-concept.";
            listItem["Category"] = "Development";
            listItem["Estimate"] = "42";
            listItem.Update();

            listItem = list.AddItem(itemCreateInfo);
            listItem["Title"] = "Write test plan for user interface.";
            listItem["Category"] = "Test";
            listItem["Estimate"] = "16";
            listItem.Update();
            listItem = list.AddItem(itemCreateInfo);
            listItem["Title"] = "Validate SharePoint interaction.";
            listItem["Category"] = "Test";
            listItem["Estimate"] = "18";
            listItem.Update();
            listItem = list.AddItem(itemCreateInfo);
            listItem["Title"] = "Develop user interface.";
            listItem["Category"] = "Development";
            listItem["Estimate"] = "18";
            listItem.Update();
            clientContext.ExecuteQuery();

 3、访问大型列表使用 ListItemCollectionPosition 属性进行分页的方法。

using System;
using System.Linq;
using Microsoft.SharePoint.Client;

class Program
{
    static void Main()
    {
        ClientContext clientContext =
            new ClientContext("http://intranet.contoso.com");

        List list = clientContext.Web.Lists
            .GetByTitle("Client API Test List");

        // First, add 20 items to Client API Test List so that there are
        // enough records to show paging.
        ListItemCreationInformation itemCreateInfo =
            new ListItemCreationInformation();
        for (int i = 0; i < 20; i++)
        {
            ListItem listItem = list.AddItem(itemCreateInfo);
            listItem["Title"] = String.Format("New Item #{0}", i);
            listItem["Category"] = "Development";
            listItem["Estimate"] = i;
            listItem.Update();
        }
        clientContext.ExecuteQuery();

        // This example shows paging through the list ten items at a time.
        // In a real-world scenario, you would want to limit a page to
        // 2000 items.
        ListItemCollectionPosition itemPosition = null;
        while (true)
        {
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ListItemCollectionPosition = itemPosition;
            camlQuery.ViewXml =
                @"<View>
                    <ViewFields>
                      <FieldRef Name='Title'/>
                      <FieldRef Name='Category'/>
                      <FieldRef Name='Estimate'/>
                    </ViewFields>
                    <RowLimit>10</RowLimit>
                  </View>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            itemPosition = listItems.ListItemCollectionPosition;
            foreach (ListItem listItem in listItems)
                Console.WriteLine("  Item Title: {0}", listItem["Title"]);
            if (itemPosition == null)
                break;
            Console.WriteLine(itemPosition.PagingInfo);
            Console.WriteLine();
        }
    }
}


 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值