I am trying to automate the creation of a document library.
Therefore I have a button which after clicking on it creates a new document library with at least one managed metadata column. I already created a script which works until I want to add a document to the library and set the metadata, because then the metadata field is greyed out. I have to manually choose the term set in the library settings. Then it works, but I want it already chosen/set with this script.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function createDocLib() {
var context = SP.ClientContext.get_current();
var web=context.get_web();
var list=web.get_lists();
var docLibCreation;
docLibCreation = new SP.ListCreationInformation();
var docLibTitel = $('#docLibTitle').val();
docLibCreation.set_title(docLibTitel);
docLibCreation.set_templateType(SP.ListTemplateType.documentLibrary);
var savedList = list.add(docLibCreation);
var newCols = [
"<Field Type='TaxonomyFieldType' DisplayName='Location' Name='Location'
StaticName='Location' ShowField='Term1033' Open='false' Group='People'
TermSetId='b49f64b3-4722-4336-9a5c-56c326b344d4'
TextField='155963e0-be13-4b91-bb2b-df69fdcbbeec'
SspId='0d9b6833f2694bb4a3bbe47fb4f6b770' />",
"<Field Type='Note' DisplayName='Location_0'
ID='{155963e0-be13-4b91-bb2b-df69fdcbbeec}' ShowInViewForms='FALSE'
Required='FALSE' Hidden='TRUE' CanToggleHidden='TRUE' Group='People'/>"
];
var numberCols = newCols.length;
for (var i=0; i<numberCols;i++){
this.newColumns = savedList.get_fields().addFieldAsXml(newCols[i], true, SP.AddFieldOptions.defaultValue);
}
context.load(this.newColumns);
context.executeQueryAsync(onDocLibCreationSuccess,onDocLibCreationFail);
}
function onDocLibCreationSuccess() {
alert("Document Library 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/>
<button onclick="createDocLib()">Create Document Library</button><br/>
Why isn't the metadata termset automatically set and choosable?