Audience Targeting in Windows SharePoint Services 3.0, based on AD Groups

WSS does not have a user profile service and does not allow any kind of native targeting of content to users in different groups. This is one of the more serious limitations of WSS, especially for corporate intranets, where pages might need to be customized for users in different offices. MOSS, on the other hand, allows for the creation of audiences and easy targeting of content. As is typically the case with WSS, it is possible to achieve this functionality by writing code.

One way to do this is by adding a reference in your code to “System.DirectoryServices,” which allows you to query Active Directory. With that class, you can compare the current user to an Active Directory group’s membership collection, and add logic based on whether or not the user is in the group. If you plan to edit an aspx page directly, you will also have to add a page parser path in web.config, so that the code in the page will run. Rather than editing the page directly, it is often preferable to create a control or web part for security and manageability reasons.

If editing an aspg page directly in SharePoint Designer, just add the following line to the top of the page, so that the correct assembly is referenced.

<%@ Assembly Name="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>

Then add a code block into the page that gets the current user and compares it to the group membership. The following example uses VB.Net and sees if the current user is in the “NY Staff” group. If so, then the script redirects to the page “NY.aspx.” Rather than redirecting, you could also add code to write out customized content based on the membership information.

               <%

    Try
   'Get group membership for current user
        Dim DomainUser As String = Replace(User.Identity.Name, "", "/")
        Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)
        Dim MembersCollection As Object 'Underlaying is a IADsMembers interface
        MembersCollection = ADEntry.Invoke("Groups")
        Dim group As Object 'IADsGroup interface
        Dim vFound As Boolean = False
        For Each group In MembersCollection
            If LCase(group.Name) = "ny staff" Then
                vFound = True
                Exit For
            End If
        Next
        'Do something if group is found
        If vFound Then
            Response.redirect("NY.aspx")
        End If
      Catch ex As Exception
        'response.write(ex.message)
      End Try
%>