Is it possible to add a new item in a SharePoint 2010 document library using JavaScript (client object model)? I have successfully created list items in other type of lists, but when it comes to uploading a document I'm out of luck. Maybe this is a feature only supported in SharePoint 2013?

Edit: This is a working example how I add an item to a custom list. I'd like to do something similar using a document library.

var clientContext = new SP.ClientContexturl);
var list = clientContext.get_web().get_lists().getByTitle('ProjectData');


var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);

listItem.set_item('Title', 'My project title!');

clientContext.executeQueryAsync(onItemAdded, onItemAddedFailed);

1 Answer 1


You can do this using this code:

// Getting a reference to the document library 
var sp = new SPSite("http://localhost"); 
var site = sp.OpenWeb(); 
var folder = site.GetFolder("Documents"); 
var files = folder.Files;

// Opening a filestream 
var fStream = File.OpenRead("C:MyDocument.docx"); 
var contents = new byte[fStream.Length]; 
fStream.Read(contents, 0, (int)fStream.Length); 

// Adding any metadata needed 
var documentMetadata = new Hashtable {{"Comments", "Hello World"}};

// Adding the file to the SPFileCollection 
var currentFile = 
    files.Add("Documents/MyDocument.docx", contents, documentMetadata, true);


link to source Using client object:

ClientContext context = new ClientContext("http://spdevinwin");
Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(@"C:\Work\Files\17580_FAST2010_S05_Administration.pptx");
newFile.Url = "17580_FAST2010_S05_Administration 4MB file uploaded via client OM.pptx";
List docs = web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);

link to source

Using Java Script:

function createFile(resultpanel) {
    var clientContext;
    var oWebsite;
    var oList;
    var fileCreateInfo;
    var fileContent;

    clientContext = new SP.ClientContext.get_current();
    oWebsite = clientContext.get_web();
    oList = oWebsite.get_lists().getByTitle("Shared Documents");

    fileCreateInfo = new SP.FileCreationInformation();
    fileCreateInfo.set_url("my new file.txt");
    fileCreateInfo.set_content(new SP.Base64EncodedByteArray());
    fileContent = "The content of my new file";

    for (var i = 0; i < fileContent.length; i++) {


    this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);

        Function.createDelegate(this, successHandler),
        Function.createDelegate(this, errorHandler)

    function successHandler() {
        resultpanel.innerHTML =
            "Go to the " +
            "<a href='../Lists/Shared Documents'>document library</a> " +
            "to see your new file.";

    function errorHandler() {
        resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();
  • Thanks for reply. However, I assume this is server based code. I don't think using a file stream is possible. My goal is to upload a file using JavaScript.
    – kolback
    Dec 11, 2013 at 8:37
  • Please look at this article blogs.msdn.com/b/sridhara/archive/2010/03/12/… Dec 11, 2013 at 8:42
  • Sorry, I've tried this specific code already but it doesn't work. I cannot even create a FileCreationInformation object. This code will be put in a Content Editor webpart if that might be the problem?
    – kolback
    Dec 11, 2013 at 9:03
  • I update the code with java script solution Dec 11, 2013 at 11:22
  • 1
    The SP.FileCreationInformation type wasn't included in the JavaScript Client Object Model in SharePoint 2010 Dec 11, 2013 at 14:25

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