kamagra how much to take

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:

  1. private bool UserHasDeletePermission(SPList list)
  2. {
  3. try
  4. {
  5. if (((ISecurableObject)list).DoesUserHavePermissions(SPBasePermissions.DeleteListItems))
  6. {
  7. return true;
  8. }
  9. }
  11. catch (Exception exception)
  12. {
  13. WriteLogEvent(string.Format("An Error Occured With The {name of your app} | Exception Message:{0} StackTrace: {1}", exception.Message, exception.StackTrace));
  14. }
  16. return false;
  17. }
  18. public void WriteLogEvent(string strmessage)
  19. {
  20. EventLog comLog = new EventLog("Application");
  21. if (!EventLog.SourceExists("{name of your app}"))
  22. {
  23. EventLog.CreateEventSource("{name of your app}", "Application");
  24. }
  25. comLog.Source = "{name of your app}";
  26. comLog.WriteEntry(strmessage);
  27. }

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.

Oh well, hopefully it saves someone some time :)


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>