7

I am using the following code to download documents from SharePointand surface them to the user.

ClientContext clientContext = new ClientContext(ServerUrl);
clientContext.Credentials = cred;
FileInformation fileInformation =
    Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, reference);
return fileInformation.Stream;

My next task is to upload a document to SharePoint, but in the process, set some column values. These values will indicate a filetype, one of several categories that would be added and made available within SharePoint. How does one go about setting this column data and uploading the document?

Responses which help me to ask the right follow up questions will also be extremely helpful-- as I understand that at this point my question may not have enough detail.

3 Answers 3

4

Can you use Microsoft.SharePoint.Client.File instead of just FileInformation? If so try the method below:

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

Stolen from here.

1
  • this looks super promising. unfortunately I won't be able to test this for a little bit. Apr 25, 2011 at 20:51
2

As an addendum to Abe Miessler's answer, I have a slightly modified version. You only need to call _context.ExecuteQuery() once and I define the ListItem fields to return after the item.Update(); _context.ExecuteQuery() statements.

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;
}
0

Thanks for the example. In my case I had the exception

Value does not fall within the expected range

due to a wrong url.

To solve this :

newFile.Url = System.IO.Path.Combine(docs.RootFolder.ServerRelativeUrl, fileName).Replace('\\\','/');

Variables names are same as yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.