SharePoint 2013 list item attachment search.
Copy below highlighted content save as .html. Upload into a search template gallery and change the search result webpart template with your custom template which one you created above.
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head>
<title>Product and Datasheet</title>
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
<mso:MasterPageDescription msdt:dt="string">Displays the default result item template.</mso:MasterPageDescription>
<mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>
<mso:TargetControlType msdt:dt="string">;#SearchResults;#</mso:TargetControlType>
<mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>
<mso:ManagedPropertyMapping msdt:dt="string">'Title':'Title','Path':'Path','Description':'Description','EditorOWSUSER':'EditorOWSUSER','LastModifiedTime':'LastModifiedTime','CollapsingStatus':'CollapsingStatus','DocId':'DocId','HitHighlightedSummary':'HitHighlightedSummary','HitHighlightedProperties':'HitHighlightedProperties','FileExtension':'FileExtension','ViewsLifeTime':'ViewsLifeTime','ParentLink':'ParentLink','FileType':'FileType','IsContainer':'IsContainer','SecondaryFileExtension':'SecondaryFileExtension','DisplayAuthor':'DisplayAuthor'</mso:ManagedPropertyMapping>
</mso:CustomDocumentProperties>
</xml><![endif]
-->
</head>
<body>
<div id="Item_Default">
<!--#_
ExecuteOrDelayUntilScriptLoaded(loadResult, "sp.js");
function loadResult(){
if(!$isNull(ctx.CurrentItem) && !$isNull(ctx.ClientControl)){
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/Search/Item_Default_HoverPanel.js";
$setResultItem(itemId, ctx.CurrentItem);
if(ctx.CurrentItem.IsContainer){
ctx.CurrentItem.csr_Icon = Srch.U.getFolderIconUrl();
}
ctx.currentItem_ShowHoverPanelCallback = Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl);
ctx.currentItem_HideHoverPanelCallback = Srch.U.getHideHoverPanelCallback();
var Path=ctx.CurrentItem.Path;
var SiteName=ctx.CurrentItem.SiteName;
var ParentLink=ctx.CurrentItem.ParentLink;
var siteRelativePath = "";
if(ctx.CurrentItem.ParentLink.indexOf("/sites") != 0 && ctx.CurrentItem.ParentLink.indexOf("/Lists") != 0)
{
siteRelativePath = ParentLink.substring(ParentLink.indexOf("/sites"), ParentLink.indexOf("/Lists")) ;
}
var pathId = Path.substring(Path.indexOf("ID=") + 3, Path.length);
var clientContext;
if (siteRelativePath == "") {
clientContext = SP.ClientContext.get_current();
}
else {
clientContext = new SP.ClientContext(siteRelativePath);
}
var lists = clientContext.get_web().get_lists();
clientContext.load(lists, 'Include(Id,Title,DefaultViewUrl)');
clientContext.executeQueryAsync(
function () {
var listEnumerator = lists.getEnumerator();
var listUrl = "";
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
listUrl = oList.get_defaultViewUrl();
if (listUrl !== "" && ParentLink.indexOf(listUrl) > 0)
{ break;}
}
if (listUrl !== "" && ParentLink.indexOf(listUrl) > 0) {
var list = lists.getById(oList.get_id());
var listItem = list.getItemById(pathId);
clientContext.load(listItem);
clientContext.load(listItem.get_attachmentFiles());
clientContext.executeQueryAsync(
function () {
var item = listItem;
var total = item.get_attachmentFiles().get_count();
var files = "";
var itemtitle=item.get_item('Title');
for (var count = 0; count < total; count++) {
var spfile = item.get_attachmentFiles().get_item(count);
var fileName = spfile.get_fileName();
var serverRelativeUrl = spfile.get_serverRelativeUrl();
files += "<div><a href='" + encodeURI(serverRelativeUrl) + "'>" + itemtitle + "</a></div>"
}
if (files != "") {
var items = "<div class='ms-srch-hover-subTitle'></div><div class='ms-srch-hover-text'>" + files + "</div>";
document.getElementById(id + "_wrapper").innerHTML=items;
}
},
function (sender, args) {
});
}
},
function (sender, args) {
});
_#-->
<div id="_#= $htmlEncode(itemId) =#_" name="Item" data-displaytemplate="DefaultItem" class="ms-srch-item" >
<div id="_#= $htmlEncode(id + '_wrapper') =#_"> </div>
</div>
<!--#_
}
}
_#-->
</div>
</body>
</html>
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head>
<title>Product and Datasheet</title>
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
<mso:MasterPageDescription msdt:dt="string">Displays the default result item template.</mso:MasterPageDescription>
<mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>
<mso:TargetControlType msdt:dt="string">;#SearchResults;#</mso:TargetControlType>
<mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>
<mso:ManagedPropertyMapping msdt:dt="string">'Title':'Title','Path':'Path','Description':'Description','EditorOWSUSER':'EditorOWSUSER','LastModifiedTime':'LastModifiedTime','CollapsingStatus':'CollapsingStatus','DocId':'DocId','HitHighlightedSummary':'HitHighlightedSummary','HitHighlightedProperties':'HitHighlightedProperties','FileExtension':'FileExtension','ViewsLifeTime':'ViewsLifeTime','ParentLink':'ParentLink','FileType':'FileType','IsContainer':'IsContainer','SecondaryFileExtension':'SecondaryFileExtension','DisplayAuthor':'DisplayAuthor'</mso:ManagedPropertyMapping>
</mso:CustomDocumentProperties>
</xml><![endif]
-->
</head>
<body>
<div id="Item_Default">
<!--#_
ExecuteOrDelayUntilScriptLoaded(loadResult, "sp.js");
function loadResult(){
if(!$isNull(ctx.CurrentItem) && !$isNull(ctx.ClientControl)){
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/Search/Item_Default_HoverPanel.js";
$setResultItem(itemId, ctx.CurrentItem);
if(ctx.CurrentItem.IsContainer){
ctx.CurrentItem.csr_Icon = Srch.U.getFolderIconUrl();
}
ctx.currentItem_ShowHoverPanelCallback = Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl);
ctx.currentItem_HideHoverPanelCallback = Srch.U.getHideHoverPanelCallback();
var Path=ctx.CurrentItem.Path;
var SiteName=ctx.CurrentItem.SiteName;
var ParentLink=ctx.CurrentItem.ParentLink;
var siteRelativePath = "";
if(ctx.CurrentItem.ParentLink.indexOf("/sites") != 0 && ctx.CurrentItem.ParentLink.indexOf("/Lists") != 0)
{
siteRelativePath = ParentLink.substring(ParentLink.indexOf("/sites"), ParentLink.indexOf("/Lists")) ;
}
var pathId = Path.substring(Path.indexOf("ID=") + 3, Path.length);
var clientContext;
if (siteRelativePath == "") {
clientContext = SP.ClientContext.get_current();
}
else {
clientContext = new SP.ClientContext(siteRelativePath);
}
var lists = clientContext.get_web().get_lists();
clientContext.load(lists, 'Include(Id,Title,DefaultViewUrl)');
clientContext.executeQueryAsync(
function () {
var listEnumerator = lists.getEnumerator();
var listUrl = "";
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
listUrl = oList.get_defaultViewUrl();
if (listUrl !== "" && ParentLink.indexOf(listUrl) > 0)
{ break;}
}
if (listUrl !== "" && ParentLink.indexOf(listUrl) > 0) {
var list = lists.getById(oList.get_id());
var listItem = list.getItemById(pathId);
clientContext.load(listItem);
clientContext.load(listItem.get_attachmentFiles());
clientContext.executeQueryAsync(
function () {
var item = listItem;
var total = item.get_attachmentFiles().get_count();
var files = "";
var itemtitle=item.get_item('Title');
for (var count = 0; count < total; count++) {
var spfile = item.get_attachmentFiles().get_item(count);
var fileName = spfile.get_fileName();
var serverRelativeUrl = spfile.get_serverRelativeUrl();
files += "<div><a href='" + encodeURI(serverRelativeUrl) + "'>" + itemtitle + "</a></div>"
}
if (files != "") {
var items = "<div class='ms-srch-hover-subTitle'></div><div class='ms-srch-hover-text'>" + files + "</div>";
document.getElementById(id + "_wrapper").innerHTML=items;
}
},
function (sender, args) {
});
}
},
function (sender, args) {
});
_#-->
<div id="_#= $htmlEncode(itemId) =#_" name="Item" data-displaytemplate="DefaultItem" class="ms-srch-item" >
<div id="_#= $htmlEncode(id + '_wrapper') =#_"> </div>
</div>
<!--#_
}
}
_#-->
</div>
</body>
</html>
Comments
Post a Comment