updated listitem attributes aren't commiting changes to sharepoint

不羁岁月 提交于 2020-01-13 13:12:12

问题


i'm uploading a document to sharepoint.. however i would like to provide a custom name rather than it inherit the name of the file which im uploading.

my code was based on this solution: http://www.codeproject.com/Articles/103503/How-to-upload-download-a-document-in-SharePoint-20.aspx

however this doesnt work.

Additionally, i would also like to provide a title of the file: so i wanted to update the title:

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

However, once the file has completed its upload..i login to sharepoint and notice the title hasnt been applied.

how can i intergrate uploading the file and applying a new name?

many thanks,

EDIT:

        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);

回答1:


You are missing a call to clientContext.Load after you add the file to the Files collection. See these blog posts for more information:

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/

This code sample is from the first blog post linked above:

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();  
}



回答2:


Are you calling Update after setting the field values?

uploadFile.ListItemAllFields.Update();



回答3:


instead of setting:

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

set it as follows:

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


来源:https://stackoverflow.com/questions/6200654/updated-listitem-attributes-arent-commiting-changes-to-sharepoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!