开发者

sitecore: get absolute url in a xslt rendering

I开发者_JS百科 want to pass the absolute url of the current page to sharing services of facebook/twitter/linkedin. How do I get the absolute url of a page from a xslt rendering


I can at least tell you how this is done in a .NET sublayout, not sure if that helps:

Sitecore.Links.UrlOptions urlOptions = new Sitecore.Links.UrlOptions();
urlOptions.AlwaysIncludeServerUrl = true;
string url = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item, urlOptions);

Set other options on urlOptions as appropriate.

Happy coding.


I realize this is an old question, but the answer given really isn't the full picture. You can build an XSLT extension to handle this:

public class XslExtensions : Sc.Xml.Xsl.XslHelper 
{
    public string GetUrl(XPathNodeIterator iterator)
    {
        Sc.Data.Items.Item item = this.GetRequiredItem(iterator);
        return item.GetUrl(); // Extension method for Item that returns the URL as a string
    }

    public Sc.Data.Items.Item GetRequiredItem(XPathNodeIterator iterator)
    {
        Sc.Diagnostics.Assert.IsNotNull(iterator, "iterator");

        if (!iterator.MoveNext())
        {
            XsltException ex = new XsltException("No iterator.");
            Sc.Diagnostics.Log.Error(ex.Message, ex, this);
            throw ex;
        }

        Sc.Data.Items.Item item = this.GetItem(iterator);

        if (item == null)
        {
            XsltException ex = new XsltException("No item.");
            Sc.Diagnostics.Log.Error(ex.Message, ex, this);
            throw ex;
        }

        return item;
    }
}

Then you need to add the class that holds the above to the <xslExtensions> node:

<extension mode="on" type="MyProject.XslExtensions, MyProject" namespace="http://myproject.com/extensions" singleInstance="true" />

And finally you can use the method. First reference the extensions...

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:sc="http://www.sitecore.net/sc"
    xmlns:sql="http://www.sitecore.net/sql"
    xmlns:myp="http://myproject.com/extensions"
    exclude-result-prefixes="sc sql myp">

Then use!

<xsl:value-of select="myp:GetUrl(.)" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