簡體   English   中英

更新的 listitem 屬性未提交對 sharepoint 的更改

[英]updated listitem attributes aren't commiting changes to sharepoint

我正在將文檔上傳到 sharepoint .. 但是我想提供一個自定義名稱,而不是繼承我上傳的文件的名稱。

我的代碼基於此解決方案: http://www.codeproject.com/Articles/103503/How-to-upload-download-a-document-in-SharePoint-20.aspx

但是這不起作用。

此外,我還想提供文件的標題:所以我想更新標題:

uploadFile.ListItemAllFields.FieldValues["Title"] = "my custom title";

但是,一旦文件完成上傳..我登錄到 sharepoint 並注意到標題尚未應用。

我如何集成上傳文件和應用新名稱?

非常感謝,

編輯:

        using (var clientContext = GetNewContext())
        {
            var uploadLocation = string.Format("{0}{1}/{2}", SiteUrl, Helpers.ListNames.RequestedDocuments, Path.GetFileName(document));

            //Get Document List
            var documentslist = clientContext.Web.Lists.GetByTitle(Helpers.ListNames.RequestedDocuments);

            var fileCreationInformation = new FileCreationInformation
            {
                Content = System.IO.File.ReadAllBytes(document), //Assign to content byte[] i.e. documentStream
                Overwrite = true, //Allow owerwrite of document
                Url = uploadLocation //Upload URL,

            };

            var uploadFile = documentslist.RootFolder.Files.Add(fileCreationInformation);

            uploadFile.ListItemAllFields.FieldValues["Title"] = title;

            uploadFile.ListItemAllFields.Update();

            clientContext.ExecuteQuery();           
        }
        site.SubmitChanges(ConflictMode.FailOnFirstConflict, true);

將文件添加到 Files 集合后,您缺少對 clientContext.Load 的調用。 有關更多信息,請參閱這些博客文章:

https://www.c-sharpcorner.com/code/965/programmatically-upload-document-using-client-object-model-in-sharepoint.aspx

https://zimmergren.net/sp-2010-uploading-files-using-the-client-om-in-sharepoint-2010/

此代碼示例來自上面鏈接的第一篇博客文章:

public Boolean UploadDocument(String fileName, String filePath, List metaDataList)   
{  
    SP.ClientContext ctx = new SP.ClientContext("http: //yoursharepointURL");  
    Web web = ctx.Web;  
    FileCreationInformation newFile = new FileCreationInformation();  
    newFile.Content = System.IO.File.ReadAllBytes(@"C: \TestFile.doc");  
    newFile.Url = " / " + fileName;  
    List docs = web.Lists.GetByTitle("Shared Documents");  
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);  
    context.Load(uploadFile);  
    context.ExecuteQuery();  
    SPClient.ListItem item = uploadFile.ListItemAllFields;  
    //Set the metadata  
    string docTitle = string.Empty;  
    item["Title"] = docTitle;  
    item.Update();  
    context.ExecuteQuery();  
}

設置字段值后是否調用更新?

uploadFile.ListItemAllFields.Update();

而不是設置:

uploadFile.ListItemAllFields.FieldValues["Title"] = title;
uploadFile.ListItemAllFields.Update();

設置如下:

uploadFile.ListItemAllFields["Title"] = title;
uploadFile.ListItemAllFields.Update();

暫無
暫無

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

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