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 need to create a Document Library and assign it columns with Javascript.

I've been searching around and I found the SP.ListCreationInformation documentation. Here is my code so far:

function createDocLibrary() {
    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();

    var lci = new SP.ListCreationInformation ();
    lci.set_title('My Custom Title');
    lci.set_templateType(101); 

    this.oList = web.get_lists().add(lci);
    ctx.load(oList);
    ctx.executeQueryAsync(Function.createDelegate(this, this.newPBDLSuccess), Function.createDelegate(this, this.newPBDLFail));

}
function newPBDLSuccess() {
    alert('success');    
}
function newPBFail() {
    alert('fail');
}

I can create the document library (thanks @Dylan Cristy for catching my mistake - I updated the code on this post), but am stuck on how to create the libraries columns with javascript.

Also, I'm using ExecuteOrDelayUntilScriptLoaded(createDocLibrary, "sp.js"); to ensure sp.js is loaded first.

Does anyone have a working example of creating a Document Library (or list) and setting its columns with javascript?

share|improve this question
    
Looks like you want to use ListCreationInformation (which you mention above your code block, and link to), but in your code you apparently are using ListItemCreationInformation. Reference here: msdn.microsoft.com/en-us/library/… –  Dylan Cristy Jul 8 '13 at 18:00
add comment

1 Answer

up vote 0 down vote accepted

Without being able to provide you with specific code examples, I'd say that now that you've created your Document Library, and have a reference to it as this.oList, you can use the SP.List.fields property to get the field collection for the list, and use the SP.FieldCollection.add() method to add fields to the list. Since add takes an SP.Field object as an argument, you may need to get your reference to the field object by pulling it out of the web's field collection, using SP.FieldCollection.getByInternalNameOrTitle() method.

share|improve this answer
add comment

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.