none
Determine if current user is part of audience

Answers

  • Try this.

     Audience audience = audienceManager.GetAudience("isManager");

    SPUser user = SPControl.GetContextWeb(Context).CurrentUser;

     

    if (!audience.IsMember(user.LoginName))

    {

     

    this.Visible = false;

    }

    Hope it helps.

    • Marked as answer by Porter Wang Monday, October 04, 2010 5:50 AM
    Tuesday, September 28, 2010 5:30 AM

All replies

  • Please take a reference of below code sinppet

    using (SPSite site = new SPSite("http://myserver"))
    {
    	ServerContext context = ServerContext.GetContext(site);
    
    	AudienceManager audManager = new AudienceManager(context);
    
    	AudienceCollection audiences = audManager.Audiences;
    
    	SPList docList = SPContext.Current.Web.Site.RootWeb.Lists["Your Library"];
    
    	AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
    
    	foreach (SPListItem listItem in docList.Items)
    
    	{
    		string audienceFieldValue = (string)listItem["Target Audiences"];
    
    		//Check audiences
    
    		if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader ,audienceFieldValue, false))
    		{
    
    			//To do
    
    		}
    	}
    
    }
    


    Regards, Sandip Patil, .Net,Sharepoint Developer www.worldofsharepoint.com
    Tuesday, September 28, 2010 4:13 AM
  • Hi sayitfast,

    You can use below classes.

    AudienceManager Manager = new AudienceManager(ServerContext.Current);

    AudienceCollection audiences = Manager.Audiences;

    AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();

    Refer this link for more info:

    http://www.sharepointdev.net/sharepoint--development-programming/checking-audiences-using-audiencemanager-audienceloader-in-console-application-49440.shtml

    Hope it helps you


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
    Tuesday, September 28, 2010 4:18 AM
  • both of the examples above work great for filtering list items... and I am able to filter list items without an issue.

    What I cannot seem to figure out is  if a user is a member of a specific audience.

    I have a web part that does filter list items based on audience.... that works no problems.

    Problem is a section of this web part should not be visible if a user is not a member of the "isManager" audience.

    I thought this would work:

     AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
    
     string audienceFieldValue = "isManager";
    
     if ((AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, false)))
     {
     //visible yes
     }
     else
     {
     //visible no
     }
    
    

     

    but I get a null ref exception on AudienceLoader.GetAudienceLoader();

     

     


    sayitfast
    Tuesday, September 28, 2010 4:41 AM
  • Hi sayitfast,

    Thanks for reply me.

    Have you set the target audience in webpart?? because while designing the webpart, you could specify the target audience for this webpart. In this case, the webpart would be rendered only if the current-user is part of the target audience. In all other cases, the webpart would not be visible / rendered.

    Please confirm this.


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
    Tuesday, September 28, 2010 4:48 AM
  • you can have look at http://sharepointquery.blogspot.com/2009/04/webparts-and-audiences-part-1-show-or_27.html

    hope this helps


    Warm Regards, Bhushan http://www.passionatetechie.blogspot.com
    Tuesday, September 28, 2010 4:50 AM
  • Try this.

     Audience audience = audienceManager.GetAudience("isManager");

    SPUser user = SPControl.GetContextWeb(Context).CurrentUser;

     

    if (!audience.IsMember(user.LoginName))

    {

     

    this.Visible = false;

    }

    Hope it helps.

    • Marked as answer by Porter Wang Monday, October 04, 2010 5:50 AM
    Tuesday, September 28, 2010 5:30 AM