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

This question already has an answer here:

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');

clientContext.load(list);

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

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

clientContext.load(listItem);
clientContext.executeQueryAsync(onItemAdded, onItemAddedFailed);
share|improve this question

marked as duplicate by Rob Windsor, John Chapman, PirateEric Dec 11 '13 at 16:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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); 
fStream.Close();

// 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);

site.Dispose(); 
sp.Dispose();

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);
context.Load(uploadFile);
context.ExecuteQuery();
 Console.WriteLine("done");

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++) {

        fileCreateInfo.get_content().append(fileContent.charCodeAt(i));
    }

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

    clientContext.load(this.newFile);
    clientContext.executeQueryAsync(
        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();
    }
}
share|improve this answer
    
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 '13 at 8:37
    
Please look at this article blogs.msdn.com/b/sridhara/archive/2010/03/12/… –  Andrey Ungur Dec 11 '13 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 '13 at 9:03
    
I update the code with java script solution –  Andrey Ungur Dec 11 '13 at 11:22
    
The SP.FileCreationInformation type wasn't included in the JavaScript Client Object Model in SharePoint 2010 –  Rob Windsor Dec 11 '13 at 14:25

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