Extend Sitefinity CMS data model: Create the provider class

After you create the fluent mappings, you create the custom provider class.

Sitefinity's CMS architecture is based on the provider model design pattern, which provides a number of extensibility possibilities. The provider class in this example inherits from the OpenAccessPageProvider class that stores and retrieves page data. The exposed methods in the CustomPageProvider are used to create and retrieve objects from the database context.

To create the custom provider class, perform the following:

  1. In Visual Studio, open the context menu of the CustomTypesproject and click Add » Class.
  2. Name the class file CustomPageProvider.cs and click Add.
  3. Make the CustomPageProvider class inherit the OpenAccessPageProvider class and implement the IOpenAccessMetadataProvider interface.
    IYou use the ContentProviderDecoratorAttribute to tell the provider class which data provider decorator to use.
  4. Implement the GetMetaDataSource method to return the CustomMetaDataSource.
  5. Implement the following operations:
    • Create custom type
    • Get custom type
    • Get custom types
    • Delete custom type

Use the following code sample:

C#
using System;
using System.Linq;
using Telerik.OpenAccess.Metadata;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Data.Linq;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Modules.GenericContent.Data;
using Telerik.Sitefinity.Modules.Pages.Data;

namespace SitefinityWebApp
{
   [ContentProviderDecoratorAttribute(typeof(OpenAccessContentDecorator))]
   public class CustomPageProvider : OpenAccessPageProvider, IOpenAccessMetadataProvider
   {
       MetadataSource IOpenAccessMetadataProvider.GetMetaDataSource(IDatabaseMappingContext context)
       {
           return new CustomMetadataSource(context);
       }

       public CustomType CreateCustomType()
       {
           return this.CreateCustomType(this.GetNewGuid());
       }

       public CustomType CreateCustomType(Guid id)
       {
           if (id == Guid.Empty)
               throw new ArgumentException("Id cannot be an Empty Guid");

           var item = new CustomType(this.ApplicationName, id);
           ((IDataItem)item).Provider = this;
           this.GetContext().Add(item);
           return item;
       }

       public CustomType GetCustomType(Guid id)
       {
           if (id == Guid.Empty)
               throw new ArgumentException("Id cannot be an Empty Guid");

           var item = this.GetContext().GetItemById<CustomType>(id.ToString());
           ((IDataItem)item).Provider = this;
           return item;
       }

       public IQueryable<CustomType> GetCustomTypes()
       {
           var appName = this.ApplicationName;
           var query = from c in SitefinityQuery.Get<CustomType>(this)
                       where c.ApplicationName == appName
                       select c;
           return query;
       }

       public void Delete(CustomType item)
       {
           this.GetContext().Remove(item);
       }
   }    
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?