LinkCollection and external/internal links problem

Hg El
Member since: 2013
 

Hi!

I've run into a little problem with LinkCollection. I've searched the forum and think I've found a solution for cms 6 (using GetMappedHref()) but I'm stuck with cms 5 here. My code works just fine for adding external links (http://test.com) but once I point to a local/internal page I get the long guid type of link (~/link/2f3f05692d04468a8c33f815ff2ca12c.aspx). It all works when I render the list via <EPiServer:Property PropertyName="TPLC" runat="server" /> in the markup code. But this option doesnt give me the possibilty to style the list the way I want to... So I'm using an asp:repeater to handle this. I databind the collection from code behind:

links = CurrentPage.Property["TPLC"].Value as LinkItemCollection;
linkRepeater.DataSource = links;
linkRepeater.DataBind();

and in markup I use:

<asp:Repeaterrunat="server" ID="linkRepeater">
<HeaderTemplate><ol></HeaderTemplate>
<ItemTemplate>
<li><ahref="<%# (Container.DataItem as EPiServer.SpecializedProperties.LinkItem).Href %>"target="<%# (Container.DataItem as EPiServer.SpecializedProperties.LinkItem).Target %>"title="<%# (Container.DataItem as EPiServer.SpecializedProperties.LinkItem).Title %>"><%# (Container.DataItem as EPiServer.SpecializedProperties.LinkItem).Text %></li></a>
</ItemTemplate>
<FooterTemplate></ol></FooterTemplate>
</asp:Repeater>

As I said, it works just fine for external links but how do I convert the internal guid links to a usable link that works?
The function will mainly be used for external links but incase someone decides to go internal I need the compability. I've googled and found some tips but nothing small and "simple"... Seems like there should be some kind of simple solution for this. Thankful for any tips!

 

#64940 Jan 16, 2013 16:32
  • Johan Petersson
    Member since: 2007
     

    Have you tried ToMappedLink()?

    #64945 Jan 16, 2013 17:42
  • Johan Petersson
    Member since: 2007
     
  • Johan Petersson
    Member since: 2007
     

    ToMappedLink() gives you the whole link, if you only want the url I guess you can do something like;

     

    public static string ToMappedUrl(this LinkItem link)
    {
            var url = new UrlBuilder(link.Href);
    
            if (PermanentLinkMapStore.ToMapped(url))
            {
                return url.ToString();
            }
    
            return link.Href;
    }

        

    #64947 Jan 16, 2013 17:47
  • Hg El
    Member since: 2013
     

    Ah... That looks like a method that would solve the whole thing... I've tried to implement the extension but I'm having some problems and I get complaints when I compile. I'm kind of new to EPi so if you could show me an example on how to implement the method (in code behind) I would be grateful! Thanks...

    #64987 Jan 17, 2013 9:50
  • Hg El
    Member since: 2013
     

    I got it to work... I missed importing the extension from markup and I was also sending the wrong type (Href) to the method. When using LinkItem and importing extension it worked like a charm :) Many thanks!

    #64994 Jan 17, 2013 10:56
  • Nishant Mittal
    Member since: 2012
     

    Hi, 

    I am also getting stuck with same scenario after createing linkcollection of documents type(PDF Type), In case of repeater it shows the different url which lookes like a GUID type url and its not navigating to the pdf cdoc which i have save from the episerver,

    can you please tell me the code how to get the href of uploaded pdf doc file from the linkcollection by using method ToMappedUrl(). I am using episerver 6 version.

    #65695 Feb 06, 2013 10:53
  • Hg El
    Member since: 2013
     

    I solved it by using an extension class for the method (same as above):

    public static class UrlExtension
        {
            public static string ToMappedUrl(this LinkItem link)
            {
                var url = new UrlBuilder(link.Href);
    
                if (PermanentLinkMapStore.ToMapped(url))
                {
                    return url.ToString();
                }
    
                return link.Href;
            }
        }

        

    And then by calling it from markup (and importing the namespace for the extension):

    <%@ import Namespace="yournamespace.Templates.Public.Units.LinkCollectionUrlExtension" %>

    ... and in a repeater adding ...
    <a href="<%# (Container.DataItem as EPiServer.SpecializedProperties.LinkItem).ToMappedUrl() %>" target="<%# (Container.DataItem as EPiServer.SpecializedProperties.LinkItem).Target %>"
                            title="<%# (Container.DataItem as EPiServer.SpecializedProperties.LinkItem).Title %>">
                            <%# (Container.DataItem as EPiServer.SpecializedProperties.LinkItem).Text %>
    </a>
            
    #65723 Feb 06, 2013 16:31
  • Nishant Mittal
    Member since: 2012
     

    Everything is fine but i dont have the unit LinkCollectionUrlExtension, so code is not working and can not map tomappedurl method on href. 

    where i can find this LinkCollectionUrlExtension? Please tell me...

    #65794 Feb 10, 2013 8:11
  • Johan Petersson
    Member since: 2007
     

    Nishant you have to import the namespace where you put ToMapped() extension method.  Hg El's code was just an example.

    #65795 Feb 10, 2013 12:43
  • Johan Petersson
    Member since: 2007
     

    Nishant you have to import the namespace where you put ToMapped() extension method.  Hg El's code was just an example.

    #65796 Feb 10, 2013 12:43
  • Nishant Mittal
    Member since: 2012
     

    Thanks a lot Hg El and  Johan Pettersson, now its working.

    #65798 Feb 10, 2013 15:34
  • Roman Golub
    Member since: 2011
     

    Hi,

    Is it possible to add a LinkItem to the page with parameter in query string?

    I create an "external link" with Url to some page (http://www.mysite/Page_A?someId=1) and add it to my LinkItemCollection, but when I save and publish my changes - this link is converted from external to internal, and there is no query parameter anymore.

    Can somebody help with this?

    Thanx

    #87000 Jun 06, 2014 10:48
  • Linus Ekström
    Member since: 2002
     

    Additional query string parameters should be kept, even though the link is converted to an internal one. Which version are you running on?

    #87002 Jun 06, 2014 11:32
  • Roman Golub
    Member since: 2011
     

    We use EPiServer 7.7.1.0;

    Sorry, parameter (from query string) is kept, it is only not visible for Editor.

    But still, there is another problem:

    We have our custom route registered: "club/{clubId}/{language}/{node}/{partial}/{action}". When I create an external link to the page using this route - (for example, "http://mysite/club/15/somePage" - link is converted to internal ("http://mysite/somepage"), and parameter disappears (I have subscribed to the DataFactory.Instance.PublishedPage event, and I see that when I publish page - link is already converted to internal)

    Btw, it looks like problems come from database: when I use my local database - link is not converted to external, and all works fine, but when I use database backup from our Development stand - it doesn't work.

    #87006 Jun 06, 2014 13:10
  • Linus Ekström
    Member since: 2002
     

    Hi!

    You are correct that we don't store additional path information after the part that is used to address a content item, only additional query string information. Also, you are correct that the UI does not show these values when editing the link again. We are discussing how to handle this internally since we've got some feedback in this area. However, the solutions we have discussed are not that simple to implement so we need to find out the best way to handle this. Ideally, we would present this as an external link but still have support for permanent links in the backend so that links does not get broken just because the URL to an item is changed.

    #87623 Jun 18, 2014 12:39
  • Renat Khasanshin
    Member since: 2008
     

    Hi @Linus.

    Is EPiServer going to fix this issue with custom routing?

    #112286 Oct 27, 2014 13:36
  • Renat Khasanshin
    Member since: 2008
     

    We have our custom route registered: "club/{clubId}/{language}/{node}/{partial}/{action}". When I create an external link to the page using this route - (for example, "http://mysite/club/15/somePage" - link is converted to internal ("http://mysite/somepage"), and parameter disappears (I have subscribed to the DataFactory.Instance.PublishedPage event, and I see that when I publish page - link is already converted to internal)

    Ok, for now this problem can be fixed with using HttpModule instead of routing

    #112488 Oct 29, 2014 9:27
  • Linus Ekström
    Member since: 2002
     

    Hi!

    I just talked to the team and they plan to start investigating a possible fix for this now. So if we are lucky, this might be released within a few weeks if the fix is reasonable to implement.

    #112578 Oct 30, 2014 13:23