3

I used the display template from Elio Struyf to create a datatable "format" with my results.

http://www.eliostruyf.com/table-layout-display-template-with-managed-property-sorting/

However its my requirement to show the List or Library Name as one of the columns together with the link.

The link its easy, as its only the "parent link" managed property.

However, I dont know how to get the List Name

My code is as follows:

This code is from Elio, with a slight modification to match my needs:

<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head>
    <title>List Table Item</title>

    <!--[if gte mso 9]><xml>
    <mso:CustomDocumentProperties>
    <mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
    <mso:ManagedPropertyMapping msdt:dt="string">ListID','ListItemID','ContentTypeId','Link URL':'Path','Property 1':'Title','Property 2':'','Property 3':'','Property 4:'','Property 5':'','Property 6':'','Property 7':'','Property 8':'','Property 9':'','Property 10':'','FileExtension','SecondaryFileExtension'</mso:ManagedPropertyMapping>
    <mso:MasterPageDescription msdt:dt="string">This is the item row template, use this template in combination with the List Table Control template.</mso:MasterPageDescription>
    <mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>
    <mso:TargetControlType msdt:dt="string">;#Content Web Parts;#</mso:TargetControlType>
    <mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>
    </mso:CustomDocumentProperties>
    </xml><![endif]-->
</head>

<body>
    <div id="TwoLines">
        <!--#_
        // Give up your names for the properties in the following array
        var propertyNames = {"Property 1":"File Name","Property 2":"Last modified by","Property 3":"Last modified date","Property 4":"Content type","Property 5":"Library"};

        var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "_tableitems_");

        var linkURL = $getItemValue(ctx, "Link URL");
        var libraryLink = $getItemValue(ctx, "ParentLink");

        linkURL.overrideValueRenderer($urlHtmlEncode);
        var iconURL = Srch.ContentBySearch.getIconSourceFromItem(ctx.CurrentItem);

        var listID = $getItemValue(ctx, 'ListID');
        var listItemID = $getItemValue(ctx, 'ListItemID');
        var ContentTypeId = $getItemValue(ctx, 'ContentTypeId');

        var rowId = encodedId + "row";
        var editCellId = rowId + "_cell";

        var currentItemIdx = ctx.CurrentItemIdx + 1;
        var alternating = currentItemIdx % 2 === 0 ? "ms-alternating" : "";




        if (currentItemIdx === 1) {
        _#-->
        <thead>
            <tr>
                <!--#_
                // Show the file icon
                _#-->
                <th class="ms-vh-icon ms-minWidthHeader">
                    <img width="16" height="16" border="0" src="/_layouts/15/images/icgen.gif">
                </th>
                <!--#_

                // Show the properties that aren't empty
                for(var i = 1; i <= 10; i++) {
                    var property = $getItemValue(ctx, String.format("Property {0}", i));
                    if(property.managedPropertyName !== String.format("Property {0}", i)) {
                        var propertyName = typeof propertyNames[property.propertyLookupName] === "undefined" ? property.managedPropertyName : propertyNames[property.propertyLookupName];
                _#-->
                <th class="ms-vh2">
                    _#= propertyName =#_                     
                </th>
                <!--#_
                    }
                }
                _#-->
            </tr>
        </thead>
        <tbody>
            <!--#_
            }
            _#-->
            <tr id="_#= rowId =#_" class="_#= alternating =#_ ms-itmHoverEnabled ms-itmhover">
                <td class="ms-cellstyle ms-vb-icon">
                    <!--#_ if(!iconURL.isEmpty) { _#-->
                    <img src="_#= iconURL =#_" />
                    <span id="_#= editCellId =#_"></span>
                    <!--#_
                    } else {
                    _#-->
                    <img width="16" height="16" border="0" src="/_layouts/15/images/icgen.gif">
                    <!--#_
                    }
                    _#-->
                </td>

                <!--#_
                // Show the properties that aren't empty
                for(var i = 1; i <= 10; i++) {
                    var property = $getItemValue(ctx, String.format("Property {0}", i));
                _#-->
                <td class="ms-cellstyle ms-vb2">
                    <!--#_
                        if(!property.isEmpty)
                        {
                            var value = i === 1 ? String.format("<a href='{0}' class='ms-listlink'>{1}</a>", linkURL, property) : property;
                            var value = i === 5 ? String.format("<a href='{0}' class='ms-listlink'>{1}</a>", libraryLink, 'Library') : property;
                    _#-->
                    _#= value =#_
                    <!--#_
                            if (i === 1 && !listID.isEmpty && !listItemID.isEmpty && !ContentTypeId.isEmpty) {
                                var viewPropUrl = String.format('/_layouts/15/listform.aspx?ListId={0}&ID={1}&ContentTypeID={2}&Source={3}', listID, listItemID, ContentTypeId, window.location.href.split('#')[0]);
                    _#-->
                    &nbsp;&nbsp;
                    <span title="Open Menu" class="NotificationDiv ms-ellipsis-a" rel="_#= viewPropUrl =#_" ref="_#= property =#_" path="_#= linkURL =#_" style="cursor: pointer; margin-top: 3px; float: right;">
                        <img alt="Open Menu" src="/_layouts/15/images/spcommon.png" class="ms-ellipsis-icon">
                    </span>
                    <!--#_
                            }
                        }
                    _#-->
                </td>
                <!--#_
                }
                _#-->
            </tr>
            <!--#_
            if (currentItemIdx === ctx.CurrentGroup.RowCount) {
            _#-->
        </tbody>
        <!--#_
        }
        _#-->
    </div>
</body>
</html>
3

EDIT

Creating var

var listPath = ctx.CurrentItem.Path; //Get Path
if (listPath.indexOf("Lists/") >= 0) {  //If contains List/
var afterLists = listPath.substring(listPath.lastIndexOf("Lists/")+6); //get url after "Lists/"
var listTitle = afterLists.split('/'); // split 
listTitle[0] = listTitle[0].replace("_", " "); //get list title and replace _ with " "
listTitle = listTitle[0]; // set list title in var
 }
else  { //no Lists/ in path
listPath = ""; //set empty
listTitle = listPath;
}

Call it this way

<!--#_
if(listTitle!=null && !listTitle.isEmpty && listTitle!="")
{
_#-->
<div class="ListTitle">List Title: _#= listTitle =#_ </div>
<!--#_
}
_#-->

Edit2

For Document library you can add after if one else if for that subsite

else if (listPath.indexOf("ces/") >= 0 && listPath.indexOf("Lists/") < 0) {  //If contains ces/ without lists
var afterLists = listPath.substring(listPath.lastIndexOf("ces/")+4); //get url after "ces/"
var listTitle = afterLists.split('/'); // split 
listTitle[0] = listTitle[0].replace("_", " "); //get list title and replace "_" with " "
listTitle = listTitle[0]; // set list title in var
}
1

Your code already have listId

var listID = $getItemValue(ctx, 'ListID');
    var listItemID = $getItemValue(ctx, 'ListItemID');

So you can use ecmascript here to get the list title.

An alternative way would be:

  1. Create an column in list having text as List Title.
  2. Start crawl.
  3. After finishing of crawl, a crawl property will be generated corresponding to custom column.
  4. Map that crawl property to any existing Managed property or create a managed property and map it to crawl property.
  5. Refer the managed property in your display template.
  6. It will give you list name
  • well thats exactly the question, I know I have the list id, how can I get the listname with the list id?, – Luis Valencia Oct 6 '14 at 12:09
1

Answered by Elio Struyf here: http://www.eliostruyf.com/retrieving-list-library-name-search-result-display-templates/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.