moopoo.net
Fishing, technology and anything else

Programmatically checking if a user belongs to an SharePoint Audience

December 9th, 2011 by Matt

The following code will check if a user belongs to a given Audience in SharePoint

using (SPSite elevatedSite = new SPSite(Microsoft.SharePoint.SPContext.Current.Site.ID))
{
 
    SPServiceContext serviceContext = SPServiceContext.GetContext(elevatedSite);
    AudienceManager audManager = new AudienceManager(serviceContext);
    Audience audience = audManager.GetAudience("AudienceToCheck");
 
    SPUser user = SPControl.GetContextWeb(Context).CurrentUser;
 
    if (audience.IsMember(user.Name))
    {
          // do some stuff
     }
}

Posted in SharePoint

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.