SharePoint 更新列表项

我正在尝试更新 SharePoint 列表,我在 Internet 上找到了一个代码示例(Microsoft 官方文档)所以这是代码:


using System;

using Microsoft.SharePoint.Client;

using SP = Microsoft.SharePoint.Client;


namespace Microsoft.SDK.SharePointServices.Samples

{

    class UpdateListItem

    {

        static void Main()

        {   

            string siteUrl = "http://MyServer/sites/MySiteCollection";


            ClientContext clientContext = new ClientContext(siteUrl);

            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            ListItem oListItem = oList.Items.GetById(3);


            oListItem["Title"] = "My Updated Title.";


            oListItem.Update();


            clientContext.ExecuteQuery(); 

        }

    }

}

如果我在 visual studio 中复制/粘贴此代码,则此行会出现错误:


ListItem oListItem = oList.Items.GetById(3);

List 不包含 Items 的定义,并且找不到可访问的扩展方法“接受类型为‘List’的第一个参数”


知道我必须做什么才能使用此代码吗?


江户川乱折腾
浏览 151回答 1
1回答

噜噜哒

您提供的用于更新列表项的代码适用于SharePoint 2010. 对于较新的版本尝试ListItem oListItem = oList.GetItemById(3);// Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("http://SiteUrl"); // Assume that the web has a list named "Announcements". List announcementsList = context.Web.Lists.GetByTitle("Announcements"); // Assume there is a list item with ID=1. ListItem listItem = announcementsList.GetItemById(1); // Write a new value to the Body field of the Announcement item.listItem["Body"] = "This is my new value!!"; listItem.Update(); context.ExecuteQuery();
打开App,查看更多内容
随时随地看视频慕课网APP