Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top