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

Get Sharepoint Term’s Custom Local Property values (Server side)

We can get values of Custom local properties using server side code. There is no direct way to get custom local from terms. For that we need to do manual coding which is as follow:

 

/// Get Custom Local Property values of particular term.
private void GetCustomLocalPropertyValues(Term term)
{
       int valCnt = 0;
       int keyCnt = 0;
       string property1Val = null;
       string property2Val = null;
       string property3Val = null;
       int property1KeyId = 0;
       int property2KeyId = 0;
       int property3KeyId = 0;
       foreach (var custPropKey in term.LocalCustomProperties.Keys)
       {
       	keyCnt++;
              if (custPropKey == "Property1")
              	property1KeyId = keyCnt;
              if (custPropKey == "Property2")
              	property2KeyId = keyCnt;
              if (custPropKey == "Property3")
              	property3KeyId = keyCnt;
       }
       foreach (var custProp in term.LocalCustomProperties.Values)
       {
       	valCnt++;
              if (valCnt == property1KeyId)
              	property1Val = custProp;
              if (valCnt == property2KeyId)
              	property2Val = custProp;
              if (valCnt == property3KeyId)
              	property3Val = custProp;
       }
 }

 

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

Create Custom Local Property in Sharepoint Terms

In my project there is a requirement to manage Terms with Custom Local property. For that there is a simple way to create or update custom local property in term. We can use this code at the time of Term create or update. Here is Code to create custom local property for particular term set.

///<summary>
/// Creates the custom local properties of a Term
///</summary>
private void CreateCustomLocalProperties(Term term)
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        term.SetLocalCustomProperty("Property1", "Value1");
        term.SetLocalCustomProperty("Property2", "Value2");
        term.SetLocalCustomProperty("Property3", "Value3");
    });
}

We can see our custom local property inside Central Admin > Manage service applications > Managed Metadata Service > select your term inside Term set:

2015-02-27_1148