Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

Microsoft.Office.Server.Audience.AudienceManager.IsCurrentUserInAudienceOf("Testing",false)

what would be the issue?

Update:

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

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

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

Update-2:

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;
SPSecurity.RunWithElevatedPrivileges(delegate
{
//HttpContext httpCtxt = HttpContext.Current;
//HttpContext.Current = null;
Microsoft.Office.Server.Audience.AudienceManager audienceManager = new Microsoft.Office.Server.Audience.AudienceManager(SPServiceContext.Current);
Audience audience = audienceManager.GetAudience("Testing");
//HttpContext.Current = httpCtxt ;
if ( audience.IsMember(currentUser.LoginName))
{
Page.Response.Redirect("http://www.redirectNew.com");
}
else
{
Page.Response.Redirect("http://www.redirectOld.com");
}
});
}

share|improve this question
    
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

3 Answers 3

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

UPDATE

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.

share|improve this answer
    
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(objAu‌​dienceLoader, "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

Try AudienceManager.IsMemberofAudience

http://msdn.microsoft.com/en-us/library/ms550183.aspx

share|improve this answer

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
}
else
{
    // Redirect to oldWeb
}

Here are some other supporting cases:

http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/ea67d79c-3ff4-4212-b627-dd6045cd0402

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/71b41b3c-9117-49f8-95a4-583be687505c/

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

share|improve this answer

Your Answer

 
discard

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.