I am trying to find whether the current user is a member of a specific audience and used the below code however it returns "false" though the user belongs to the "testing" audience.


what would be the issue?


We would like to redirect the user based on the audience group.

If Userx is a member of the audience "Testing", he will be redirected to "newWeb" otherwise to "oldWeb".

DelegationControl loads a user control, validation & redirection is taken place on the user control.


Tried the below code however getting the "Access denied" error message while trying to get the audience - "Audience audience = audienceManager.GetAudience("Testing");", have verified and found that the app., pool has full control on the user profile application.

        else {  
                SPUser currentUser = SPControl.GetContextWeb(Context).CurrentUser;  
                    //HttpContext httpCtxt = HttpContext.Current;  
                    //HttpContext.Current = null;  
                    Microsoft.Office.Server.Audience.AudienceManager audienceManager = new 
                    **Audience audience = audienceManager.GetAudience("Testing");**  
                    //HttpContext.Current = httpCtxt ;  
                    if ( audience.IsMember(currentUser.LoginName))  
  • If you still see unexpected results, look for an exception right about the same time it returns false. I realize SharePoint returns false when there is an unexpected error encountered. – Ashish Patel Dec 21 '11 at 3:31
AudienceLoader objAudLoader = AudienceLoader.GetAudienceLoader();
AudienceManager.IsCurrentUserInAudienceOf(objAudLoader, audience, false)

The AudienceLoader belongs to Microsoft.Office.Server.Audience namespace and documented here: http://msdn.microsoft.com/en-us/library/microsoft.office.server.audience.audiencemanager.iscurrentuserinaudienceof.aspx


If you still see unexpected results, look for an exception right about the same time it returns false. I realize SharePoint returns false when there is an unexpected error encountered.

  • Tried with the below code however it does not work as expected...returns "false" in all the cases... Microsoft.Office.Server.WebControls.AudienceLoader objAudienceLoader = Microsoft.Office.Server.WebControls.AudienceLoader.GetAudienceLoader(); if ( Microsoft.Office.Server.Audience.AudienceManager.IsCurrentUserInAudienceOf(objAudienceLoader, "Testing", false)) { Page.Response.Redirect("redirectWebNew/person.aspx"); } else { Page.Response.Redirect("redirectWebOld/person.aspx"); } – Karthikeyan Dec 16 '11 at 15:03
  • Testing is the name of the audience created in the User profile application in SharePoint 2010.....and the user is a member of the audience "Testing". – Karthikeyan Dec 16 '11 at 15:04
  • Have you resolved it yet? See my updated answer. – Ashish Patel Dec 21 '11 at 3:27
  • You have to pass the Id of the "Testing" audience. If you test "Testing" as audience, you'll never get true result. – jaloplo Dec 22 '11 at 8:13
  • yes, it works with the id. I want this part of the code should be executed before processing any other code. Now, the is inserted using a delegate control with the sequence number of 96. Is there anything can be done so that it will fire up first? – Karthikeyan Dec 26 '11 at 4:53

Step 1

The AudienceLoader class is just a cache of the user's audience information and so it can sometimes give some misleading results. Try using the Audience.IsMember function instead:

Audience audience = audienceManager.GetAudience("Testing");
SPUser currentUser = SPControl.GetContextWeb(Context).CurrentUser;

if (audience.IsMember(currentUser.LoginName))
    // Redirect user to newWeb
    // Redirect to oldWeb

Here are some other supporting cases:



Step 2

Since changes made to audiences and audience rules are not reflected in the audience membership until it has been compiled, you will need to recompile your audiences in Central Administration before programatically performing the checks above. See http://technet.microsoft.com/en-us/library/cc263278.aspx


Try AudienceManager.IsMemberofAudience


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.