Change the URL format of hierarchical dynamic modules
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. 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:
- 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:
In the above example, the URL of the items is based on the value of fields in the module, which are named
JobTitle are fields that you must create in the module, the
UrlName is a built-in field.
NOTE: All fields, used for the URL format must be text fields.
- Register the new provider.
Perform the following:
- In the backend, click Administration » Settings » Advanced.
- In the tree on the left, click DynamicModules » Provider » Create new.
- Enter a Name and Title of the new provider.
- 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
- Under the new provider, click Parameters » Create new.
- In Key, enter applicationName
- In Value, enter the name of your application, for example, myCustomProvider.
NOTE: Do not use
/DynamicModule for the value, because this name represents the default Sitefinity CMS provider.
- 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.
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.
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:
- Implement the custom provider.
- Create a new class and inherit from Telerik.Sitefinity.DynamicModules.Data.OpenAccessDynamicModuleProvider.
- Override the
GetUrlFormat that returns the format string and use the following code to remove the parent URL of the parent item:
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.
- Register the new provider in Sitefinity.
- Republish all dynamic content items.
To apply the new format to previously created items, you must republish them.
- Restart the application.
To remove the parent item of any type, perform the same procedure as above but use the following code snippet: