Get Sharepoint Term’s Custom Local Property values (Client Object Model)

We can get values of Custom local properties of Term using Client Object Model.

Register following files on your page:

<SharePoint:ScriptLink ID="spPage" Name="sp.js" LoadAfterUI="true" Localizable="false" runat="server" />
<script type="text/javascript" src="../../../_layouts/15/SP.Taxonomy.js"></script>

 

Js code to get Custom Local Property values:

$(document).ready(function () {
	SP.SOD.executeOrDelayUntilScriptLoaded(loadAllTerms, 'SP.Taxonomy.js');
});

//loads all existing terms
function loadAllTerms() {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllTerms);
}

var terms = null; // terms object
var termContext = null; // termContext object
var termStore = null; // termStore object
var termSet = null; // termSet object

//gets all existing terms
function getAllTerms() {
    var metaDataServiceName = "Managed Metadata Service";
    var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js", function () {
        $.getScript(scriptbase + "SP.js", function () {
            $.getScript(scriptbase + "SP.Taxonomy.js", function () {
                termContext = SP.ClientContext.get_current();
                var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(termContext); //Current Taxonomy Session
                var termStores = taxSession.get_termStores(); //Term Stores
                termStore = termStores.getByName(metaDataServiceName); //Name of the Term Store from which to get the Terms.
                termSet = termStore.getTermSet("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"); //GUID of Term Set from which to get the Terms.
                terms = termSet.getAllTerms();
                // Get Custom Local Properties
                getTermCustomProperties();
            });
        });
    });
}

// loads the terms properties
function getTermCustomProperties() {
    termContext.load(terms);
    termContext.executeQueryAsync(Function.createDelegate(this, function () { onQuerySucceeded}), Function.createDelegate(this, onQueryFailed));
}

// get specific Term Custom Local Property
function onQuerySucceeded() {
    var termEnumerator = terms.getEnumerator();
    while (termEnumerator.moveNext()) {
        var currentTerm = termEnumerator.get_current();
        // Get value of Custom Local property "Property1"
        var property1 = currentTerm.get_objectData().get_properties().LocalCustomProperties.Property1;
        // Get value of Custom Local property "Property2"
        var property2 = currentTerm.get_objectData().get_properties().LocalCustomProperties.Property2;
        // Get value of Custom Local property "Property3"
        var property3 = currentTerm.get_objectData().get_properties().LocalCustomProperties.Property3;
    }
}

// Exceprion to get data from Term
function onQueryFailed(sender, args) {
    alert('TermStore Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}

See my previous blog : How to Create Custom Local Property in Sharepoint Terms

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website