SP2013 CSOM C#更新列表

时间:2015-01-15 19:22:26

标签: c# sharepoint sharepoint-2013 csom

ClientContext ctx = new ClientContext(Site);
ctx.Credentials = new NetworkCredential(userName, passWord, "dmz");               
List list = ctx.Web.Lists.GetByTitle(SpList);
ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items); // loading all the fields              
ctx.ExecuteQuery();
foreach (var item in items)
{
    if (((FieldUrlValue)(item["VideoSetExternalLink"])).Url.ToString() != VideoURL)
    {
        ((FieldUrlValue)(item["VideoSetExternalLink"])).Url = vp.VideoURL;
        item.Update(); 
    }
}
ctx.Load(items);
ctx.ExecuteQuery();

工作正常没有错误。但该列表没有得到更新。我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

删除最后一个ctx.Load(items),因为您没有将更新发送到sharepoint而是重新加载项值

ClientContext ctx = new ClientContext(Site);
ctx.Credentials = new NetworkCredential(userName, passWord, "dmz");               
List list = ctx.Web.Lists.GetByTitle(SpList);
ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items); // loading all the fields              
ctx.ExecuteQuery();
foreach (var item in items)
{
    if (((FieldUrlValue)(item["VideoSetExternalLink"])).Url.ToString() != VideoURL)
    {
        ((FieldUrlValue)(item["VideoSetExternalLink"])).Url = vp.VideoURL;
        item.Update(); 
    }
}
ctx.ExecuteQuery();

答案 1 :(得分:0)

ClientContext ctx = new ClientContext(Site);
ctx.Credentials = new NetworkCredential(userName, passWord, "dmz");               
List list = ctx.Web.Lists.GetByTitle(SpList);
ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items); // loading all the fields              
ctx.ExecuteQuery();
foreach (var item in items)
{
    if (((FieldUrlValue)(item["VideoSetExternalLink"])).Url.ToString() != VideoURL)
    {
        FieldUrlValue va = ((FieldUrlValue)(item["VideoSetExternalLink"]));
        va.Url = vp.VideoURL;
        item["VideoSetExternalLink"] = va;
        item.Update(); 
    }
}
ctx.Load(items);
ctx.ExecuteQuery();

This is how i Fixed the issue.