Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
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 '13 at 14:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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