Thursday, August 16, 2012

CustomAction Menu for Full Control permission

This code snippet to helps you to understand "How to hide the Custom Action menu which placed under location of SharePoint Site Action menu". I have found so many workaround for this issue on forums but all dealing with Javascript to hide menu. Later I checked the MSDN notes on CustomAction feature,I came to know the attribute"Rights". This will check the currently logged in user's permission level on the site or list/document library accordingly it will display the menu. So I wanted to show the custom action menu only for those having "Full Control" access permission on the site or web.


  
    
  

Again,I just redirect to application page from this custom action menu.On my page loading checking the user's permission on the site using below code.
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (!web.DoesUserHavePermissions(web.CurrentUser.LoginName.ToString(), SPBasePermissions.FullMask))
                        {
                            lblPermissionLevel.Text = "You do not have permission to view this content";
//WebPart's unique Id                            
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (!web.DoesUserHavePermissions(web.CurrentUser.LoginName.ToString(), SPBasePermissions.FullMask))
                        {
                            lblPermissionLevel.Text = "You do not have permission to view this content";
//WebPart's guid                            
g_0114039b_aaed_49ae_bc9f_479a130c68dc.Visible = false;
                            g_9c0f6d83_fbb9_441d_a0fd_43a266e68657.Visible = false;

                        }
                    }
                }
                        }
                    }
                }
Both codes are equivalent to each other both inheriting the SPBasePermission class.