1

I created some JavaScript Code whose purpose it is to create a Document Library, enable Content Types in it and add another content type. I added it to my SharePoint 2013 environment using the script editor webpart.

<script type="text/javascript" src="link/to/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
    var docLibCreation;

    function createDocLib() {
        var context = SP.ClientContext.get_current();
        var web=context.get_web();
        var contentTypeCol = web.get_contentTypes();
        var contentType = contentTypeCol.getById('0x0120D520');
        var listCol=web.get_lists();

        docLibCreation = new SP.ListCreationInformation(); 
        var docLibTitel = $('#docLibTitle').val();
        docLibCreation.set_title(docLibTitel);
        docLibCreation.set_templateType(SP.ListTemplateType.documentLibrary);
        var savedList = listCol.add(docLibCreation);
        savedList.set_contentTypesEnabled(true);
        var cts = savedList.get_contentTypes();
        cts.addExistingContentType(contentType);
        context.load(cts);
        context.executeQueryAsync(onDocLibCreationSuccess,onDocLibCreationFail);    
    }   

    function onDocLibCreationSuccess() {
        alert(docLibCreation.title + " created");
    }

    function onDocLibCreationFail(sender, args) {
        alert('Failed to Create the Document Library. Error:' + args.get_message());
    } 
</script>
<label for="docLib">Name of the Document Library: 
    <input type="text" placeholder="Name of the Document Library" id="docLibTitle" />
</label><br/>

<input type="button" onClick="createDocLib()" value="Create Document Library"/><br/>

Problem
The alert message of the success callback function is called but returns "undefined created". Nevertheless the script creates the document library but without having enabled Content Types in it.

What is the reason for that and how can I fix this?


UPDATE:
When I manually allow management of content types in the library settings, the content type I wanted to add (Document Set) is already added! But I don't understand why I have to allow it manually as I use savedList.set_contentTypesEnabled(true).

3

It occurs since SP.List.update Method has to be called in order to set SP.List.contentTypesEnabled Property.

Example

How to enable Allow management of content types in List via JSOM:

var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);

list.set_contentTypesEnabled(true);
list.update();  //update operation is required to apply list changes
context.executeQueryAsync(
  function onDocLibCreationSuccess() {
    console.log(list.get_contentTypesEnabled());
  }, 
  function onDocLibCreationFail(sender, args) {
    console.log('Error:' + args.get_message());
  }
);    
0

Try using get_title() as shown on MSDN:

http://msdn.microsoft.com/en-us/library/office/jj245739(v=office.15).aspx

You may have to refetch the library (through another context.executeQueryAsync) but I would try get_title() first.

EDIT:

Sorry for not answering both questions initially. For the problem adding Content Types, I would suggest refetching the library after it is created and then adding the Content Types in a second call. It has been my experience creating an item then editing in the same call is problematic. This article, which has a great writeup on content types and the SHarePoint JSOM, also mentions this issue in the section titled Challenges when provisioning fields and content types to the host web.

http://www.sharepointnutsandbolts.com/2013/06/sp2013-host-web-apps-provisioning.html

Here is an example of fetching the new list and modifying the Content Types (untested):

<script type="text/javascript">
    var docLibCreation;

    function createDocLib() {
        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var contentTypeCol = web.get_contentTypes();
        var contentType = contentTypeCol.getById('0x0120D520');
        var listCol = web.get_lists();
        docLibCreation = new SP.ListCreationInformation();
        var docLibTitel = $('#docLibTitle').val();
        docLibCreation.set_title(docLibTitel);
        docLibCreation.set_templateType(SP.ListTemplateType.documentLibrary);
        var savedList = listCol.add(docLibCreation);
        context.load(cts);
        context.executeQueryAsync(onDocLibCreationSuccess, onDocLibCreationFail);
    }

    function onDocLibCreationSuccess() {
        alert(docLibCreation.title + " created");
        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var contentTypeCol = web.get_contentTypes();
        var contentType = contentTypeCol.getById('0x0120D520');
        var docLibTitel = $('#docLibTitle').val();
        var targetList = web.get_lists().getByTitle(docLibTitel);
        targetList.set_contentTypesEnabled(true);
        var cts = targetList.get_contentTypes();
        cts.addExistingContentType(contentType);
        context.load(cts);
        context.executeQueryAsync(onDocLibContentTypeSuccess, onDocLibCreationFail);
    }

    function onDocLibCreationFail(sender, args) {
        alert('Failed to Create the Document Library. Error:' + args.get_message());
    }

    function onDocLibContentTypeSuccess() {
        alert('Content Type Added To Library');
    }
</script>
  • Thanks for that. Now the alert message shows the right library title. But there is still the problem with the content type enabling. Do you have an idea why? – pbrz Jul 30 '14 at 14:28
-1

Update list with below code works

list.update(true);

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.