簡體   English   中英

使用客戶端對象模型在Sharepoint C#中創建ListItem

[英]Create ListItem in Sharepoint C# Using Client-Object Model

我是這個主題的新手,但在搜索之前,我搜索了很多東西,意思是真的很多。 所以我的問題是,當我在應用程序中創建ListItem並設置所需的字段時,一切看起來都很好。 如果我轉到Sharepoint Online,則可以在列表中看到帶有正確字段的Lis​​tItem。 但是,當我單擊ListItem時,字段為空,並且在頁面頂部是一個錯誤,提示“對象引用未設置為對象的實例”。 我不知道問題是否在代碼的cos中發生,或者Sharepoint設置有問題嗎? 我按照這里的大多數指南走了直到現在。 我的代碼如下。

        SP.List targetList = ctx.Web.Lists.GetByTitle("Documents");

        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
        ListItem newListItem = targetList.AddItem(itemCreateInfo);

        newListItem["Title"] = "Test_title";
        newListItem["EAN_x0020_k_x00f3_d"] = "AnotherCodeWhichIsRequired";
        newListItem.Update();

        string strFilePath = @"PathToMyPDF";

        byte[] bytes = System.IO.File.ReadAllBytes(strFilePath);

        using (System.IO.MemoryStream mStream = new System.IO.MemoryStream(bytes))
        {
            SP.AttachmentCreationInformation aci = new SP.AttachmentCreationInformation();
            aci.ContentStream = mStream;
            aci.FileName = System.IO.Path.GetFileNameWithoutExtension(strFilePath);

            newListItem.AttachmentFiles.Add(aci);

            newListItem.Update();
            ctx.ExecuteQuery();
        }

似乎您正在使用文檔庫。您使用的代碼用於共享點列表。

要將文件上傳到文檔庫並更新其屬性,請如下修改代碼:

string strFilePath = @"PathToMyPDF";
FileCreationInformation newFile = new FileCreationInformation();  
newFile.Content = System.IO.File.ReadAllBytes(strFilePath);  
newFile.Overwrite = true;
SP.List targetList = ctx.Web.Lists.GetByTitle("Documents");  
Microsoft.SharePoint.Client.File uploadFile = targetList.RootFolder.Files.Add(newFile);  
ctx.Load(uploadFile);  
ctx.ExecuteQuery();  

uploadFile.ListItemAllFields["Title"] = "Test_title";
uploadFile.ListItemAllFields["EAN_x0020_k_x00f3_d"] = "AnotherCodeWhichIsRequired";
uploadFile.ListItemAllFields.Update();

ctx.ExecuteQuery();  

暫無
暫無

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

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