SharePoint Foundation 2010 托管客户端对象模型概述(七) --更新客户端对象\删除客户对象

更新客户端对象

使用客户端对象模型更新客户端对象非常简单。只需检索对象、更改属性,对要更改的每个对象调用 Update 方法,然后调用 ExecuteQuery 方法即可。以下示例将修改 客户端 API 测试列表中的项,将所有开发项的估计值提高 50%(一种常见操作)。



C#

using System;

using Microsoft.SharePoint.Client;



class Program

{

    static void Main(string[] args)

    {

        ClientContext clientContext = new ClientContext("http://intranet.contoso.com");

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

        CamlQuery camlQuery = new CamlQuery();

        camlQuery.ViewXml =

            @"<View>

                <Query>

                  <Where>

                    <Eq>

                      <FieldRef Name='Category'/>

                      <Value Type='Text'>Development</Value>

                    </Eq>

                  </Where>

                </Query>

                <RowLimit>100</RowLimit>

              </View>";

        ListItemCollection listItems = list.GetItems(camlQuery);

        clientContext.Load(

             listItems,

             items => items.Include(

                 item => item["Category"],

                 item => item["Estimate"]));

        clientContext.ExecuteQuery();

        foreach (ListItem listItem in listItems)

        {

            listItem["Estimate"] = (double)listItem["Estimate"] * 1.5;

            listItem.Update();

        }

        clientContext.ExecuteQuery();

    }

}

删除客户端对象

删除客户端对象也非常简单。但是,从客户端对象集合中删除客户端对象时有一个非常重要的注意事项。您不能通过循环访问集合来删除对象。删除第一个对象后,客户端对象集合的迭代器随即失效。迭代器可能会引发异常,或者安静地完成,而不访问集合中的所有项目。您必须使用 ToList 方法将集合具体化到 List<T> 中,然后循环访问该列表以删除客户端对象。



以下示例将从 客户端 API 测试列表中删除测试项。它演示了在循环访问集合之前如何使用 ToList 方法具体化该集合:



C#

using System;

using System.Linq;

using System.Collections.Generic;

using Microsoft.SharePoint.Client;



class Program

{

    static void Main(string[] args)

    {

        ClientContext clientContext = new ClientContext("http://intranet.contoso.com");

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

        CamlQuery camlQuery = new CamlQuery();

        camlQuery.ViewXml =

            @"<View>

                <Query>

                  <Where>

                    <Eq>

                      <FieldRef Name='Category'/>

                      <Value Type='Text'>Test</Value>

                    </Eq>

                  </Where>

                </Query>

                <RowLimit>100</RowLimit>

              </View>";

        ListItemCollection listItems = list.GetItems(camlQuery);

        clientContext.Load(

             listItems,

             items => items.Include(

                 item => item["Title"]));

        clientContext.ExecuteQuery();

        foreach (ListItem listItem in listItems.ToList())

            listItem.DeleteObject();

        clientContext.ExecuteQuery();

    }

}



以下代码示例演示了错误的方法。



C#

clientContext.Load(

    listItems,

    items => items.Include(

        item => item["Title"]));

clientContext.ExecuteQuery();



// The ToList() method call is removed in the following line.

foreach (ListItem listItem in listItems)  

    listItem.DeleteObject();



clientContext.ExecuteQuery();



最后,为了清理客户端 API 测试列表,在下面提供了一个示例,以删除列表和列表项。



C#

using System;

using Microsoft.SharePoint.Client;



class DisplayWebTitle

{

    static void Main()

    {

        ClientContext clientContext =

            new ClientContext("http://intranet.contoso.com");

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

            .DeleteObject();

        clientContext.ExecuteQuery();

    }

}

你可能感兴趣的:(SharePoint)