Testing Whether A User Has SPBasePermission On SPList Object

This was driving me nuts yesterday, I was looping through all sorts of crap, and all I wanted to do was conditionally display something base on the SPBasePermissions enumeration. Reason being, we have a custom menu that appends to the Site Actions menu (i.e. through the use of a feature) building out relative links to the site to a variety of lists using SubMenuTemplate and MenuItemTemplate objects. I didn’t want to display on my menu lists that a SPUser didn’t have specific rights to, particularly I wanted to test whether the principal had SPBasePermissions.DeleteListItems. So, I just wanted a stupid little method that took an SPList object as a parameter, then I could use it to wrap my menu outputs within an if statement to conditionally display them. This probably isn’t the most optimized way that this could be written, but it works for me:

You know what sucks? After you always write these annoying little things they make sense, and you sit there going, “well, of course it should be that way”. I hadn’t read about the ISecurableObject interface before, but it is pretty clutch, since it will allow you to expose the role assignments and pretty much tackle any permission manipulation with pretty much any SharePoint object type. Which is where I got stuck.