Get Site URL in Display Template

Currently I'm hard coding the URL for the current site, and would like to use the current context to get the proper link. Which property can I use to get the current url?

In my example, the variable "href" is being hard coded, I would like to make this a bit more dynamic.

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

<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
<mso:MasterPageDescription msdt:dt="string">Displays the inline result body elements that are common to all results.</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','ListUrl':'ListUrl','ListItemId':'ListItemId','ListId':'ListId','SiteId':'SiteId','WebId':'WebId','LegacyItemId':'LegacyItemId'</mso:ManagedPropertyMapping>
<mso:_dlc_DocId msdt:dt="string">5HUVTUKZTP46-1-223</mso:_dlc_DocId>
<mso:_dlc_DocIdItemGuid msdt:dt="string">27aca1c4-1300-489b-bf9c-71edbbb401b2</mso:_dlc_DocIdItemGuid>
<mso:_dlc_DocIdUrl msdt:dt="string">http://intranet/sites/jwtest/_layouts/15/DocIdRedir.aspx?ID=5HUVTUKZTP46-1-223, 5HUVTUKZTP46-1-223</mso:_dlc_DocIdUrl>
</mso:CustomDocumentProperties>
</xml><![endif]-->
</head>
<body>
    <div id="Item_CommonItem_Body">
<!--#_
        var id = ctx.CurrentItem.Id;
		var legacyItemId = ctx.CurrentItem.LegacyItemId;
		var listUrl = ctx.CurrentItem.ListUrl;
		var listItemId = ctx.CurrentItem.ListItemId;
		var listId = ctx.CurrentItem.ListId;
		var siteId = ctx.CurrentItem.SiteId;
		var webId = ctx.CurrentItem.WebId;
        	var title = ctx.CurrentItem.Title;
		var parentLink = ctx.CurrentItem.ParentLink;

		var href = "http://intranet/sites/jwtest/";
		
		var action = "_layouts/15/CopyUtil.aspx?Use=id&Action=dispform";
		var completeLink = String.format('<a href="{0}{1}&itemId={2}&listId={3}&siteId={4}&webId={5}">{6} </a>', href, action, listItemId, $htmlEncode(listId), $htmlEncode(siteId), $htmlEncode(webId), title);

		
_#-->				
    
            <div>
				<div><span><h1>Unique Item Results</h1></span></div>
					<div>
						<ul>
							<li>Title: _#= title =#_</li>
							<li>Legacy ID: _#= legacyItemId =#_</li>
							<li>List URL: _#= listUrl =#_</li>
							<li>List Item ID: _#= listItemId =#_</li>
							<li>List ID: _#= listId =#_</li>
							<li>Site ID: _#= siteId =#_</li>
							<li>Web ID: _#= webId =#_</li>
							<li>Parent Link: _#= parentLink =#_</li>
							<li>Completed URL: _#= completeLink =#_ </li>
						</ul>
					</div>
     
            </div>

	</div>
</body>
</html>

Open in new window


Cheers,
Brendan
brendanlefavreAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
colly92002Connect With a Mentor Commented:
Try:

var href = ctx.CurrentItem.SPSiteUrl;

Open in new window


or if that is what you need, then you could try this if the client model is loaded:
var clientContext = new SP.ClientContext.get_current();
href  = clientContext.get_site().get_serverRelativeUrl ()

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.