Question

I have this display template which displays certain managed properties from a list, including Created by.

I want Created by to be clickable and take the user to the mysite of who ever created the list item.

However I am confused on how to do this as there doesn't seem to be a managed property for me to do that?

Was it helpful?

Solution

  1. In your display template html, map the managed property 'AuthorOWSUSER'

    'Author'{Author}':'AuthorOWSUSER'
    
  2. Add these javascript within your template (assuming you customized picture on top template, after line 3 or line 3 before OnPostRender :

    var author = $getItemValue(ctx, "Author");
    var personalSiteUrl = window.location.protocol + '//' + window.location.host.replace(".sharepoint", "-my.sharepoint") + "/personal/";
    var authorUserName = Srch.U.getUsernameFromAuthorField(ctx.CurrentItem.AuthorOWSUSER);
    var authorUrl = authorUserName.substring(authorUserName.lastIndexOf('|') + 1).replace(/[^A-Z0-9]/ig, "_");
    var absUserPersonalUrl = personalSiteUrl + authorUrl;
    
  3. Add the following html to your template's html.

    <a class="cbs-pictureOnTopAuthorLink" href="_#= absUserPersonalUrl =#_" title="_#= $htmlEncode(author) =#_">
         _#= author =#_
    </a>
    

P.S If you'd like the link to point to user's profile page instead, replace the '/personal/' with "/Person.aspx?accountname=" in 'personalSiteUrl' variable and use 'authorUrl' variable's value in place of 'absUserPersonalUrl '.

OTHER TIPS

In .js file of Display template, you can call spservices to get mysite url as follows:

//Get user's personal MySite url
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetUserProfileProperties);

function GetUserProfileProperties() {
//Get the current user's account information
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=UserUrl",
    method: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {
        var userUrl = data.d.UserUrl;
        window.console && console.log(userUrl);
    },
    error: function (err) {
        alert(JSON.stringify(err));
    }
});
}

And then user "userUrl" variable in anchor tag around "CreatedBy" field.

Note: To add spServices JS in yor display template, in HTML file, use following snippet.The script block is located just below the first body tag.

<script>
$includeLanguageScript(this.url, "PATH");
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top