0

I am helping maintain and clean up the code on a SharePoint 2010-based web site/application. I have found code that handles permissions sprinkled all over the code base. Is there no idiomatic SharePoint way of doing this that uses somethign more maintainable or declarative like attributes or something?

SPWeb cWeb = site.AllWebs["SiteName"];
SPGroup siteManagerGroup = cWeb.Groups["Site Manager Group"];
if (siteManagerGroup.ContainsCurrentUser)
{
    // Allow some action
}
else
{
   // Do not allow some action
}
1

dont quite understand where your comming from, but im going to have a go ;)

I presume that you have the code above repeated all over?

you could place the code into a class of its own? within a new project make it into a method that returns true or false.

then you could get the dll from that project and add it as a reference in any project that would need it.

if the project was called checkgroup i could then do:

If (checkgroups.checkGroup.check("SiteName","Site Manager Group",site) == true)
{

}
else
{

}

so anywhere you have that code you can replace it with the above and that calls the method below. This would be global as the project below would be in the gac and accessed by any other project requiring that method! the project also needs to be strongly named to be in the gac!

public method called:

namespace checkgroups
{
    public class checkGroup
    {
      public bool check(string sitename,string groupname,SPSite site)
      {
         using(SPWeb cWeb = site.AllWebs[sitename])
         {
           SPGroup siteManagerGroup = cWeb.Groups[groupname];
           if (siteManagerGroup.ContainsCurrentUser)
           {
              return true;
           }
           else
           {
              return false;
           }
         }
         return false;
      }
    }
}
  • Thanks for the answer that is pretty close to what I ended up doing. I was hoping I could just decorate methods with an attribute describing the permission required but I guess it's not that simple. – Shane Wealti Dec 23 '13 at 21: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.