Get Site URL in Display Template

Posted on 2014-04-16
Last Modified: 2014-04-17
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"> 
<title>Unique Item Id Results</title>

<!--[if gte mso 9]><xml>
<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>
    <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><span><h1>Unique Item Results</h1></span></div>
							<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>


Open in new window

Question by:brendanlefavre
1 Comment
LVL 15

Accepted Solution

colly92002 earned 500 total points
ID: 40006213

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


Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question