SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 1 down vote accepted

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

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

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")

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

Actually the best option is to use the Security Trimming controls, as in

As you would immediately realize is they do not support SPGroups, so find here a solution doing exactly this

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

share|improve this answer

Your Answer


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.