Item Level Audience targeting in XSLTListViewWebPart

You can enable “Audience Targeting” in list level in SharePoint 2010, When you enable the Audience Targeting a file called “Target Audience” is added to the list and you can populate this field with target audience for every item to show the items only to the specified audience. But the audience filtering doesn’t apply to XSLTListViewWebPart and only applies to “ContentByQueryWebpart”. That means, you cant expect the XSLTListViewWebPart to filter the items based on audience while you are viewing the items in the list view or through XSLTListViewWebPart in any web part page, This item level audience filtering works only when you use the “ContentByQueryWebpart” and configures it to apply audience filtering.

This behavior is because item level audience filtering is implemented through “CrossListQueryCache” and “CrossListQueryInfo” API and ContentByQueryWebpart uses them to query against the sites and lists, Hence able to apply the item level audience filtering in the ContentByQueryWebpart, But the XSLTListViewWebPart renders the item based on view (SPView) which fetches the item based on the “Query” (SPQuery) element of view. In Short, Item level audience filtering cannot be applied for XSLTListViewWebPart.

However, there is a  work around to filter the list items based on audience in the XSLTListViewWebPart:-

1.    Construct the CrossListQueryInfo and CrossListQueryCache to fetch the items from the list
2.    The result set after executing the CrossListQueryInfo would be “DataTable” which cannot be fed to XSLTListViewWebPart directly, So need to loop through the “rows” in the datatable and get the “Target Audience” field value
3.    Construct the CAML query on the fly based on the “Target Audience” field value in the result data table and apply the query to the current view using XMLDefinition property of XSLTListViewWebPart.
4.    Implement the above steps in a hidden control/delegate/WebPart and it to the “AllItems.aspx” view page or to any other web part page which has the XSLTListViewWebPart.

Deploy this WebPart to the site and add the Hidden WebPart  to the view page or to the web part page in which you have the XSLTListViewWebPart.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s