+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Resolving the url of dynamic links

by Georgi Chokov
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

Leave a comment
  1. Martin Jul 02, 2009
    very helpful!

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

    Thank you for your great work over there.
  2. Eric Jul 10, 2009
    How would I go about skimming through text entered in a RTE in a custom control and resolve all the dynamic links?
  3. James Jul 10, 2009
    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. Georgi Jul 13, 2009
    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. Eric Jul 13, 2009
    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. Eric Jul 14, 2009
    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. Ricardo Jul 29, 2009
    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. V Oct 21, 2009
    Work Like a charm.

    Thanks :)
  9. David Nov 03, 2009
    This works like a charm with links, but not images from the image libraries.  Please help, need it for a project ASAP.  Thanks
  10. David Nov 03, 2009
    This works like a charm with links, but not images from the image libraries.  Please help, need it for a project ASAP.  Thanks
  11. David Nov 05, 2009
    Thanks for the quick turnaround as usual. Thanks.
  12. Mohan Nov 17, 2009
    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. Devin Jan 13, 2010
    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. Krishna Feb 09, 2010
    How can I find all the dynamic links in the content and resolve their URLs?
  15. Krishna Feb 09, 2010
    How can I find all the dynamic links in the content and resolve their URLs?
  16. Adeel Mar 11, 2010
    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. Joe Sep 28, 2010
    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. Joe Sep 28, 2010
    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

    Leave a comment