0

I am trying to limit visibility to buttons and ability on a user control based on Role on a SharePoint 2010 Site. I get the user from the following code line

SPContext.Current.Web.CurrentUser

Then using a function I found online I check if the user belongs to a certain role

public static bool SPUserHasRole(SPUser spUser, string spRole)
        {
            bool hasRole = false;
            if (spUser != null)
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url, spUser.UserToken))
                {
                    using (SPWeb rootWeb = site.RootWeb)
                    {
                        SPRoleDefinition spRoleDef = null;
                        try
                        {
                            spRoleDef = rootWeb.RoleDefinitions[spRole];
                        }
                        catch 
                        { 
                        }
                        if (spRoleDef != null)
                        { 
                            hasRole = rootWeb.AllRolesForCurrentUser.Contains(spRoleDef); 
                        }
                    }
                }
            } return hasRole;
        }

At first I was thinking maybe on page load I can mess with button visible property set them to true/false based on the corresponding role. However on the user control I have some controls don't have a server side action and I am not able to access the properties from the code behind.

Is there a way to access the properties of the client side controls. Or is my approach wrong and I should be doing this some other way. I am new to web and am very open to any leads in accomplishing the end goal which is to control the availability of the controls within the usercontrol based on the current user role.

Thanks.

1 Answer 1

1

How about placing those client controls inside <div> tags and changing the visibility of the div tag.

1
  • What would prevent user to then through a browser in development mode to execute a java script to change visibility of a div tag or a button for that matter. As I thought about this I have a feeling my approach is flawed. But I do not know of other alternatives. I left out the part about restricting access securely :) Thank you in advance for any suggestionss
    – crossroad
    Aug 15, 2013 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.