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

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

function newPBDLSuccess() {
function newPBFail() {

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?

  • 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

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.

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.