1

I have a content search webpart that shows the title of the file and other properties.

However in some file its not using the property Title, it looks like its using the title on the first big letters from the documen itself

Example:

All the documents below which have the name INTRANET are wrong: http://screencast.com/t/vxM64hKBayy

The first page of one of those files look like this: http://screencast.com/t/CUAQkjcM

If I edit properties of one of these files its correct: http://screencast.com/t/oRa4SkPSidv8

I am assumming Sharepoint indexes the content of the document and assumes that first big letters on the first page is the title

Anyway to fix this?

My display template is like this

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

<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
<mso:ManagedPropertyMapping msdt:dt="string">'Link URL'{Link URL}:'ServerRedirectedURL','Line 1'{Line 1}:'Title','Line 2'{Line 2}:'ParentLink','Site Title'{Site Title}:'SiteTitle', 'Site Path'{Site Path}:'SPWebUrl', 'Author'{Author}:'Author','ServerRedirectedEmbedURL':'ServerRedirectedEmbedURL','FileExtension','SecondaryFileExtension','SPWebURL','ListID'</mso:ManagedPropertyMapping>
<mso:MasterPageDescription msdt:dt="string">This template shows the Document name, library name and site name.</mso:MasterPageDescription>
<mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>
<mso:TargetControlType msdt:dt="string">;#SearchResults;#Content Web Parts;#</mso:TargetControlType>
<mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>
<mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>
<mso:HtmlDesignStatusAndPreview msdt:dt="string">https://capatechsas.sharepoint.com/_catalogs/masterpage/Display Templates/Content Web Parts/Item_documentName_libraryName_siteName.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>
<mso:CrawlerXSLFile msdt:dt="string"></mso:CrawlerXSLFile>
<mso:HtmlDesignPreviewUrl msdt:dt="string"></mso:HtmlDesignPreviewUrl>
</mso:CustomDocumentProperties>
</xml><![endif]-->
</head>

<body>
    <script>

        Type.registerNamespace('search.listname');
        search.listname = function () {
            var itemInfo = {};
            var listInfo = {};
            var init = function (context, elmId) {
                // Store item info
                itemInfo[elmId] = {};
                itemInfo[elmId]["webUrl"] = context.CurrentItem.SPWebUrl;
                itemInfo[elmId]["listId"] = context.CurrentItem.ListID;
                // Add an on post render callback, this will be excuted when all the results are rendered
                AddPostRenderCallback(context, function() {
                    get(elmId);
                });
            },
            get = function (elmId) {
                // Retrieve the item information
                var webUrl = itemInfo[elmId].webUrl;
                var listId = itemInfo[elmId].listId;
                if ($isEmptyString(webUrl) || $isEmptyString(listId)) {
                    return;
                }
                // Remove brackets from guid
                listId = listId.substring(listId.indexOf('{') + 1, listId.indexOf('}'));
                // Check if the list object exists, if it exists, an Ajax call is already initiated or performed
                if (typeof listInfo[listId] !== "undefined") {
                    // Check if the list name is retrieved
                    if (typeof listInfo[listId]["Title"] !== "undefined") {
                        // Write the list name to the listname element on the page
                        quickRender(elmId, listId);
                    } else {
                        // List name was not yet retrieved, set the list ID as class on the element
                        setClass(elmId, listId);
                    }
                    return;
                } else {
                    // Ajax call not yet initiated, create a new object for the list ID
                    listInfo[listId] = {};
                    setClass(elmId, listId);
                }
                // Do a Ajax call to retrieve the list / library title
                (function (elmId, listId) {
                    var request = new XMLHttpRequest();
                    var restUrl = String.format("{0}/_api/Web/Lists(guid'{1}')?$select=Title'", webUrl, listId);
                    request.open('GET', restUrl, true);
                    request.setRequestHeader('Accept', 'application/json;odata=nometadata');
                    request.setRequestHeader('Content-Type', 'application/json;odata=nometadata');
                    request.onload = function (e) {
                        if (request.readyState === 4) {
                            // Check if the get call was successful
                            if (request.status === 200) {
                                // List retrieved
                                var data = JSON.parse(request.response);
                                if ($isEmptyString(data.Title)) {
                                    return;
                                }
                                // Store the list name
                                listInfo[listId]["Title"] = data.Title;
                                // Render the list name
                                render(elmId, listId);
                            } else {
                                // No list found
                            }
                        }
                    };
                    request.onerror = function (e) {
                        // Catching errors
                    };
                    request.send(null);
                })(elmId, listId);
            },
            // Set the list ID as class name to the SPAN element
            setClass = function (elmId, listId) { 
                var elm = document.getElementById(elmId);
                if (!$isNull(elm)) {
                    var listElm = elm.getElementsByClassName('listname')[0];
                    if (!$isNull(listElm)) {
                        // Write the list name to the page
                        listElm.className = String.format('{0} {1}',listElm.className, listId);
                    }
                }
            },
            // Write the document title to all elements with the same list ID
            render = function (elmId, listId) {
                var elms = document.getElementsByClassName(listId);
                if (!$isNull(elms)) {
                    for (var i = 0; i < elms.length; i++) {
                        elms[i].innerHTML = listInfo[listId].Title;
                    }
                }
            },
            // Write the list name to the element
            quickRender = function (elmId, listId) {
                var elm = document.getElementById(elmId);
                if (!$isNull(elm)) {
                    var listElm = elm.getElementsByClassName('listname')[0];
                    if (!$isNull(listElm)) {
                        // Write the list name to the page
                        listElm.innerHTML = listInfo[listId].Title;
                    }
                }
            };
            return {
                init: init
            };
        }();
        Srch.U.registerRenderTemplateByName("listname_init", search.listname.init);
    </script>


    <div id="Item_Listname">
