0

How can I get the default value of a Managed metadata column. I'm trying to do:

var ctx = new SP.ClientContext(url);
web = ctx.get_web();
lists = web.get_lists();

ctx.load(lists,'Include(Title,BaseType,Fields.Include(Title,InternalName,DefaultValue,TypeAsString))');
ctx.executeQueryAsync(function(){onQuerySucceeded(myParam)}, onQueryFailed);

But the default value when the column is TaxonomyFieldType is empty. How can I get the value? I know that the column has a default.

1 Answer 1

1

I was curious about why it didn't work so started testing my self. I got it finally working, using an example from msdn (https://msdn.microsoft.com/fr-fr/library/office/hh185009(v=office.14).aspx). Try using following code to see if you it return a default value:

function retrieveAllListsAllFields() {
    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();
    var collList = oWebsite.get_lists();

    this.listInfoArray = clientContext.loadQuery(collList, 
        'Include(Title,Fields.Include(Title,InternalName, DefaultValue))');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this._onQueryFailed));
}

function onQuerySucceeded() {

    var listInfo = '';

    for (var i = 0; i < this.listInfoArray.length; i++) {

        var oList = this.listInfoArray[i];
        var collField = oList.get_fields();

        var fieldEnumerator = collField.getEnumerator();

        while (fieldEnumerator.moveNext()) {
            var oField = fieldEnumerator.get_current();          
                listInfo += '\nList: ' + oList.get_title() + 
                    '\n\tField Title: ' + oField.get_title() + 
            '\n\tField DefaultValue: ' + oField.get_defaultValue();
        }
    }
    console.log(listInfo);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
   retrieveAllListsAllFields();
});

I tested on SharePoint Online with a managed metadata field and it return the default value in this format: 5;#testLevel2|8e5ede37-1722-4b4e-928c-8e15b52a2bd2

3
  • No, this is exactly what is not working for me. It's returning "" as DefaultValue for me
    – yngrdyn
    Apr 29, 2016 at 13:39
  • I even try to get the values for an specific library and I'm having the same issue
    – yngrdyn
    May 2, 2016 at 20:31
  • The problem was that the defaults were assigned to the root folder instead of the library
    – yngrdyn
    May 3, 2016 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.