Request a Quote

Create List for multilingual site in SharePoint 2013

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);
            }
}

 

Leave a Reply

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

Copyright © 2015 Trimantra Software Solution LLP- Software Outsourcing Company India. All rights reserved.