+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / How to use a custom url in the Products module - Solution

How to use a custom url in the Products module - Solution

5 posts, 0 answered
  1. Sonu Kapoor
    Sonu Kapoor avatar
    41 posts
    Registered:
    24 Apr 2006
    14 Jul 2011
    Link to this post
    Hi,

    If anybody is looking for a way to generate a seo friendly url with the category in it, then please use the following code-snippet for your module. The url will change from /2011/01/28/product-name to /category/product-name. This code needs to be placed in your openaccessdataprovider.cs.

    Hope this helps somebody. I would not have been able to figure this out without the help of the amazing telerik support team - kudos to them.

     public override string GetUrlFormat(ILocatable item)
     {
         return "/[CategoryName]/[UrlName]";
     }
     
     protected override string GetUrlPart<T>(string key, string format, T item)
     {
         if (key == "CategoryName")
         {
             return this.GetCategoryNameForItem(item);
         }
         return base.GetUrlPart<T>(key, format, item);
     }
     
     protected string GetCategoryNameForItem(IDataItem item)
     {
         if (item == null)
         {
             throw new ArgumentNullException("item");
         }
         string category = string.Empty;
         IContent content = (IContent)item;
     
         IList<Guid> categoryIds = ((ProductItem)item).GetValue<IList<Guid>>("Category");
         Guid firstCategoryId = categoryIds.FirstOrDefault();
         if (Guid.Empty != firstCategoryId)
         {
             var taxonManager = TaxonomyManager.GetManager();
             category = taxonManager.GetTaxon(firstCategoryId).Name;
             category = Regex.Replace(category, "[^a-zA-Z0-9]", "-");
         }
     
         return category;
     }
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    30 Nov 2016
    14 Jul 2011
    Link to this post
    Hi Sonu,

    Thank you for sharing this with the community. This will truly help a lot of people and save them time. We really appreciate when our clients actively share knowledge and help each other.

    Kind regards,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Sonu Kapoor
    Sonu Kapoor avatar
    41 posts
    Registered:
    24 Apr 2006
    14 Jul 2011
    Link to this post
    My pleassure Stanislav. It would have not been possible without your help. Thanks again.
  4. Mike
    Mike avatar
    5 posts
    Registered:
    14 Aug 2012
    12 Sep 2012
    Link to this post
    Absolutely, a great help.  I suggest Sitefinity team to start a knowledge base where they can publish with their solutions to clients problems.  I am concern with solutions to problems that more than one site are facing.  Knowledge base applications have more powerful search engines.

    Thanks,
    Zia
  5. Mike
    Mike avatar
    5 posts
    Registered:
    14 Aug 2012
    12 Sep 2012
    Link to this post
    Absolutely, a great help.  I suggest Sitefinity team to start a knowledge base where they can publish with their solutions to clients problems.  I am concern with solutions to problems that more than one site are facing.  Knowledge base applications have more powerful search engines.

    Thanks,
    Zia
5 posts, 0 answered