none
Show or hide programmatically based on audience

    Question

  • I'm trying to either show or hide a datalist control based on a users Audience.

          AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
    
          string audienceFieldValue = "United States";
    
          if ((AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, false)))
          {
            DataList2.Visible = true;
          }
          else
          {
            DataList2.Visible = false;
          }
    

    I have tried couple different things this is where I'm at now... any help would be appreciated.

    Thanks

     


    sayitfast
    Friday, September 24, 2010 10:13 PM

Answers

  • Can you try this

    Audience audience = audienceManager.GetAudience("United States"

    );

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

     

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

    {

     

    this.Visible = false;

    }

    Hope it helps.

    • Marked as answer by Porter Wang Thursday, October 07, 2010 10:00 AM
    Tuesday, September 28, 2010 5:37 AM

All replies

  • You are on right path, What is your problem?
    Regards, Sandip Patil, .Net,Sharepoint Developer www.worldofsharepoint.com
    Friday, September 24, 2010 10:30 PM
  • I know that the audience exists "United States" and that I'm a member but the datalist will not show for me.

    I comment out the targeting and is shows...


    sayitfast
    Friday, September 24, 2010 10:53 PM
  • Please once again confirm that you are member of that audiences and audienceFieldValue. Also debugg the code snippet.

    Regards, Sandip Patil, .Net,Sharepoint Developer www.worldofsharepoint.com
    Saturday, September 25, 2010 4:09 AM
  • I'm gettting a null reference exception at:

    AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();


    sayitfast
    Monday, September 27, 2010 7:37 PM
  • Hi,

    may be you should have a look at this link http://sharepointquery.blogspot.com/2009/04/webparts-and-audiences-part-1-show-or_27.html

    and please make sure that you have audiences and audience members

    hope this helps


    Warm Regards, Bhushan http://www.passionatetechie.blogspot.com
    Tuesday, September 28, 2010 4:48 AM
  • Can you try this

    Audience audience = audienceManager.GetAudience("United States"

    );

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

     

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

    {

     

    this.Visible = false;

    }

    Hope it helps.

    • Marked as answer by Porter Wang Thursday, October 07, 2010 10:00 AM
    Tuesday, September 28, 2010 5:37 AM