<!--#_
var id = ctx.ClientControl.get_nextUniqueId();
var itemId = id + Srch.U.Ids.item;
var hoverId = id + Srch.U.Ids.hover;
var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Item_documentName_libraryName_siteName_HoverPanel.js";
$setResultItem(itemId, ctx.CurrentItem);
ctx.currentItem_ShowHoverPanelCallback = Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl);
ctx.currentItem_HideHoverPanelCallback = Srch.U.getHideHoverPanelCallback();

var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "_listname_");
var containerId = encodedId + "container";
// Listname init
Srch.U.getRenderTemplateCollection().listname_init(ctx, containerId);
var linkURL = $getItemValue(ctx, "Link URL");
linkURL.overrideValueRenderer($urlHtmlEncode);
var iconURL = Srch.ContentBySearch.getIconSourceFromItem(ctx.CurrentItem);
var line1 = $getItemValue(ctx, "Line 1");
var line2 = $getItemValue(ctx, "Line 2");
var SitePath = $getItemValue(ctx, "Site Path");
var SiteTitle = $getItemValue(ctx, "Site Title");
var Author = $getItemValue(ctx, "Author");
var ServerRedirectedEmbedURL = $getItemValue(ctx, "ServerRedirectedEmbedURL");
line1.overrideValueRenderer($contentLineText);
line2.overrideValueRenderer($contentLineText);
var pictureLinkId = encodedId + "pictureLink";
var pictureId = encodedId + "picture";
var dataContainerId = encodedId + "dataContainer";
var line1LinkId = encodedId + "line1Link";
var line1Id = encodedId + "line1";
var line2Id = encodedId + "line2";

function showPopUp (element) {
                $(element).find('.ms-srch-hover-outerContainer').css({visibility:'inherit',opacity:'inherit',left: '690px'});
            }
            function hidePopUp (element) {
                $(element).find('.ms-srch-hover-outerContainer').css({visibility:'hidden',opacity:'0'});
            }
_#-->

        <div name="Item" id="_#= $htmlEncode(itemId) =#_" data-displaytemplate="Listname" onmouseover="_#= ctx.currentItem_ShowHoverPanelCallback =#_showPopUp(this);" onmouseout="_#= ctx.currentItem_HideHoverPanelCallback =#_hidePopUp(this);">
            <div id="_#= $htmlEncode(hoverId) =#_" class="ms-srch-hover-outerContainer"></div>
            <div class="cbs-Item" id="_#= containerId =#_">
                <a class="cbs-ItemLink" title="_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_" id="_#= pictureLinkId =#_">
                    <img class="cbs-Thumbnail" src="_#= $urlHtmlEncode(iconURL) =#_" alt="_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_" id="_#= pictureId =#_" />
                </a>
                <div class="cbs-Detail list-group-item" id="_#= dataContainerId =#_">
                    <a class="cbs-Line1Link ms-noWrap ms-displayBlock" href="_#= linkURL =#_" title="_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_" id="_#= line1LinkId =#_"><h4 class="list-group-item-heading" target="_blank">_#= line1 =#_</h4></a>

                    <strong>Library: </strong> <a  class="listUrl" href="_#= line2 =#_" target="_blank"><span class="listname"></span></a>
                    <strong>Site:</strong> <a  href="_#= SitePath =#_" target="_blank"><span>_#= SiteTitle =#_</span></a>
                    <strong>Author:</strong> <span>_#= Author =#_</span>
                   <!-- <div>
                            <iframe src="_#= $urlHtmlEncodeString(ctx.CurrentItem.ServerRedirectedEmbedURL) =#_" scrolling="no" frameborder="0px" style="height: 267px;"></iframe>
                    </div>-->
                </div>
            </div>
        </div>
    </div>
</body>
</html>
3
+50

In SharePoint Online you cannot change the crawled property mappings of the Title managed property.

What you could do is to use one of the predefined RefinableString.. managed properties and add the crawled property mappings that you want.

Wait for the next full crawl and once that is done and you should get your field values.

  • thanks Elio, I mapped RefinableString00 to Title and ows_Title, that should be enough. – Luis Valencia Mar 2 '15 at 16:20
2

Sounds like you got what you needed, but I thought I'd elaborate a bit to save time for others that come across this sort of issue.

Recently, I had to customize search results for a client that wanted a very specific look and feel. Whenever possible, I like to use Managed metadata or Managed Content Types (Custom Columns) because SharePoint AutoMagically indexes these columns during search. Then, you can add them to the managed properties of the search display templates and call them in the via code like below, where MyModules is a custom content type / custom column .

Modules: _#= MyModuleOWSTEXT =#_

In short, changes to Item_CommonItem_Body.html will affect almost all results except for type specific searches such as Word, Excel, People, etc.

If you create a custom *.html, then you will need to create a custom result source and result type so they are all connected. Then, you can create a new search page or modify an existing search page so that you can use the custom result source and result type.

Lastly, changes to Sharepoint online search take several minutes. In between the changes, you may see an error on the search page.

After perfecting this process, you can ignore the intermittent error to allow the changes to go through.

Hopes this helps!

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.