
Customizing SharePoint
❘
23
FileCreationInformation object and add that object into a document library. This returns
a SharePoint Client OM File object. With that file object you call the
Load method on the
ClientContext that causes the file to upload. After that you call the ExecuteQuery method on
the
ClientContext. Finally, you can get the ListItem that the file is associated with by access-
ing the
ListItemAllFields method on the SharePoint File object that you just loaded. At this
point, you have a list item, and you can modify the metadata properties and update the list item.
The code for this would look like:
ClientContext context = new ClientContext(“http://localhost/”);
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”);
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();
This approach is relatively straightforward and makes it simple to set the document metadata. It is
also good for creating folders in libraries. This approach does, however, come with a major issue.
Using the
FileCreationInformation ...