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:
- In Visual Studio, open the context menu of the CustomTypesproject and click Add » Class.
- Name the class file
CustomPageProvider.csand click Add. - Make the
CustomPageProviderclass inherit theOpenAccessPageProviderclass and implement theIOpenAccessMetadataProviderinterface.
IYou use theContentProviderDecoratorAttributeto tell the provider class which data provider decorator to use. - Implement the
GetMetaDataSourcemethod to return theCustomMetaDataSource. - Implement the following operations:
Create custom typeGet custom typeGet custom typesDelete 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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.