In SharePoint 2010, I have custom Web Parts. In the Web Part Gallery, I grant read permissions to certain users to let them add these web parts to a page. Within CreateChildControls, how can I detect if the current user is able to read the current web part?

Basically, I am looking for the equivalent of the DoesUserHavePermissions method (from SPList) for the current web part.


You can use the below code in your CreateChildControls method:

protected override void CreateChildControls()
    using (SPSite spSite = new SPSite("http://sitecollectionurl"))
         using (SPWeb spWeb = spSite.OpenWeb())
            // Get the current user.
            SPUser currentUser = SPContext.Current.Web.CurrentUser;
            // Get the list.
            SPList spList = spWeb.Lists.TryGetList("MyList");
            // Variable to determine permission.

            bool userPermission = spList.DoesUserHavePermissions(currentUser, SPBasePermissions.AddListItems);
            if (userPermission)
                  // Perform operation here.
  • Thanks. Yes, that is the DoesUserHavePermissions method for interrogating a LIST. I mentioned that. I am looking for something similar to interrogate the current WEB PART. – CigarDoug Nov 1 '16 at 11:02
  • In the line trygetlist, use spWeb.Lists.TryGetList("Web Part Gallery"); – Gautam Sheth Nov 1 '16 at 11:15

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.