Search code examples
sharepoint-2010sharepointdocumentlibrarysharepoint-clientobject

Sharepoint 2010 - Client Object Model Upload content to document library


I have the following requirement:

I have created a Flash application that is embedded in a Sharepoint Application Page. In the Flash application I have to upload text (I cannot create a file on the client side without prompting the user, so I just have to upload the content in plain text) to a document library of the user's choice.

When the text is uploaded (as a .url file), I have to redirect the browser to the edit form that is associated with the library (or more specific with the content type of the new item).

How can I upload content (plain text) as a new document to a Document library using the Client Object Model?

Kind regards,

Karel


Solution

  • You can upload files to SharePoint using the Client Object Model's FileCreationInformation class which has a Content property that is a byte array.

    You might use it like so:

    ClientContext clientContext = new ClientContext(webUrl);
    Web web = clientContext.Web;
    List documentLibrary = web.Lists.GetByTitle("Documents");
    
    FileCreationInformation newFile = new FileCreationInformation();
    newFile.Content = System.IO.File.ReadAllBytes(localFile);
    newFile.Url = System.IO.Path.GetFileName(localFile);
    
    Microsoft.SharePoint.Client.File uploadFile = documentLibrary.RootFolder.Files.Add(newFile);