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)
.