# Change the URL format of hierarchical dynamic modules

## Overview

The URL of a hierarchical item is automatically generated and, by default, includes the URL of all of its parent items. For more information, see [URLs of dynamic content items](https://www.progress.com/documentation/sitefinity-cms/administration-urls-of-dynamic-content-items). Below are two examples you can use to change the URL format of dynamic content items.

## Add a custom field to the URL of a dynamic item

When you use Sitefinity CMS Module Builder to create a dynamic module, by default, the URL of the dynamic items is constructed in the following way: *\<your site>*/*\<page where item is displayed>*/*\<item’s URL>*. By default, the item URL is its title. This can result in duplicated URLs for items with the same title.

To avoid creating items with duplicate URLs, you  can build your item’s URL based on a field or combination of fields that are unique for every item.

To do this, perform the following:

1. Create an extension to the module provider, where you construct the URL format.\
   You do this, by adding a new class to your project. You inherit from `OpenAccessDynamicModuleProvider `and override the `GetUrlFormat` method where you enter the desired URL.\
   Use the following code example:

   ```C#
   using Telerik.Sitefinity.DynamicModules.Data;
   using Telerik.Sitefinity.GenericContent.Model;

   namespace SitefinityWebApp
   {
       public class NewModuleBuilderProvider : OpenAccessDynamicModuleProvider
       {
           public override string GetUrlFormat(ILocatable item)
           {
               return "/[Address]/[JobTitle]/[UrlName]";
           }
       }
   }
   ```

   In the above example, the URL of the items is based on the value of fields in the module, which are named `Address`, `JobTitle`, and `UrlName`. The `Address` and `JobTitle` are fields that you must create in the module, the `UrlName` is a built-in field.

   > info **NOTE**: All fields, used for the URL format must be text fields.
2. Register the new provider.\
   Perform the following:
   1. In the backend, click *Administration* » *Settings*  » *Advanced*.
   2. In the tree on the left, click *DynamicModules* » *Provider* » *Create new*.
   3. Enter a *Name* and *Title* of the new provider.
   4. In field *ProviderType*, enter the type of the class that you created in *Step 1*.\
      In the code example above, the type of the class is `SitefinityWebApp.NewModuleBuilderProvider`.
   5. Under the new provider, click *Parameters* » *Create new*.
   6. In *Key*, enter `applicationName`
   7. In *Value*, enter the name of your application, for example, `myCustomProvider`.

      > info  **NOTE**: Do not use `/DynamicModule` for the value, because this name represents the default Sitefinity CMS provider.
3. Restart the application.\
   Your provider is created.

When you open the module and add new items, there are two providers available - *Default* and the new provider you added. If you have multiple sites, you must add the provider to the modules where you want to use it. For more information, see [Share content providers of modules](https://www.progress.com/documentation/sitefinity-cms/share-content-providers-of-modules).\
New items added to this new provider use the new URL format - `/[Address]/[JobTitle]/[UrlName]`

The new provider is displayed in all modules you create with the Module Builder, together with the *Default* provider. If you want to display only the new provider and hide the default one, preform the following procedure [Filter by data provider the items displayed on backend pages](https://www.progress.com/documentation/sitefinity-cms/filter-by-data-provider-the-items-displayed-on-backend-pages).

## Remove the parent item from the URL of the child item

The following procedure explains how to remove the parent item URL from the URL of a child item. To do this, you must create a custom provider and override some of the methods.

To remove the parent item of *just* a specific module and type, perform the following:

1. Implement the custom provider.
   1. Create a new class and inherit from `Telerik.Sitefinity.DynamicModules.Data.OpenAccessDynamicModuleProvider`.
   2. Override the `GetUrlFormat` that returns the format string and use the following code to remove the parent URL of the parent item:

      ```C#
      using Telerik.Sitefinity.DynamicModules.Model;

      namespace SitefinityWebApp
      {
          public class RemoveParentFromChildURL : Telerik.Sitefinity.DynamicModules.Data.OpenAccessDynamicModuleProvider
          {
              private bool resolvingChildUrlFormat = false;

              /// <inheritdoc />
              public override string GetUrlFormat(Telerik.Sitefinity.GenericContent.Model.ILocatable item)
              {
                  var resolvedItem = (DynamicContent)item;
                  if (resolvedItem.GetType().FullName == "Telerik.Sitefinity.DynamicTypes.Model.M1.Grandchild")
                  {
                      this.resolvingChildUrlFormat = true;
                      return "/[UrlName]";
                  }

                  if (this.resolvingChildUrlFormat)
                  {
                      // Provide all the parent content types whose url part must be hidden(except the content type at the top most level) in this if clause
                      if (resolvedItem.GetType().FullName == "Telerik.Sitefinity.DynamicTypes.Model.M1.Child")
                      {
                          return "";
                      }

                      // Provide the content type at the top most level in this if clause
                      if (resolvedItem.GetType().FullName == "Telerik.Sitefinity.DynamicTypes.Model.M1.Parent")
                      {
                          this.resolvingChildUrlFormat = false;
                          return "";
                      }

                  }

                  this.resolvingChildUrlFormat = false;
                  return "/[UrlName]";
              }
          }
      }
      ```

      In the above code, you can return a different value, for example, the value from a custom field that you have created for your custom provider.
2. Register the new provider in Sitefinity.
3. Republish all dynamic content items.\
   To apply the new format to previously created items, you must republish them.
4. Restart the application.

To remove the parent item of *any* type, perform the same procedure as above but use the following code snippet:

```C#
using System;
using Telerik.Sitefinity.DynamicModules.Model;

namespace SitefinityWebApp
{
    public class RemoveParentFromChildURLAllTypes : Telerik.Sitefinity.DynamicModules.Data.OpenAccessDynamicModuleProvider
    {
        private bool resolvingChildUrlFormat = false;

        /// <inheritdoc />
        public override string GetUrlFormat(Telerik.Sitefinity.GenericContent.Model.ILocatable item)
        {
            string urlFormat = "/[UrlName]";
            var resolvedItem = (DynamicContent)item;
            if (!this.resolvingChildUrlFormat && resolvedItem.SystemParentId != Guid.Empty)
            {
                this.resolvingChildUrlFormat = true;
            }
            else if (this.resolvingChildUrlFormat)
            {
                urlFormat = string.Empty;
                if (resolvedItem.SystemParentId == Guid.Empty)
                {
                    // Stop child url format resolving when we reach the topmost parent
                    this.resolvingChildUrlFormat = false;
                }
            }

            return urlFormat;
        }
    }
}
```
