In this post I am showing you how to assign custom permissions to sharepoint groups.
Add following namespaces:
---------------------------
using Microsoft.SharePoint;
using System.Resources;
using System.Reflection;
----------------------------
namespace Demo
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("URL");
SPWeb web = site.OpenWeb();
ResourceManager rm = new ResourceManager("DemoFolder.Resource1", Assembly.GetExecutingAssembly()); //We are creating resource file and taking info from there.
string MUFolder = rm.GetString("MUFolder");
string[] grpmu = MUFolder.Split(',');
string CntryFolder = rm.GetString("CntryFolder");
string[] grpCntry = CntryFolder.Split(',');
string CCfolder = rm.GetString("CCfolder");
string[] grpcc = CCfolder.Split(',');
string fold = rm.GetString("Folder");
string[] grpList = fold.Split(',');
for (int i = 0; i < grpList.Length; i++)
{
SPList list = web.Lists["CustomList"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + grpList[i] + "</Value></Eq></Where>";
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
SPFolder folder = web.GetFolder(item.Url);
folder.Item.BreakRoleInheritance(true);
SPGroupCollection spc = web.SiteGroups;
foreach (SPGroup oGroup in spc)
{
folder.Item.RoleAssignments.Remove((SPPrincipal)oGroup);
}
Console.WriteLine("All existing groups are removed");
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)web.SiteGroups[grpmu[i]]);
roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Country"]);
folder.Item.RoleAssignments.Add(roleAssignment);
Console.WriteLine("Assigned " + grpmu[i] + " to " + grpList[i]);
SPRoleAssignment roleAssignment1 = new SPRoleAssignment((SPPrincipal)web.SiteGroups[grpcc[i]]);
roleAssignment1.RoleDefinitionBindings.Add(web.RoleDefinitions["Country"]);
folder.Item.RoleAssignments.Add(roleAssignment1);
Console.WriteLine("Assigned " + grpcc[i] + " to " + grpList[i]);
SPRoleAssignment roleAssignment2 = new SPRoleAssignment((SPPrincipal)web.SiteGroups[grpCntry[i]]);
roleAssignment2.RoleDefinitionBindings.Add(web.RoleDefinitions["Country"]);
folder.Item.RoleAssignments.Add(roleAssignment2);
Console.WriteLine("Assigned " + grpCntry[i] + " to " + grpList[i]);
SPRoleAssignment roleAssignment3 = new SPRoleAssignment((SPPrincipal)web.SiteGroups["Custom"]);
roleAssignment3.RoleDefinitionBindings.Add(web.RoleDefinitions["Custom"]);
folder.Item.RoleAssignments.Add(roleAssignment3);
Console.WriteLine("Assigned Custom to " + grpList[i]);
SPRoleAssignment roleAssignment4 = new SPRoleAssignment((SPPrincipal)web.SiteGroups["ReadOnlyUser"]);
roleAssignment4.RoleDefinitionBindings.Add(web.RoleDefinitions["ReadOnlyUser"]);
folder.Item.RoleAssignments.Add(roleAssignment4);
Console.WriteLine("Assigned ReadOnlyUser to " + grpList[i]);
web.Update();
}
}
site.Dispose();
}
}
}
--------------------------------------------
This way we can assign permissions programmatically.
Add following namespaces:
---------------------------
using Microsoft.SharePoint;
using System.Resources;
using System.Reflection;
----------------------------
namespace Demo
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("URL");
SPWeb web = site.OpenWeb();
ResourceManager rm = new ResourceManager("DemoFolder.Resource1", Assembly.GetExecutingAssembly()); //We are creating resource file and taking info from there.
string MUFolder = rm.GetString("MUFolder");
string[] grpmu = MUFolder.Split(',');
string CntryFolder = rm.GetString("CntryFolder");
string[] grpCntry = CntryFolder.Split(',');
string CCfolder = rm.GetString("CCfolder");
string[] grpcc = CCfolder.Split(',');
string fold = rm.GetString("Folder");
string[] grpList = fold.Split(',');
for (int i = 0; i < grpList.Length; i++)
{
SPList list = web.Lists["CustomList"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + grpList[i] + "</Value></Eq></Where>";
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
SPFolder folder = web.GetFolder(item.Url);
folder.Item.BreakRoleInheritance(true);
SPGroupCollection spc = web.SiteGroups;
foreach (SPGroup oGroup in spc)
{
folder.Item.RoleAssignments.Remove((SPPrincipal)oGroup);
}
Console.WriteLine("All existing groups are removed");
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)web.SiteGroups[grpmu[i]]);
roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Country"]);
folder.Item.RoleAssignments.Add(roleAssignment);
Console.WriteLine("Assigned " + grpmu[i] + " to " + grpList[i]);
SPRoleAssignment roleAssignment1 = new SPRoleAssignment((SPPrincipal)web.SiteGroups[grpcc[i]]);
roleAssignment1.RoleDefinitionBindings.Add(web.RoleDefinitions["Country"]);
folder.Item.RoleAssignments.Add(roleAssignment1);
Console.WriteLine("Assigned " + grpcc[i] + " to " + grpList[i]);
SPRoleAssignment roleAssignment2 = new SPRoleAssignment((SPPrincipal)web.SiteGroups[grpCntry[i]]);
roleAssignment2.RoleDefinitionBindings.Add(web.RoleDefinitions["Country"]);
folder.Item.RoleAssignments.Add(roleAssignment2);
Console.WriteLine("Assigned " + grpCntry[i] + " to " + grpList[i]);
SPRoleAssignment roleAssignment3 = new SPRoleAssignment((SPPrincipal)web.SiteGroups["Custom"]);
roleAssignment3.RoleDefinitionBindings.Add(web.RoleDefinitions["Custom"]);
folder.Item.RoleAssignments.Add(roleAssignment3);
Console.WriteLine("Assigned Custom to " + grpList[i]);
SPRoleAssignment roleAssignment4 = new SPRoleAssignment((SPPrincipal)web.SiteGroups["ReadOnlyUser"]);
roleAssignment4.RoleDefinitionBindings.Add(web.RoleDefinitions["ReadOnlyUser"]);
folder.Item.RoleAssignments.Add(roleAssignment4);
Console.WriteLine("Assigned ReadOnlyUser to " + grpList[i]);
web.Update();
}
}
site.Dispose();
}
}
}
--------------------------------------------
This way we can assign permissions programmatically.
No comments:
Post a Comment