Hi,
i am trying to assign sub-folders level permission in sharepoint document library. Want to delete all previous inherit permission first and then assign sub-folders permission to specific group. i am having problem with following code, cannot make a call to the function. Could you tell me what should i modify the code to work properly.
Thanks in advanced
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ManageFolderLevelPermission
{
class FolderLeverPermission
{
static void Main(string[] args)
{
//Connect to Sharepoint Site
SPSite oSPSite = new SPSite("http://nyc01d1sp:8080/");
//Open Sharepoint Site
SPWeb web = oSPSite.OpenWeb("/hr/DI/");
//Get the Sharepoint list item for giving permission
foreach(SPFolder folder in web.GetFolder("http://nyc01d1sp:8080/hr/DI/docs/").SubFolders);
setPermissions();
}
private static void setPermissions(SPFolder folder,SPSite oSPSite, SPWeb web)
{
SPGroupCollection spc = web.SiteGroups;
//Break the role inheritance in order to assign individual rights on folders
if (!folder.Item.HasUniqueRoleAssignments)
{
folder.Item.BreakRoleInheritance(true);
}
while (folder.Item.RoleAssignments.Count > 0)
{
try
{
folder.Item.RoleAssignments.Remove(0);
}
catch (Exception)
{
break;
}
}
//Role Assignment For the Current User
SPUser CurrentUser = SPContext.Current.Web.CurrentUser;
SPGroup group = spc["GroupName"];
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)CurrentUser);
roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator));
folder.Item.RoleAssignments.Add(roleAssignment);
//Role Assignment for the Group "Contentteam - Management"
roleAssignment = new SPRoleAssignment((SPPrincipal)group);
roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator));
folder.Item.RoleAssignments.Add(roleAssignment);
oSPSite.AllowUnsafeUpdates = true;
folder.Item.Update();
}
}
}