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?
Design the ascx carefully, the portion you want to show/hide declare the part as runat="server", example -
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
you can pretty much construct your own code from the sample i guess
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