Resolving the url of dynamic links

by
As most of you have noticed, in Sitefinity 3.6 we introduced a new way of handling links to content items, images, documents and pages. By implementing the dynamic content links, you can now move pages around the sitemap and rename libraries, and the links to such items will not be broken.

Each time you insert a link through the LinkManager and Images/Document manager, a link in the following format is being produced:

[ProviderName]GUID_of_content item

Here are some examples:

  • [Sitefinity]fc6427d9-a514-4818-ba4d-5b0595989898 - a link to cms page
  • [Blogs_Libraries]b515d826-5ba6-4d2e-b638-13ae1b93b3c9 - a link to a library item
Later on, Sitefinity parses these links and forms the real paths to the items.

 

Sometimes you need to parse such "links" in your custom controls or modules, right? In such cases, you might use this code:

public string GetItemUrl(string provider, Guid id, bool resolveAsAbsoluteUrl) 
    if (ContentManager.Providers.ContainsKey(provider)) 
    { 
        IContent cnt = ContentManager.Providers[provider].GetContent(id); 
        if (cnt != null
            return VirtualPathUtility.ToAbsolute(cnt.UrlWithExtension, this.Context.Request.ApplicationPath); 
    } 
    else 
    { 
        // we assume it is a page 
        SiteMapNode node = SiteMap.Provider.FindSiteMapNodeFromKey(id.ToString()); 
        if (node != null
            return this.ResolveClientUrl(node.Url); 
    } 
    return String.Concat("Item not found: [", provider, "]", id); 

You can as well use the different Manager classes depending on the provider that is defined in the link, or even use the ContentManager class initialized with different provider.

I hope you will find that code helpful.

18 Comments

  1. 1 Martin 02 Jul
    very helpful!

    You guys, Georgi, Ivan and the whole support team - you just rock!

    Thank you for your great work over there.
  2. 2 Eric 10 Jul
    How would I go about skimming through text entered in a RTE in a custom control and resolve all the dynamic links?
  3. 3 James 10 Jul
    Very useful post. But how do we get a property in a custom module to bring up a URLEditor that returns the [providerName]Guid ...

    Ie. Similar to using [Telerik.Cms.Web.UI.CmsUrlWebEditor] on a property but instead of it returning a URL have it return the dynamic link page GUID

    thanks
  4. 4 Georgi 13 Jul
    Eric,

    You can use the following method:
    LinkParser.ResolveLinks(Content, this.GetItemUrl);

    Note that this.GetUtemUrl is the same method, which will parse all links from the Content.

    James,
    I will try to blog about your inquiry soon.
  5. 5 Eric 13 Jul
    Thanks Georgi,

    I must be missing something cause when I use LinkParser.ResolveLinks(), all the links in the text still show the Provider name + guid.

    any ideas?

    Thanks,
    Eric


  6. 6 Eric 14 Jul
    Update:  I got it to work, only after I modified some of the code in the RTE. 

    Had to replace %5B and %5D with [ ].

    When inserting a link, it sets the href to %5BSitefinity%5Da0ea2779-7a06-4647-96d6-35ebd2214fd3, while the sfref is in this format: [Sitefinity]a0ea2779-7a06-4647-96d6-35ebd2214fd3.

    I dont understand why the RTE replaces [ ]  with %5B and %5D, is there anything I am missing?


    thanks
  7. 7 Ricardo 29 Jul
    This example works great with any sitefinity page, but it ALWAYs returns a Item Not Found when passing in a Library item(image).

    passing in [Library]Guid... nothing
  8. 8 V 21 Oct
    Work Like a charm.

    Thanks :)
  9. 9 David 03 Nov
    This works like a charm with links, but not images from the image libraries.  Please help, need it for a project ASAP.  Thanks
  10. 10 David 03 Nov
    This works like a charm with links, but not images from the image libraries.  Please help, need it for a project ASAP.  Thanks
  11. 11 David 05 Nov
    Thanks for the quick turnaround as usual. Thanks.
  12. 12 Mohan 17 Nov
    Hello,
    Can you please point me to some documentation that would help me to implement a new provider? For example, I want to implement a provider that would link to a partner site based on some parameters. 

    An example would need the link to be
    [Partner_Site]xxxxx

    Also, will this work only with links <a href="xxx">? Is it possible to have some content (tokens inside content) that could be parsed at run time?
  13. 13 Devin 13 Jan
    Hi Georgi,
    I have a user control that implements Telerik HtmlContentEditor to edit string properties.  If I insert a link into one of these properties it will get the right page but if I rename the page that I linked to the links do not get updated.  Is there a way to update links when the control loads?

    Thanks,
    Devin
  14. 14 Krishna 09 Feb
    How can I find all the dynamic links in the content and resolve their URLs?
  15. 15 Krishna 09 Feb
    How can I find all the dynamic links in the content and resolve their URLs?
  16. 16 Adeel 11 Mar
    The ERROR Some time is caused due to the login problem when a page that has anonymous access restricted is accessed through the above code with provider not given that is it tries to get the page through sitemap, it would throw an error.

    "Item not found: GUID of the page.


  17. 17 Joe 28 Sep
    Hello
    I am working on a project currently and my task is to create an RSS feed. I started by naming the RSS but what about Feeds URL ?

    Thank you
  18. 18 Joe 28 Sep
    Hello
    I am working on a project currently and my task is to create an RSS feed. I started by naming the RSS but what about Feeds URL ?

    Thank you

Comment

  1.    
     
     
      
       

Categories

+ View all

Blogs

+ View all