Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

Deleting all inherit sub-folders permission and reassigning to specific group

$
0
0

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

            

Viewing all articles
Browse latest Browse all 11508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>