Solved

Get Site URL in Display Template

Posted on 2014-04-16
1
2,740 Views
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"> 
<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
0
Comment
Question by:brendanlefavre
1 Comment
 
LVL 15

Accepted Solution

by:
colly92002 earned 500 total points
ID: 40006213
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now