Solved

Get Site URL in Display Template

Posted on 2014-04-16
1
3,148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

695 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