Monday, October 12, 2015

Display User Profile Picture in O365 using a Content Search Web Part

If you have ever tried to display a user's profile picture that is stored in their SharePoint Online user profile you will have noticed an issue.  When you configure everything just right and log onto the page for the first time you wont see their picture.  All you see is the wonderful grey box with a question mark like this:

clip_image001

This is a documented issue within SharePoint Online/O365.  From Microsoft:

This issue occurs because the profile image is located on another site collection. You must authenticate with the site collection for the image to be displayed in the Content Search Web Part.

The workaround provided involves browsing to a MySite or OneDrive page within your tenant and then browse back to the page.  Obviously this is not a great user experience.  I was putting together an Employee Spotlight section on a client's new portal's landing page and this workaround was not acceptable.  I did some digging through the internet and stumbled across some pieces to put together to solve this issue.  The solution involved a new display template and update the pictureURL to point to a local source instead of the "My" site collection(s).

1.  Make a copy of whatever display template you want to use and give it a new name.  In this example I am using the Picture with 3 lines on the right.  (Item_Picture3Lines.html) 

2.  Open the downloaded and renamed display template and update the Title.

image

3.  Add a new managed property mapping for WorkEmail  -->  'Work Email'{Work Email}:'WorkEmail'

image

4.  Scroll down to the line that holds the pictureURL variable.  Before:

image

5.  Replace the line -->  var pictureURL = $getItemValue(ctx, "Picture URL");

    With the line -->  var pictureURL = "/_layouts/15/userphoto.aspx?size=L&accountname=" + $htmlEncode(ctx.CurrentItem.WorkEmail);

-------------

   Replace the line -->  var pictureMarkup = Srch.ContentBySearch.getPictureMarkup(pictureURL, 100, 100, ctx.CurrentItem, "cbs-picture3LinesImg", line1, pictureId);

    With the line -->  var pictureMarkup = "<img src='" + pictureURL + "' class='cbs-picture3LinesImg' alt='" + line1 + "' id='" + pictureId + "' onerror='this.parentNode.innerHTML=Srch.ContentBySearch.getNoPictureMarkup(100);' onload='Srch.ContentBySearch.resizeImageToSquareLength(this, 100);'/>";

    After:

image

6.  Save, upload, and PUBLISH  your new display template.

7.  Set your CSWP to use your new display template.

image

8.  Refresh your page and look at some smiling faces!

image

Here is a helpful link to get started with configuring a content search web part in SharePoint.

*Note* If you are on a domain joined PC with a synchronized identity and ADFS this should not be an issue.

Originally PostedDisplay User Profile Picture in O365 using a Content Search Web Part


by Drew Madelung via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment