Environment: SP2010 Enterprise, VS2012, c# Server-Side, full control of environment

I'm trying to assign unique permissions to an SPItem (a folder in a document library) through a console app on the server. I've been reading up on how to break inheritance and assign permissions on this blog and combing through msdn articles like this one. I have a specific custom role, CustomContribute, that I want to assign to certain folders programmatically, but none of the guides I've found tell how to do anything beyond the OOB roles (Contribute, Delete, Read, etc). A few places like this one explain how to create a custom permission level, but I need a little help making the leap from that to actually assigning it to things.

Am I on the right track, or barking up the wrong tree? Any advice or links would be appreciated!

Edit: The SPFolder.Item property is probably what I'm looking for to add the role definition but I'm having issues specifying the folder in such a way that the property will fetch the item. See the code below for an example.

SPFolder oFolder = oWeb.GetFolder(folderPath);
SPListItem oItem = oFolder.Item; // Here it throws an exception, "The object specified does not belong to a list."

Is there a better way to select the folder based on the list itself, rather than through the SPWeb?

1 Answer 1


Yes its a little confusing, the thing to note is that a folder is really a SPListItem so the permissions are applies at that object.

once you have created your role in the appropriate site or web. To be able to use it you need to grab it from the SPWeb.RoleDefinitions collection.

You can do something like, you need to add reference to System.Linq for following code.

//create SPWeb object thisWeb

SPRoleDefinition myRole = thisWeb.RoleDefinitions.OfType(SPRoleDefinition).FirstOrDefault(r=>r.Name=="CustomContribute");

//now get your user/group

SPUser currentUser = thisWeb.CurrentUser;

//now get your list
SPList restrictedDocuments = thisWeb.ListsTryGetList("Documents");

//create role assignment and role definition binding where you link your user to the permissions they have.

SPRoleAssignment roleAssignment = new SPRoleAssignment(currentUser);

//break permission inheritance so you can assign permissions


//add permission


Hope that helps please ignore case issues trying to type code on touch screen Interface is painful.

You would probably would not cannot set permissions on root folder but hopefully you get the jist

  • Thanks for the help. I've tried your suggested method but ran into a problem with the SPRoleDefinition, VS gives errors about it being a type but used like a variable, and that OfType doesn't take 1 argument. I have System.Linq referenced, but I'm not experienced enough with Linq to really know what's going on. The other problem is that I don't know how to specify a subfolder as an SPListItem object, previously I've always used an SPFolder object with the server relative url. Thoughts?
    – thanby
    Commented Aug 9, 2013 at 14:45
  • 1
    Read through the code a bit more and you'll see how to turn an SPFolder object into a SPListItem. the RootFolder is an example of an SPFolder object.
    – Simon Doy
    Commented Aug 9, 2013 at 17:07
  • Ah I get it. Unfortunately the way I'm selecting the SPFolder object is through the SPWeb.GetFolder method, which doesn't seem to play nicely with the .Item property because there's no list specified. Is there a better way to select the folder? See my edit above for an example.
    – thanby
    Commented Aug 12, 2013 at 18:24

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.