Audience Object Model Overview
Microsoft Office SharePoint Portal Server 2003 supported targeting based on complied rules-based audiences. In addition to rules-based audiences, Microsoft Office SharePoint Server 2007 supports targeting based on SharePoint groups and distribution list (DL) memberships. Office SharePoint Server 2007 also provides the ability to target content to the list item level, rather than just to the list level.
Note
Office SharePoint Server 2007 also provides a Web service that finds all the SharePoint sites that are targeted to a particular user. For more information, see How to: Use the Published Links Web Service.
Working with the New Namespace for the Audience Object Model
In SharePoint Portal Server 2003, the Microsoft.SharePoint.Portal.Audience namespace is used to work with audiences. In Office SharePoint Server 2007, the Audience object model is implemented in the Microsoft.Office.Server.Audience namespace in Microsoft.Office.Server.dll. This new namespace provides object model support for the new functionality in Office SharePoint Server 2007.
The Microsoft.SharePoint.Portal.Audience namespace still exists for backward compatibility. Your applications and tools written in SharePoint Portal Server 2003 using the Microsoft.SharePoint.Portal.Audience namespace should still work in Office SharePoint Server 2007. However, note that they may not behave as expected, specifically with the new features. For example, if you use the APIs in Microsoft.SharePoint.Portal.Audience, you can only work with rules-based audiences. Therefore, if you are writing new applications or upgrading old applications, you should use the new Microsoft.Office.Server.Audience namespace in Microsoft.Office.Server.dll. All the classes in the Microsoft.SharePoint.Portal.Audience namespace have been deprecated, and you will get a warning every time you use a class or method in the Microsoft.SharePoint.Portal.Audience namespace.
Getting the AudienceManager Object in Office SharePoint Server 2007
The AudienceMananger object is the top-level object in the Audience object model. You can get an AudienceManager object in Office SharePoint Server 2007 by using the syntax shown in the following code example. Replace servername with an actual value and add any code that you may need before using this example.
using (SPSite site = new SPSite("https://servername"))
{
ServerContext context = ServerContext.GetContext(site);
AudienceManager audManager = new AudienceManager(context);
//your code here
}
Personalized Portals
The following code example shows how to target list items to a particular audience.
AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
foreach (SPListItem listItem in list.Items)
{
// get roles the list item is targeted to
string audienceFieldValue = (string)listItem[k_AudienceColumn];
// quickly check if the user belongs to any of those roles
if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue,
false))
{
// is a member
}
else
{
// not a member
}
}