I have added CSWP to a page which retrieves 5 recent posts from a community site. Now I want to modify display template to show the thumbnail of the user who created the post. Is it possible?
2 Answers
If you're bulding your own custom template, get hold of one of the templates from the '_catalogs\masterpage\Display Templates' and modify it.
Inside ManagedPropertyMapping add an Author property.
Then within your template you could use something like:
ms_outHtml.push(' '
,' <img class="authorImage" src="/_layouts/15/userphoto.aspx?size=S&accountname=', ctx.CurrentItem.Author , '" />'
);
You might also be able to simply map an Author property and use PicureURL of the built in templates from the search web part properties.
-
OK, in the end of ManagedPropertyMapping added ,'Author':'Author' becuase there is a managed property with this name. and added your code in my own display template, published a major version of it, edited CSWP and chose my display template. Nothing appears.– MedesDec 5, 2013 at 8:52
-
it renders this testarbetsrum/_layouts/15/…', firstName lastName , ' but it does not show the image (after full crawl)– MedesDec 10, 2013 at 14:32
-
You're right, I tried the same in the CSWP and it displays the 'Display Name'. I logged the AuthorName in the console, it's simply a string . Had it been an object, we could retrieve the userName from it. /_layouts/15/userphoto.aspx?size=S&accountname= trick works with the username not displayname. However, the AccountName mapped property returns blank (tried w/ Discussion content type)– AveenavDec 18, 2013 at 22:00
(My account is new so I have to 'answer' instead of comment. Don't accept this as the answer since I'm just expanding on Aveenav's suggestion.)
Try using ctx.CurrentItem.Author[0].email So in my TemplateOverrides for Item, I do something like...
return '<img src="/_layouts/15/userphoto.aspx?size=S&accountname=' + ctx.CurrentItem.Author[0].email + '"/>';
... and this works just fine for me. Even with size=S, I needed to force the size with something like style="width:64px; height: auto;"
inside my img tag.