Create List for multilingual site in SharePoint 2013

In our previous blog we have seen that how to create site column, content type for multilingual site. Now we will create custom list for all multilingual site with different name on feature activation.

 

try
{
    using (SPSite site = new SPSite(RootSiteUrl))
    {
        SPWebCollection allWebs = site.AllWebs;
        foreach (SPWeb currentWeb in allWebs)
        {
           if (PublishingWeb.IsPublishingWeb(currentWeb))
           {
               using (currentWeb)
               {
                 CreateListInstance(currentWeb.Url, "$Resources:EventNotification,EventListName", "", true, true);
               }
           }
         }
    }
}
catch (Exception ex)
{
     Logger.LogError(ex.Message, "List Error", ex.StackTrace);
}

Here the SPWeb object will give all multilingual site and sub site object. The above code will create list in all sub sites. If you want to avoid any sub site then you can specify that sub site name in if condition.

In the above code “EventNotification” is the name of the resource file and “EventListName” is the name attribute of resource file. It will get the value of “EventListName” from the resource file and create list with that name. In the above code CreateListInstance is the method to creates list which is below.

 

private void CreateListInstance(string absoluteSiteUrl, string dispalyNameFromResx, string description, bool allowContentType, bool allowVersioning)
{
     SPList list = null;
     try
     {
                SPSecurity.RunWithElevatedPrivileges(() =>
                 {
                     using (SPSite site = new SPSite(absoluteSiteUrl))
                     {
                         using (SPWeb web = site.OpenWeb())
                         {
                             if (web.Lists.TryGetList(dispalyNameFromResx) == null)
                             {
                                 Guid listId = web.Lists.Add(dispalyNameFromResx, description, SPListTemplateType.GenericList);
                                 list = web.Lists[listId];
                                 list.EnableVersioning = allowVersioning;
                                 list.ContentTypesEnabled = allowContentType;
                                 list.EnableModeration = false;
                                 list.Update();
                             }
                         }
                     }
                 });
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message, "List create error", ex.StackTrace);
            }
}

 

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

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”>