簡體   English   中英

SharePoint更新列表項

[英]SharePoint Update list items

我正在嘗試更新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'的第一個參數

我要使用此代碼有什么想法?

謝謝

您提供的用於更新列表項的代碼適用於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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM