Create internal URLs for data items programmatically

You can create custom short internal URLs (for example, ~/news(...)) for your data types programmatically. To do so, you should:

  1. Create your format for internal links to data items by implementing the interface IInternalUrlProvider.
  2. Register the provider.

Note. The code sample here uses a static page data folder "Demo.News".

Expand the sample code for Demo.News


Important: If you also define short URL names for data types (in the GUI or when creating a static data type), the internal URL format defined in the custom provider will be used when links are inserted. However, both sets of formats will equally work if used.

Creating a custom internal URL provider

To create a custom internal URL provider that will set the format for internal data links, you need to implement the interface IInternalUrlProvider.

using System;
using System.Linq;
using Composite.Core.Extensions;
using Composite.Core.Routing;
using Composite.Core.Types;
using Composite.Data;

namespace Demo
    class CustomDataUrlProvider : IInternalUrlProvider
        private readonly string _shortTypeName;
        private readonly Type _type;
        private readonly Type _keyType;

        public CustomDataUrlProvider(string shortTypeName, Type type)
            _type = type;
            _keyType = type.GetKeyProperties().Single().PropertyType;
            _shortTypeName = shortTypeName;

        public string BuildInternalUrl(IDataReference reference)
            string serializedKey = ValueTypeConverter.Convert<string>(reference.KeyValue);
            if (string.IsNullOrEmpty(serializedKey))
                return null;
            return "~/{0}({1})".FormatWith(_shortTypeName, serializedKey);

Download CustomDataUrlProvider.cs

Expand the source code for IInternalUrlProvider

Registering the internal link format with the provider

Now that you've created a custom internal URL provider, you need to register it. You can do it on the application startup:

using System.Linq;
using Composite.Core.Application;
using Composite.Core.Routing;
using Composite.Data;
using Composite.Data.DynamicTypes;

namespace Demo
    public class Startup
        public static void OnBeforeInitialize()


        public static void OnInitialized()
            var newsDataType = DataFacade.GetAllInterfaces().FirstOrDefault(u => u.Name == "News");
            InternalUrls.Register(newsDataType, new CustomDataUrlProvider("news", newsDataType));
Important. Place the initialization code in onInitialized(). Code will throw an exception when accessing methods related to data façade in OnBeforeInitialize().
 Note. In the sample above, as Step 1, we also make sure that the Demo.News data folder has been created with EnsureCreateStore before making use of it.

Converting internal links into public URLs

The internal links you get with this sample will look like ~/news(...), and you will be able to insert them in the content referring to a specific news item. However, you'll need to explicitly convert them so that they resolve as public URLs to data items when accessed on the website.

You can do it programmatically by creating a custom URL converter, or you can make use of RoutedData<T> to build a list-to-detail view on a page (see below the sample CMS Function that does that.)

Using the sample

To test this sample, first create the provider and add it to your website:

  1. In Visual Studio, create a class library project using the sample code above for the data type, provider and the application startup.
  2. Build and copy the resulting DLL to ~/Bin on your website.
  3. Restart the server.

Next, add the "News" data folder to a page:

  1. Log in to the CMS Console.
  2. From the "Content" perspective, create a page.
  3. Add the "News" data folder to the page.
  4. Add a few news items to the folder.

Finally, create and use a CMS Function to list the news items and view their details:

  1. From the "Functions" perspective, create a Razor function.
  2. Replace the code in the function with the code below.

    Show the code of the function

  3. Save the function.
  4. Insert the function on the page from the steps above.
  5. Save and publish the page.

Now you can open the page and click the links to individual news items to view the news item's details.


C1 CMS version 5.0 or later