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

Accessing resource file value in JavaScript in Multilingual site in SharePoint 2013

Writing server side code to get resource file value in JavaScript is the easy way and most people using server side code to get resource file value. But when there is a requirement to write advanced JavaScript based development and to get resource file value in the JavaScript file (.js) at that time we need to write the JavaScript code. Below is the JavaScript code to get resource file value.

Resource File Image:

b3

 Javascript Code:

$(document).ready(function () {

    var eventName = "";

    //load the authoring resource file depending on the current language
    function loadResourceJs() {
        currentUiCluture = STSHtmlEncode(Strings.STS.L_CurrentUICulture_Name);
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getResourceVal);
    }

    //get resource strings from ‘EventNotification’ resource file depending on the current language
    function getResourceVal() {
        var resourceUrl = '/_layouts/15/ScriptResx.ashx?culture=' + currentUiCluture + '&name=EventNotification;

        $.getScript(resourceUrl, function () {
            eventName = Res['eventName'];
        });
    }

    SP.SOD.executeOrDelayUntilScriptLoaded(loadResourceJs, 'core.js');
});

 

The resource file should be deployed in the 15 hive and resource file should be language specific resource file. Here Culture is the locale language for the resource file. If the language is de-de then it will get the value from German resource file and if the language is en-us then it will get the value from English resource file.

SharePoint Error: This page allows a limit of 200 controls, and that limit has been exceeded

I have created a custom page layout through visual studio 2013 on which there are a large number of controls are registered. The page that I had created through the page layout on that page I faced this max control limit issue. Generally , when working with a SharePoint master page, page layout, forms we might will face this type of issue.

The problem was we have a large number of controls registered. The limit is set in the web.config file. By Default controls limit is set by 200.

To resolve this issue open the web.config file, located in a virtual directory in c drive. Find the safe control block as shown below and increase the “MaxControls” limit and do IISRESET.

<SafeMode MaxControls=”200″ CallStack=”false” DirectFileDependencies=”10″ TotalFileDependencies=”50″ AllowPageLevelTrace=”false”>