views:

681

answers:

2

In a Publishing site I have web part that has to show news items from the list that has Audience Targeting field. I am using CAML query to retrieve small number of last news items.

Is it possible to specify Target Audience in the CAML query ? If not, how should I do it ? Retrieve all results and than apply filter in a loop ?

I am practically duplicating Content Query Web Part and I need Audience Targeting in my custom web part.

+1  A: 

No, it is not possible to specify audience targeting in a CAML query. I think this has to do with CAML queries being a WSS thing and Audiences being a MOSS Shared Service. What you have to do is to include the audience field in the CAML query, i.e. add a <FieldRef Name='Target_x0020_Audiences'/> to the SPQuery.ViewFields property. Then filter the results code wise by audience on each list item. Use the AudienceManager class to test if the current user is a member of an audience.

Lars Fastrup
A: 

Well i found a workaround for this, i was experiencing an issue when trying to check if the current user was a memeber of the audience for a specific publishing page and what the name of that audience was. Here is the workaround i came up with.


// Run through the pages building the list items
foreach (SPListItem li in pages)
{
  // Get a reference to the publishing page
  PublishingPage p = PublishingPage.GetPublishingPage(li);

  // Make sure the page has been approved
  if (li.ModerationInformation.Status == SPModerationStatusType.Approved)
  {
    // Check if this page has an audience
    if (string.IsNullOrEmpty(p.Audience))
      // Add to everyone list
    else
    {
      // Split the audiences
      string[] Audiences = p.Audience.Split(';');

      // Check each audience to see if this user can see it
      foreach (string audPart in Audiences)
      {
        AudienceManager audienceManager = new AudienceManager();

        // Get a reference to the audience
        // IsGuid is an extenstion method i wrtoe
        if (audPart.IsGuid())
        {
          if (audienceManager.Audiences.AudienceExist(new Guid(audPart)))
            aud = audienceManager.Audiences[new Guid(audPart)];
        }
        else
        {
          if (audienceManager.Audiences.AudienceExist(audPart))
            aud = audienceManager.Audiences[audPart];
        }

        // Ensure we have a reference to the audience
        if (aud != null)
        {

          // store the item in the temp variables
          switch (aud.AudienceName)
          {
            case "All site users":
              // Add to everyone list
              break;

            case "Some List":
              if (audienceManager.IsMemberOfAudience(web.CurrentUser.LoginName, aud.AudienceID))
              {
                // Add to other list
              }
              break;

            case "Other List":
              if (audienceManager.IsMemberOfAudience(web.CurrentUser.LoginName, aud.AudienceID))
              {
                // Add to other list
              }
              break;
          }

        }
      }
    }
  }
}

As you can see its really just a mater of checking if the audience exists by using AudienceManager.Audiences.AudienceExist and the getting a reference to it by just using the default accesor AudienceManager.Audiences[GUID];

Adam