Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
this looks super promising. unfortunately I won't be able to test this for a little bit. –  MedicineMan Apr 25 '11 at 20:51

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;
}
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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