Task
When you add List View Web Part to a page in SharePoint and would like it to display e.g., all pages in the Pages library, and provide links to items, you will run into issue where you're not able to link to the item using the title of the item. You can only link to the item usin the file name, which will look bad, and include dashes, remove special characters, etc.
In SharePoint 2010, you would modify the XSLT of the XSLTListViewWebPart using SharePoint Designer (like
here), but in SharePoint 2013 you can use new feature in SharePoint 2013, called
JSLink to do the same.
Solution
Use JSLink to override how Title column is rendered. But instead of actual title column, you want to use the LinkTitle or LinkTitleNoMenu columns. This is because if you only use Title, the JSLink will not have the item URLs available – apparently optimization within SharePoint not to return item properties that are not used.
In the override, add link tags around the title. I do like this JSLink method better compared to the old XSLT SharePoint Designer way as you can reuse the JavaScript file and only reference that in the Web Parts when required.
- In a location of your choice (such as SiteAssets library of your site, or in _catalogs/masterpage), create new JavaScript file.
- In the file, add the code below
- In your web part, reference this JavaScript file in the JSLink setting, like ~site/SiteAssets/news_ui.js, or ~sitecollection/_catalogs/masterpage/news_ui.js
(function () {
// Initialize the variables for overrides objects
var overrideCtx = {};
overrideCtx.Templates = {};
//List of default Object.keys(ctx.CurrentItem)
//ID,PermMask,FSObjType,HTML_x0020_File_x0020_Type,ContentType,File_x0020_Type,
//File_x0020_Type.mapapp,HTML_x0020_File_x0020_Type.File_x0020_Type.mapcon,
//HTML_x0020_File_x0020_Type.File_x0020_Type.mapico,serverurl.progid,
//ServerRedirectedEmbedUrl,File_x0020_Type.progid,File_x0020_Type.url,
//FileRef,FileLeafRef,CheckoutUser,CheckedOutUserId,IsCheckedoutToLocal,Title,
//Created,Created.FriendlyDisplay,firstRow
overrideCtx.Templates.Fields = {
'LinkTitleNoMenu': { 'View' : '<a href="<#=ctx.CurrentItem.FileRef#>"><#=ctx.CurrentItem.Title#></a>' }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx); })();
NOTE! If you still want to use only Title column, you need to include
some other field in the View you're using that contains URL in order for this to work, e.g., Icon and Name fields.
Technorati Tags:
SharePoint 2013