2

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?

1

Use the following schema syntax for provisioning a managed metadata field:

<Field Type="TaxonomyFieldType" DisplayName="Location"  ShowField="Term1033" Required="FALSE" EnforceUniqueValues="FALSE"  StaticName="Location" Name="Location" >                           
  <Default />                  
  <Customization>                     
     <ArrayOfProperty>
         <Property><Name>SspId</Name><Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p4:type="q1:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">b5b6effd-4925-43e4-ad43-a257213dedec</Value></Property>                       
         <Property><Name>TermSetId</Name><Value xmlns:q2="http://www.w3.org/2001/XMLSchema" p4:type="q2:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">b49f64b3-4722-4336-9a5c-56c326b344d4</Value></Property>                     
         <Property><Name>TextField</Name><Value xmlns:q6="http://www.w3.org/2001/XMLSchema" p4:type="q6:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{155963e0-be13-5b99-bb2b-df69fdcbbeec}</Value></Property>                                            
         <!-- ... -->
     </ArrayOfProperty>
  </Customization>               
</Field>

How to provision a managed metadata field via JSOM

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

var taxField =  
        '<Field Type="TaxonomyFieldType" DisplayName="Location"  ShowField="Term1033" Required="FALSE" EnforceUniqueValues="FALSE"  StaticName="Location" Name="Location" > \
             <Default /> \
             <Customization> \
                <ArrayOfProperty> \
                  <Property><Name>SspId</Name><Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p4:type="q1:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">b5b6effd-4925-43e4-ad43-a257213dedec</Value></Property> \
                  <Property><Name>TermSetId</Name><Value xmlns:q2="http://www.w3.org/2001/XMLSchema" p4:type="q2:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">b49f64b3-4722-4336-9a5c-56c326b344d4</Value></Property> \
                  <Property><Name>AnchorId</Name><Value xmlns:q3="http://www.w3.org/2001/XMLSchema" p4:type="q3:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">00000000-0000-0000-0000-000000000000</Value></Property> \
                  <Property><Name>UserCreated</Name><Value xmlns:q4="http://www.w3.org/2001/XMLSchema" p4:type="q4:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value></Property> \
                  <Property><Name>Open</Name><Value xmlns:q5="http://www.w3.org/2001/XMLSchema" p4:type="q5:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">true</Value></Property> \
                  <Property><Name>TextField</Name><Value xmlns:q6="http://www.w3.org/2001/XMLSchema" p4:type="q6:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{155963e0-be13-5b99-bb2b-df69fdcbbeec}</Value></Property> \
                  <Property><Name>IsKeyword</Name><Value xmlns:q8="http://www.w3.org/2001/XMLSchema" p4:type="q8:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value></Property> \
                  <Property><Name>CreateValuesInEditForm</Name><Value xmlns:q9="http://www.w3.org/2001/XMLSchema" p4:type="q9:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value></Property> \
                </ArrayOfProperty> \
               </Customization> \
          </Field>';


list.get_fields().addFieldAsXml(taxField, true, SP.AddFieldOptions.defaultValue);
context.executeQueryAsync(function() {
       console.log("Managed metadata field provisioned successfully");
    },
    function(sender, args) {
       console.log(args.get_message());
    }
);

Note: there is no need to specify explicitly a secondary field since it will be provisioned automatically.

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.