Modify permissions of dynamic content items using the Fluent API

You can use the fluent API to modify the permissions of dynamic content items. The entry point for managing a dynamic content item’s permissions is the ManagePermissions() extension method on DynamicContent, which is available in the Telerik.Sitefinity.Model namespace.

In the following example the permissions of a specific Article dynamic content item, are being modified by allowing the Designers role to view it:

C#
using System;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Utilities.TypeConverters;

namespace SitefinityWebApp
{
   public class ModifyPermissionsDynamicType
   {
       public void ModifyPermissionsDynamicContentType()
       {
           var myItemId = Guid.NewGuid();
           string myTypeName = "Telerik.Sitefinity.DynamicTypes.Model.Articles.Article";
           var myDynamicType = TypeResolutionService.ResolveType(myTypeName);

           DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
           
           var item = dynamicModuleManager.GetDataItem(myDynamicType, myItemId);
           item
                 .ManagePermissions()
                 .ForRole("Designers")
                 .Grant()
                 .View();

           dynamicModuleManager.SaveChanges();

       }
   }
}

The example above implicitly breaks the permission inheritance for the specific item. This is done by copying the permissions of the parent item and assigning those copies as the item’s own Permissions collection. Afterwards, the new permission objects are modified so that the Viewactions is allowed for the Designersrole.

In combination with dynamic content events, you can achieve more complex scenarios - for example, defaulting the permissions of all items of specific type to a desired state, as in the following example that is taken from a Global.asax.cs file.

C#
using System;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.DynamicModules.Events;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Services;

namespace SitefinityWebApp
{
   public class Global : System.Web.HttpApplication
   {

       protected void Application_Start(object sender, EventArgs e)
       {
           Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
       }

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           EventHub.Subscribe<IDynamicContentCreatingEvent>(this.HandleDynamicContentCreatingEvent);
       }
       private void HandleDynamicContentCreatingEvent(IDynamicContentCreatingEvent creatingEvent)
       {
           var articleTypeName = "Telerik.Sitefinity.DynmicTypes.Model.Articles.Article";
           creatingEvent.Item
              .When(dynamicContentItem => dynamicContentItem.GetType().Name == articleTypeName)
              .ManagePermissions()
              .ForRole("PublicUsers")
              .Grant().View()
              .Deny().Delete()
              .Deny().Modify();
       }
   }
}

As you can see in the example above, the creating event is used, which is raised just before the transaction, in which an item has been created is committed. This is why no SaveChanges call is required for the changes to take effect.

Additional resources

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?