For every Page I want to write out a meta tag in the HTML head containing the full Page URL. How do I build up this URL including domain name?
We're using SDL Tridion 2011 SP1 with modular templates.
I'm assuming you're attempting to obtain the URL within your Template Building Block.
If you've got a Page object, you can simply call the following Page.PublishLocationUrl to give you something like /mydirectory/mypage.html.
The domain isn't stored, you're probably best using server side code to output the host name, if you're using static pages you can simply store the website URL somewhere in SDL Tridion (I recommend as metadata on the publishable Publication. You can read that value using the API from the Page.Publication.Metadata
Here's a bit more code to show getting the page and reading the page URL:
In your TBB:
Page page = null;
Item pageItem = package.GetByType(Package.PageName);
if (pageItem != null)
{
page = engine.GetObject(pageItem.GetAsSource().GetValue("ID")) as Page;
}
That will get you your page object.
You can then read the PublishURL from it:
string pagePath = page.PublishLocationUrl;
Once you've constructed your full path (including domain however you wish to manage that) you can then push the data in the package, which can be ready by your page DWT layout file :)
package.GetByName(Package.PageName)
instead of GetByType
: you can have multiple pages in the same package (rare but possible) and GetByType will get you the last page in the stack.
GetByType
will get the item of the type you request that was most recently added to the package. GetByName
will get the item with that name that was most recently added to the package. So no difference there. The difference is that you can (and should) use a non-standard name (so not "Page", but e.g. "ReferingPage") for any Page you decide to add to the package, where most likely you will want to use the default type (tridion/page
). So looking up a page by its name is more likely to give you the one you are looking for, even when other developers later add their pages to the package too.
Commented
Mar 3, 2013 at 2:16
A humble tip is to install and use Razor Templating, which gives you near-full access to the .NET back-end of your templates. In your case you can simply write @Publication.PublicationUrl. This can be done inside you Page Template Building Block, without the need for custom TBB's.
Without knowing the full requirements, it is difficult to give a definitive answer on this. However, if you are just after the URL in a HTML comment on the page then you may want to consider doing this in your ASP.NET / JSP code rather than your Tridion template**.
e.g. <meta url="<%=Request.Url.AbsoluteUri %>" />
Although this code would need to be executed at request time (which could be offset with caching), it would have a number of benefits including:
**I am aware that the question was specifically about doing this in a Page Template, but it is always worth considering alternative options.
In a Page Template there is no option to get the domain name of the website this page is Published to, since SDL Tridion doesn't know this information. Content (including Pages) is published to a Publication Target, which is in SDL Tridion terms just a destination. How this is exposed as a website (and with what domain/url) is not known.
What you can do is add metadata to for instance the Publication, containing the url (or domain name) for your live (and optionally staging) websites. Then you can have your Page Template pick up this information and build the URL from that (adding the Page path and filename + extension).
If you want to keep information about the target web site in the Tridion CM, the logical approach is to associate it with the publication target. In the past, I have done this by adding a field to the protocol schema, however these days you might prefer to use ApplicationData
In regards to point 2 of Jonathan and for the sake of storing information in the right place i suggest to put domainnames in the right place, there where it is managed, at the CD side in the same config as we configure the site settings. But this means that you will have to write a Deployer extention to retrieve that during deploy and place that in the page. I would say a bit of overkill for what you wish to accomplish :)
Reading your question there is no mention you actually want to do this in your tridion templates, you only mention what kind of templates you use. This makes Jonathans answer the most viable in my eyes, unless your pages need to be HTML only or your cashing rules are very strict. In that case i would re-consider your businesscase requirements as a HTML only website is not so much nessesary anymore these days :)