Documentation > CMS Template API Library > Asset > GetLink(LinkType,String,String,Boolean,ProtocolType)

GetLink

Renders a link to the asset based upon the current context. Upon publishing, it will return the link to the published page. Upon previewing, it will return the cms preview link to the page.

public System.String GetLink(LinkType,String,String,Boolean,ProtocolType)


Returns

A link to the page.

Parameters

NameDescriptionType
type The link type. CrownPeak.CMSAPI.LinkType
layout Optional: The layout name, without file extension. Default is "output". If this is a preview and a preview template exists, it will use that for a default. System.String
publishingArgs Optional: args such as _pagenum=2. Default is empty string. System.String
addDomain Optional: If true add HostName for current publishing package to link. Defaults to false. Must be in OutputContext (output template or filename or url template) and publishing. System.Boolean
protocolType Optional: If addDomain is true, determines if the protocol should be added and whether to use http or https.indexer. Defaults to http. CrownPeak.CMSAPI.ProtocolType

Code Example

C#

Sample:
Asset myAsset = Asset.Load("/_CPTest/Test");
if(myAsset.IsLoaded)
{
  string strLink1 = myAsset.GetLink(LinkType.Include);
  string strLink2 = myAsset.GetLink(LinkType.Default, "output", addDomain: true, protocolType: ProtocolType.Https);		
  string strLink3 = myAsset.GetLink(LinkType.Default, "output_alternate", "_pagenum=2", addDomain: true, protocolType: ProtocolType.Https);	

  Out.WriteLine("<a href=\"" + strLink1 + "\">Link 1</a><br />");
  Out.WriteLine("<a href=\"" + strLink2 + "\">Link 2</a><br />");
  Out.WriteLine("<a href=\"" + strLink3 + "\">Link 3</a><br />");

}

Connect with Crownpeak