4

I need to get the folder object, SPFolder using C# in my SP 2013 on-prem site collection.

Below code, I have created a new folder programmatically.

Now how can I get this SPFolder object and later I wanna apply Contribute permissions for this folder?

It would be good, if i can get GUID approach. There is only one param, we need to pass , thats GUID, but how to retrieve the GUID of the folder.

try
{
    SPList marketrsrchDocLib = paramCurrentWeb.Lists.TryGetList("MarketResearch");
    if (marketrsrchDocLib != null)
    {
        listitemDivisionFolder = marketrsrchDocLib.Items.Add("", 
                                  SPFileSystemObjectType.Folder, strDivisionNameCreated);
        listitemDivisionFolder.Update();
     }
     marketrsrchDocLib.Update();

     //now apply permissionsfor the folder -
     SPFolder folder =   paramCurrentWeb.GetFolder(???) 
     //?? what should be the correct code here
}

1 Answer 1

6

Can you try out the below sample code that I took from here - I have a online enviornment, hence couldnt try,

SPFolder folder = SharePointConfiguration.Site.GetFolder(path); //path of the folder
SPGroupCollection spc = SharePointConfiguration.Site.SiteGroups;
SPGroup group = spc[groupName]; //name of the group with the contribute permissions
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)group);
folder.Item.BreakRoleInheritance(true);
roleAssignment.RoleDefinitionBindings.Add(SharePointConfiguration.Site.RoleDefinitions["Contribute"]);
folder.Item.RoleAssignments.Add(roleAssignment);

Basically, this is taking a folder instance by path, picking up the relevant group and assigning permissions as specified.

Source : MSDN

2
  • Whats the value of the path variable?
    – samolpp2
    Commented Jul 13, 2017 at 11:54
  • 1
    would be in the format <sp site url>/<Library Name>/<Folder> Commented Jul 13, 2017 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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