Working with Permission Levels in Multilingual SharePoint Environments

Most of the times while working on permission levels many developers used to access the permissions levels with following code snippet.
  1. using (SPSite site = new SPSite("<<WebUrl>>"))  
  2. {  
  3.     using (SPWeb web = site.OpenWeb())  
  4.     {  
  5.         SPRoleDefinition fullControlRole = web.RoleDefinitions["Full Control"];  
  6.         SPRoleDefinition contributeRole = web.RoleDefinitions["Contribute"];  
  7.     }  
  8. }  
Now if we run the same code in different UI cultures other than English culture then this code will throw exception as SharePoint Object Model does not find permission levels with names ‘Full Control’, ‘Contribute’, etc. Such exceptions can be avoided by using following code snippet. We should use enum values provided by SharePoint Object Model for getting the permission levels.
  1. using (SPSite site = new SPSite("<<WebUrl>>"))    
  2. {    
  3.    using (SPWeb web = site.OpenWeb())    
  4.    {    
  5.       SPRoleDefinition fullControlRole = web.RoleDefinitions.GetByType(SPRoleType.Administrator);    
  6.       SPRoleDefinition contributeRole = web.RoleDefinitions.GetByType(SPRoleType.Contributor);    
  7.    }    
  8. }   
Now the next tricky part is about getting ‘Approve’ OOB permission level for which there is no enum value provided by SharePoint Object Model. We can get ‘Approve’ OOB permission level by using following code snippet.
  1. public class PublishingResources    
  2. {    
  3.    private static System.Resources.ResourceManager _ResourceManager;    
  4.          
  5.    static PublishingResources()    
  6.    {    
  7.       var resourceAssembly = System.Reflection.Assembly.Load("Microsoft.SharePoint.Publishing.intl, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");    
  8.     
  9.       _ResourceManager = new System.Resources.ResourceManager("Microsoft.SharePoint.Publishing.Strings", resourceAssembly);    
  10.    }    
  11.          
  12.    internal static string GetString(string resourceName, System.Globalization.CultureInfo culture)    
  13.    {    
  14.       return _ResourceManager.GetString(resourceName, culture);    
  15.    }    
  16.     
  17.    internal static string GetString(string resourceName, int lcid)    
  18.    {    
  19.       return _ResourceManager.GetString(resourceName, new CultureInfo(lcid));    
  20.    }    
  21. }    
  22.     
  23.     
  24. using (SPSite site = new SPSite("<<WebUrl>>"))    
  25. {    
  26.    using (SPWeb web = site.OpenWeb())    
  27.    {    
  28.       string roleName = PublishingResources.GetString(Microsoft.SharePoint.Publishing.Internal.StringIds.RoleNameApprover, Thread.CurrentThread.CurrentUICulture.LCID);    
  29.     
  30.       return web.RoleDefinitions.Cast<SPRoleDefinition>()    
  31.             .FirstOrDefault(r => string.Compare(r.Name, roleName, StringComparison.OrdinalIgnoreCase) == 0);    
  32.    }    
  33. }  
Now task for the readers – How to get ‘Design’ OOB permission level.
Ebook Download
View all
Learn
View all