Tuesday, December 14, 2010

WSS 3.0 “Audiences”

A client recently had a requirement to hide web parts that certain Users do not have access to. Typically I would try to avoid all of this by including this requirement while developing Site Governance, but unfortunately Governance was not in the project scope nor were the dollars… So this left me to brainstorming various ways to go about appeasing my client. The most obvious was to accomplish this is by using Audiences, but unfortunately this is only available in MOSS and they were running WSS. The next option was to utilize SPSecurityTrimmedControl tag. This seemed like a good idea except it only trims based on a permission set and still displays web parts to doclibs/lists that some groups didn’t have access to... The final solution to this problem I decided on was to run server sided code on the page…  Here here a breakdown of the process and the code used:

Initially you must include your page in the web.config’s <pageparserpath></pageparserpath> (if you do not you will get an error regarding page not allowing code blocks).

Web.Config Example:

pageparserpath

Next you will need to create a new web part zone and move each web part that you wish to control, and initially set the visible property to false.

Zone Example:

webpartzone

Now we are ready to move on to the page code.The first part of the code retrieves current users SP Group Memberships and checks whether user is a Site Collection Admin or not.

<script runat="server" type="text/c#">
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        string spGroups = "";
        string isAdmin = "";
    bool admin= SPContext.Current.Web.UserIsWebAdmin;
        foreach (object o in SPContext.Current.Web.CurrentUser.Groups)
            {        
        isAdmin = admin.ToString();
        spGroups = o.ToString();
        System.Web.UI.WebControls.WebParts.WebPart control=null;

The next part of the code checks to see if the currently user is part of any of the declared SharePoint groups and in return changes the specified web part zone (web part zone ID) visible on the page.

switch (spGroups)
           {
               case "SharePoint Site Owners":
                       g_F915659D5D314C5094F0D177B93063F4.Visible=true;
                   break;
                case "SharePoint Site Members":
                      g_091EC81912CE4EE8AF93EE781B502B80.Visible=true;
                   break;
               default:
                   break;
           }

The last part of the code is thrown in to allow Site Collection Administrators to open all web part zones regardless of membership.

            switch (isAdmin)
            {
                case "True":
                       g_F915659D5D314C5094F0D177B93063F4.Visible=true;
                       g_091EC81912CE4EE8AF93EE781B502B80.Visible=true;
                    break;
                default:
                    break;
            }

            }
    }
</script>

And thats it! Put it all together and you have a method to apply “Audiences” to web parts on a page.