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 need to hide some elements of a webpart and only show them to some specific grups of users. How can I do this ? I think it could be done with an If or Switch block, using the name of the grup of users which should see this Webpart controls, but how can I do it? Can anyone give some guidance on how to reach this goal?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Design the ascx carefully, the portion you want to show/hide declare the part as runat="server", example -

<table>
  <tr id="trShowHide1" runat="server">
  //some content
  </tr>
  <tr id="trShowHide2" runat="server">
  //some content
  </tr>
</table>

and in CreateChildControls() event (or if you think any other event suits you based on your requirement, but i prefer CreateChildControls) get the current user (you can use SPContext.Current.Web.CurrentUser), by this point you will have one SPUser object of current user, check the group and/or other attribute, you will need spUser.Groups attribute to check for groups current user is member of, now check for required group in SPGroupCollection object (spUser.Groups returns the group collection object), example

if(user is in "Group1")
{
  trShowHide1.Visible=true;
  trShowHide2.Visible=false;
}
else
{
  trShowHide1.Visible=false;
  trShowHide2.Visible=true;
}

you can pretty much construct your own code from the sample i guess

share|improve this answer
    
Thank You very much Diptarag :) –  Grzegorz Z Nov 23 '12 at 10:53
    
I woried that i'm made not good code. –  Grzegorz Z Dec 18 '12 at 11:27
add comment

Actually the best option is to use the Security Trimming controls, as in http://fusionovation.com/post/2008/09/18/security-trimmed-controls-in-sharepoint.aspx

As you would immediately realize is they do not support SPGroups, so find here a solution doing exactly this http://www.dhirendrayadav.com/2011/08/extending-sharepoint-security-trimmed.html

I find it a much better option for long term, C:>Marius

share|improve this answer
add comment

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.