1

I am doing a loop through for all the folders within a document library and wanna apply the Read permissions to a custom SPGroup.

How can i achieve the same?

Though i have written the code, by storing all the folder names from the doc lib into a generics ILIST object and then iterating thru these ILIST applying the permissions.

But it doesn't work. How to achieve the same? shouldn't i store the SPLISTITEM onto a ILIST OBJECT ?

IList<SPListItem> allFolderItems_BI = new List<SPListItem>();

            SPDocumentLibrary objBENCHMARKINDEXDocLibrary = 
         paramCurrentWeb.Lists.TryGetList("BenchmarkIndex") as 
       SPDocumentLibrary;
            if (objBENCHMARKINDEXDocLibrary != null && 
       objBENCHMARKINDEXDocLibrary.Folders.Count > 0)
            {
                SPQuery query = new SPQuery();
                //Condition to check the item type is folder or not
                query.Query = "<Where><Eq><FieldRef Name='FSObjType'/><Value 
              Type='Lookup'>1</Value></Eq></Where>";
                //Retrieve the items based on Query 
                SPListItemCollection folderItems = 
             objBENCHMARKINDEXDocLibrary.GetItems(query);
                string folderDetails = "";
                //Get the name and Url for the folder 
                foreach (SPListItem singleFolderitem in folderItems)
                {
                    folderDetails = singleFolderitem.Name;
                    allFolderItems_BI.Add(singleFolderitem);
                }
                if (allFolderItems_BI != null && allFolderItems_BI.Count > 
                   0)
                {
                    foreach (var singleFolderNamein_BI in allFolderItems_BI)
                    {
                        //if(singleFolderNamein_BI.HasUniqueRoleAssignments)
                        {
                            SPRoleAssignment 
           roleAssignmentFolderGroup_NPH_BI = new SPRoleAssignment(paramobjNPHDivGroup);
                            SPRoleDefinition 
              roleDefinitionFolderGroup_NPH_BI = 
        paramCurrentWeb.RoleDefinitions["NPD ReadILP"];
      roleAssignmentFolderGroup_NPH_BI.RoleDefinitionBindings.Add
         (roleDefinitionFolderGroup_NPH_BI);
     singleFolderNamein_BI.RoleAssignments.Add
       (roleAssignmentFolderGroup_NPH_BI);
                            //singleFolderNamein_BI.Update();
                          }
                      }
                   }
                //folderDetails += "Folder Name:" + item.Name + "<br/>Folder 
     URL:" + spWeb.Url + "/" + item.Url + "<br/>"; 
            }
  • What's the error/problem with that code? – Evariste Jul 22 '17 at 8:49
  • My code is running thru an infinite loop.even after 4+ minutes, debugging it loop thru the for each .. N didn't complete – SaMolPP Jul 22 '17 at 9:10
  • How many folders do you have? – Evariste Jul 23 '17 at 5:56
  • there are 71 folders at present. But the tricky this is that, when an item in another list called "Division" added , i need to create that newly created Division name in this doc lib as a folder .also I need to apply create/edit/delete kind of permissions on the newly created folder. At the same time, when 72nd folder is created as part of the admin activity, all the remaining 71 folders has also need to be updated with the spgroup "FH <<NewDivisionName>>" having READ permissions. s part of the new division listitem creation,i need to create multiple spgroups. – SaMolPP Jul 23 '17 at 7:38
  • the group names start with FH <NEWDIVISIONNAME>, NPH <NEWDIVISIONNAME>, "PL <NEW DIVISIONNAME>" these groups have to be present with read only access in this doc lib folders! – SaMolPP Jul 23 '17 at 7:39
2

Are you sure the permissions inheritance is stopped on the folders before calling RoleAssignments.Add?
You should try calling singleFolderNamein_BI.BreakRoleInheritance(true); first.

Something else: instead of retrieving all folders at once and then looping through them, you should definitely try a recursive approach: you get and process each folder at level 0, and for each of them you then get its child-folders to process them, etc.
Something like:

private void ProcessAllSubFolders(SPFolder folder)
{
    if (folder == null)
        return;

    foreach (SPFolder subFolder in folder.SubFolders)
    {
        SPRoleAssignment roleAssignmentFolderGroup_NPH_BI = new SPRoleAssignment(paramobjNPHDivGroup);
        SPRoleDefinition roleDefinitionFolderGroup_NPH_BI = paramCurrentWeb.RoleDefinitions["NPD ReadILP"];
        roleAssignmentFolderGroup_NPH_BI.RoleDefinitionBindings.Add(roleDefinitionFolderGroup_NPH_BI);
        singleFolderNamein_BI.RoleAssignments.Add (roleAssignmentFolderGroup_NPH_BI);

        ProcessAllSubFolders(subFolder);
    }
}

Usage:

 SPDocumentLibrary objBENCHMARKINDEXDocLibrary = paramCurrentWeb.Lists.TryGetList("BenchmarkIndex") as SPDocumentLibrary;
 ProcessAllSubFolders(objBENCHMARKINDEXDocLibrary.RootFolder);
  • Am already breaking role inheritance before calling this code block – SaMolPP Jul 22 '17 at 9:12
  • if possible, can you help, an links /sources where i can refer/see, the code for applying permissions for all the folders to a spgroup? – SaMolPP Jul 22 '17 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.