Previewing pages using content targeting with user segments in SharePoint 2013

The 'View as user segment' button highlighted in the Ribbon
Content targeting with user segments in SharePoint 2013 allows you to do display personalized content to your visitors. Unfortunately, SharePoint doesn’t offer you a way to view how those pages would be displayed for the particular audience. See how to easily preview pages with targeted content as a specific user segment in SharePoint 2013.

Targeting content with user segments in SharePoint 2013

When using the new search-driven content publishing model in SharePoint 2013, you can benefit of the content targeting capability. Having defined user segments you can control what content should be displayed to your visitors, based on for example their navigation history or social profile. Through search query rules, configurable through the UI, SharePoint 2013 allows you to control which content should be displayed to which user segments.

Although working with content targeting adds complexity to your website and makes it less predictable, when used correctly, it offers you great added value: by displaying relevant content to your visitors you increase the chance for conversion and increase the exposure of the content on your website.

If you are seriously looking into leveraging SharePoint 2013 content targeting with user segments on your website, the odds are high that you will end up with quite a few rules and user segments. Ideally, before enabling them on your website, you would like to preview them first. This is particularly important on pages where you may have a few areas displaying user targeted content. Unfortunately SharePoint 2013 doesn’t provide you with a mechanism that would allow you to preview how your page is going to look like for a particular user segment.

Previewing pages with targeted content using a specific user segment with Mavention View as user segment

Mavention View as user segment is a SharePoint 2013 solution that allows you to see how a particular page would be displayed to the selected user segment. When on a page all you need to do is to pick the desired user segment and Mavention View as user segment will reload the page showing it just as it would displayed to the selected user segment.

The homepage of the mavention.com website previewed as one of the user segments

Upon installation Mavention View as user segments install a Site Collection Feature that needs to be activated to use the solution on your website.

Mavention View as User Segment Site Collection Feature highlighted on the Site Collection Features page

Upon the activation the View as user segment drop-down list will be added to the Ribbon.

The ‘View as user segment’ drop-down list highlighted in the Ribbon

After you select one of the user segments from the drop-down list the page will reload and will display as if it was viewed by the selected user segment (notice how case study has been replaced with a job offer).

The homepage of the mavention.com website previewed as one of the user segments

Important: The ‘View as user segment’ drop-down list displays only user segments defined on the Site Collection-level. If your user segment isn’t in the list it could be because it’s defined in the Search Service Application or in the Site Search Settings.

Making Content Search Web Parts user segments preview-aware

As mentioned before, SharePoint 2013 doesn’t support previewing pages with user segments out of the box. For the preview functionality to work, all Content Search Web Parts on the page must be aware of the preview capability. There are two ways to do this.

The Mavention View as user segment solution contains an extended version of the Content Search Web Part which is user segments preview-aware. When building your own Content Search Web Parts and implementing user segments capability for your organization you can use the provided Content Search Web Part as a base class for your Web Part. When passing your user segments to the UserSegments property you should check if the preview has been triggered by calling the following code:

protected override void OnLoad(EventArgs e) {  
    base.OnLoad(e);

    if (SelectedUserSegments == null ||
        SelectedUserSegments.Length == 0) {
        // your logic for passing user properties to SharePoint 2013 Search
    }
}

With this check you will ensure that the preview functionality won’t override your user segments logic and vice versa.

The other way to make your Content Search Web Parts user segments preview-aware is to include the preview-logic code directly in the code of your extended Content Search Web Part. Following is the complete code of the Content Search Web Part provided with the Mavention View as user segment solution:

using Microsoft.Office.Server.Search.WebControls;  
using Microsoft.SharePoint;  
using System;  
using System.ComponentModel;  
using System.Web;

namespace Mavention.SharePoint.ViewAsUserSegment.WebParts.ContentSearchWebPartWithPreview {  
    [ToolboxItemAttribute(false)]
    public class ContentSearchWebPartWithPreview : ContentBySearchWebPart {
        protected bool PreviewAllowed { get; private set; }
        protected string[] SelectedUserSegments { get; private set; }

        protected override void OnInit(EventArgs e) {
            PreviewAllowed = SPContext.Current != null &&
                SPContext.Current.Web != null &&
                SPContext.Current.Web.DoesUserHavePermissions(SPBasePermissions.EmptyMask | SPBasePermissions.AddAndCustomizePages | SPBasePermissions.ViewFormPages);

            if (PreviewAllowed) {
                SelectedUserSegments = Utils.GetUserSegmentsFromQueryString(HttpContext.Current.Request.QueryString);
            }

            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e) {
            if (PreviewAllowed &&
                SelectedUserSegments.Length > 0 &&
                AppManager != null &&
                UseSharedDataProvider == false &&
                AppManager.QueryGroups.ContainsKey(QueryGroupName) &&
                AppManager.QueryGroups[QueryGroupName].DataProvider != null) {
                if (AlwaysRenderOnServer) {
                    AppManager.QueryGroups[QueryGroupName].DataProvider.Properties["UserSegmentTerms"] = SelectedUserSegments;
                }
                else {
                    AppManager.QueryGroups[QueryGroupName].DataProvider.BeforeSerializeToClient +=
                        new BeforeSerializeToClientEventHandler(AddPreviewUserSegments);
                }
            }

            base.OnLoad(e);
        }

        private void AddPreviewUserSegments(object sender, BeforeSerializeToClientEventArgs e) {
            DataProviderScriptWebPart dataProvider = sender as DataProviderScriptWebPart;

            if (dataProvider != null) {
                dataProvider.Properties["UserSegmentTerms"] = SelectedUserSegments;
            }
        }
    }
}

First the Web Part checks if the current user has sufficient permissions to preview the page as a specific user segment. Next, selected user segments, if any, are loaded from the query string. For this the public static GetUserSegmentsFromQueryString method is called which you can call directly from your code by referencing the Mavention.SharePoint.ViewAsUserSegment.dll assembly. Then the Web Part passes the selected user properties to SharePoint 2013 Search. User properties are passed in two ways to support both server- and client-side rendering.

Having made your Content Search Web Parts user segments preview-aware you can start using Mavention View as user segment.

Summary

Targeting content with user segments in SharePoint 2013 allows you to display personalized content to your visitors. Unfortunately, when configuring user segments and search query rules, there is no way for you to preview how a complete page is going to be displayed for a particular user segment. Mavention View as user segment allows you to view every page in your site just as it would be displayed for the particular user segment.

Download: Mavention View as user segment (9KB, WSP)

Comments

comments powered by Disqus