Dynamic Content Filtering on SharePoint Web Part Pages using Metadata & Audiences

Posted by GeorgeH on October 20, 2011

In the article below, two methods to target content to different audiences on SharePoint Sites based on meta data are explained.  Both methods assume that the targeting to each audience is being controlled by the same criteria across the entire site collection using a new global "visibility" column, but this is optional.

Adding a new audience column this may not be required in all cases when the standard audience targeting functionality can be utilised.  For example, if the content displayed to various audiences is not displayed using criteria that is associated with the audience, a new "visibility" column is not required.  In this case, the filter criteria may be specific to the content type or list/library, such as completion status (Users in a management group could see a summary view of completed tasks, where users in the action group could see active tasks by default).

The new "visibility" column would come in handy when you want to give users control over the visibility.  This would not affect the permissions and security of items in document libraries and lists as audience targeting is separate.  Users in a particular audience can still access content that is displayed to other audiences only, it is just not visible by default.  Using this method would be an attempt at improving relevance for different groups of users accessing the same sites and resources, while at the same time allowing them access to content that is not deemed relevant for some circumstances.  As the targeting would be controlled by the value in a meta data column instead of the Audience Targeting field when modifying the web part, users can easily set or change who sees specific content.

In the examples below, the filter criteria passed to Web Parts on pages across the Site Collection comes from Query String data, which is set using Javascript based on a user's audience.  The value would be one of the choices in a new global "visibility" (multiple choice) column containing a separate choice for each audience.

Adding the global multiple choice "Visibility" column:

In this article, both examples use a column that has been made global using the Item Content Type.  The result is that all existing lists and content types will have the new column in all sites from the site collection. More information and instructions to add a global column to all content types in SharePoint if required.

 

Applying the audience targeting configuration to Web Part pages using the global column:Below are two methods that use the value in the new column to filter content based on audiences.

Method 1 - Duplicate Web parts for each Audience with pre-defined filter criteria -Targeted to each audienceThis method uses multiple web parts on each page that is to display varied content for each audience.  Each web part has different filter criteria to create a filtered copy of the content for each audience.  Each web part is targeted to one audience, resulting in the page displaying different content that is filtered based on a user's audience.

Pros:

  1. Manageable if small number of sites and Audiences
  2. No redirects to include query string params
  3. No Query String Filter Web Part required, or Data View customisation.

Cons:

  1. Development required to include additional audiences:  All pages displaying dynamically filtered content require modification to include additional filtered list/data views.  This can easily become a significant amount of work if there are a large number of lists displayed on pages and/or a large number of audiences seeing their own content.

How To:There are a number of ways that an "audience" column can be added to lists, libraries and content types across a site collection.  In this case, the column used to determine the audience is a "Visibility" column created using the techniques described in the section relating to global columns (above).  Once the column has been added to lists and content types, you can the configure the Web Part pages to filter and display the content for each audience.

When different content from a list is to be displayed to different audiences based on Meta Data, you need to duplicate the web parts, and target one to each audience that requires custom content.  The Web Part for each audience can the have is't own filter criteria applied, which will not affect what is visible to users in other audiences, as they will be seeing a different web part.

Assuming you have a "Visibility" column added to each list that requires targeting, a simple process to configure a web part page could be as follows:

  1. Add a document library or list web part to the page (one of the lists or libraries on the current site)
  2. On the Modify Web Part Properties page, select the Modify Current View option.
  3. Under the Filter Criteria, add the global column and value that corresponds to one of the audiences (using the contains operator).  Press Ok to save the changes.
  4. In the Advanced section of the Web Part Properties panel, enter the audience that the Web Part should be displayed to into the "Target Audiences" field.
  5. Repeat the above 4 steps fore each audience, using the corresponding filter criteria.

Method 2 - Single Web Part for all audiences - Filter based on audience via Query String ParameterThis method is probably the one that I would recommend, as it allows for the solution to be more dynamic and requires much less ongoing maintenance.  Instead of a having a duplicate Web part configured for each audience, only one web part is used.  This is then filtered dynamically using a query string parameter that contains a value that determines the users audience.  The query string parameter can be added to the the url of each page using JavaScript (more details below).

Pros:

  1. Easy to add additional audiences
  2. Minimal page maintenance for content (single list with filter criteria via query string, instead of duplicate lists targeted with predefined filter criteria)

Cons:

  1. Targeted Redirect Required on all pages with dynamically filtered content.
  2. Slower page load times
  3. Increased number of requests to load a page
  4. Filter Web Part Required on all pages, or parameter need to be configured separately on all data view web parts

Data View Custom Filter Parameter or Query String Filter Web Part

When a Data View Web Part is used to display content from SharePoint lists, the query string filter parameter can be set using the "Filter Criteria" dialog when modifying the Web Part using SharePoint Designer. Alternatively, you can use a connection to the Query String Filter Web Part (available in Enterprise Edition (MOSS) and other third party solutions).  If only Data View Web Parts are used on pages (no List View Web Parts), then the Query String Filter Web Part may not be required at all as long as you are happy to configure the filter criteria from the a query string parameter for each Data View.

Setting the Query String Parameter Using JavaScript and Content Editor Web Parts:As the List or Data View web parts on each page with audience targeting need to get the filter criteria from the query string parameter, you need to make sure that the parameter is available with the correct value when a user is on the page.  To do this, JavaScript can be used to redirect the user to the current page with the correct parameter and value if it is not originally in the address used to access the page.  To improve performance of the site by reducing the number of redirects, JavaScript can also be used to dynamically update all internal (pointing to same site collection) links on a page to include the parameter and value corresponding to a users audience.

The query string parameter can be set dynamically using JavaScript after the page has loaded.  This would involve redirecting the user back to the current page, including a query string parameter with a value that corresponds to the users audience.  If the query string parameter and value is found in the page url, then no redirect is required.  The redirect javascript would only need to be implemented on the pages that require the audeince targeting using the global field.

The JavaScript would vary for each audience, as a different value needs to be set to the query string parameter for each.  This can be achieved by adding the javascript to a function in the global js file linked to every page via the site template (Master Page).  A separate Content Editor Web Part for each audience would be added to each page that requires audience targeting.  The script in each content editor web part would call the global function including the value corresponding to the user's audience that the function then uses as the value for the query string parameter when construing the redirect url. The Content Editor Web Part for each audience would be targeted to the one specific audience only.  The result is that the javascript associated with a users audience will be rendered on the page (the first would take precedence if multiple are visible).

The following reference is to a set of articles relating to JavaScript Query String Manipulation (TOC). The relevant functions in the set are:

Javascript Add Query String Parameter - Redirect the current user to the same page including a query string parameter and value.  A call to this function can be placed in a Content Editor Web Part targeted to one audience (repeat for each audience).  The call will execute the addParameter() function, which will redirect the user to the same page including the correct query string parameter and value corresponding to their audience.  If the parameter already exists in the url, no redirect takes place.

Javascript Add query string parameter dynamically to some or all links on the current page - This function can be used to add a query string parameter and value to hyperlinks on the  current page dynamically.  The function has been configured to allow an array of domains/hosts to be passed, which is used to determine if a link should contain the parameter or not.  If no array is supplied, only links to the current site are updated to include the parameter.  Again, a call this function can be added to a Content Editor Web part targeted to one audience (for each audience).

Rating

5/5

Reviews

There are currently no comments or reviews.

Submit a review:

Login required.