WebHosting

Thursday, November 17, 2011

Upload file to document library and update the meta data using client context

public ListItem UploadDocument(String fileName, String filePath, Dictionary<string, object> 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);

// Set the file to be uploaded and define the fields to be returned
context.Load(uploadFile,
i => i.ListItemAllFields.Id
i => i.ListItemAllFields.DisplayName,
i => i.ListItemAllFields["Title"],
i => i.ListItemAllFields["FileRef"],
i => i.ListItemAllFields["Created_x0020_By"],
i => i.ListItemAllFields["File_x0020_Size"]);

SPClient.ListItem item = uploadFile.ListItemAllFields;

//Set the metadata
foreach (var meta in metaDataList)
{
item[meta.Key] = meta.Value;
}

item.Update();

context.ExecuteQuery();

return item;
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

How to Enable and Configure Remote Desktop (RDP) on Ubuntu 24.04.4 LTS

Remote Desktop Protocol (RDP) support in Ubuntu has become considerably easier with the GNOME Remote Desktop stack included in modern Ubuntu...