Hi I'm trying to create a number of items in a document library.I've added the following in a CEWP but it errors:

function createListItem() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('IArequest');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', 'My New Item!');
   // oListItem.set_item('Body', 'Hello World!');

        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)

function onQuerySucceeded() {
    alert('Item created: ' + oListItem.get_id());

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());

Error in alert box: Request failed. To add an item to a document library, use SPFileCollection.Add()

I have successfully added items using SPD workflow (create item Action) is this not the same?


Unlike Custom List, Document Library requires a File to be uploaded. This File will have a list item and you can set the metadata properties like Title.

First you need to get the file that you want to upload. For that you can use

<input id="inputFile" type="file" />
<input id="uploadDocumentButton" type="Button" value="Upload Document"/>

Now using JavaScript you can read the file content

var parts = document.getElementById("inputFile").value.split("\\");
var filename = parts[parts.length - 1];
var file = document.getElementById("inputFile").files[0];

var getFileBuffer = function (file) {
    var deferred = $.Deferred();
    var reader = new FileReader();
    reader.onload = function (e) {
    reader.onerror = function (e) {
    return deferred.promise();

Now you can upload this to a library

var ctx = new SP.ClientContext.get_current();
var createInfo = new SP.FileCreationInformation();
createInfo.set_content(content); //setting the content of the new file
this.files = ctx.get_web().getFolderByServerRelativeUrl(serverRelativeUrl).get_files();
        function () { alert('Success'); }),
        function (sender, args) { alert('Failed'); }));

Source - http://www.shillier.com/archive/2013/03/26/uploading-files-in-sharepoint-2013-using-csom-and-rest.aspx

If you prefer REST, take a look at this article http://sharepointfieldnotes.blogspot.com/2014/04/uploading-documents-and-setting.html

  • Hi Amal - In the doc library I've added a template and then when I add an item using 'Create item' (SPD action) I am not making any reference the the template, but jut creating what seems to be list atributes. After running wf this creates a document based on the template and adds columns. Is there another way thats being missed using the coding approach ? – Orange Juice Jones Apr 10 '15 at 13:07
  • I suspect it is not tat easy - social.msdn.microsoft.com/Forums/office/en-US/… – Amal Hashim Apr 10 '15 at 13:28
  • I dont need the input box as in the code. I need to create x documents with different file names and metadata from a single template in the same web. Can this code be modified to do this? Maybe a copy method ? – Orange Juice Jones Apr 10 '15 at 13:45
  • The code assumes you already have a file ready for upload. In your case, first you need to create document based on the template. And then use the code to upload it into the library. Lets wait if someone else has a better idea on this topic. – Amal Hashim Apr 10 '15 at 13:48
  • Okay sounds good. Just to be clear, the template is already created and doesnt change and can be stored in a library. Then when the user presses a 'Go' button the documents need to be created or copied (Whichever is easier) with new metadata. – Orange Juice Jones Apr 10 '15 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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