HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback

Approval definitions

Describes the process of creating a content approval definition.

Create an approval definition

This example shows the creation and saving of a definition:

using EPiServer.Approvals.ContentApprovals;
    
    ContentReference contentLink;
    IApprovalDefinitionRepository definitionRepository;
    
    var langEN = new CultureInfo[] { CultureInfo.GetCultureInfo("en") };
    var langSV = new CultureInfo[] { CultureInfo.GetCultureInfo("sv") };
    
    // Creates a content approval definition
    var definition = new ContentApprovalDefinition
    {
        ContentLink = contentLink,
        Steps = new List<ApprovalDefinitionStep>
        {
            new ApprovalDefinitionStep("step1", new ApprovalDefinitionReviewer[]
            {
                new ApprovalDefinitionReviewer("user1a", langEN),
                new ApprovalDefinitionReviewer("user1b", langSV),
            }),
            new ApprovalDefinitionStep("step2", new ApprovalDefinitionReviewer[]
            {
                new ApprovalDefinitionReviewer("user2a", langEN.Union(langSV))
            })
        }
    };
    
    // Saves the definition
    await definitionRepository.SaveAsync(definition);

Update an approval definition

In this example, a definition is updated with a new reviewer in the second step. This reviewer can approve items in all languages using CultureInfo.InvariantCulture.

using EPiServer.Approvals.ContentApprovals;
    
    ContentReference contentLink;
    IApprovalDefinitionRepository definitionRepository;
    
    var langInvariant = new CultureInfo[] { CultureInfo.InvariantCulture };
    
    // Gets a definition
    ApprovalDefinition definition = await definitionRepository.GetAsync(contentLink);
    definition = definition.CreateWritableClone();
    definition.Steps[1].Reviewers.Add(new ApprovalDefinitionReviewer("user2b", langInvariant));
    
    // Saves a definition
    await definitionRepository.SaveAsync(definition);

Add a role to an approval definition

(New in CMS UI 10.10.0)

In this example, a definition is updated with a new role reviewer in the first step. This role can approve items in the Swedish language.

using EPiServer.Approvals.ContentApprovals;
    
    ContentReference contentLink;
    IApprovalDefinitionRepository definitionRepository;
    
    var langSV = new CultureInfo[] { CultureInfo.GetCultureInfo("sv")};
    
    // Gets a definition
    ApprovalDefinition definition = await definitionRepository.GetAsync(contentLink);
    definition = definition.CreateWritableClone();
    definition.Steps[0].Reviewers.Add(new ApprovalDefinitionReviewer("managers", langSV, ApprovalDefinitionReviewerType.Role));
    
    // Saves a definition
    await definitionRepository.SaveAsync(definition);

Delete an approval definition

This example deletes a definition using ID:

ApprovalDefinition definition;
    IApprovalDefinitionRepository definitionRepository;
    
    // Deletes a definition
    await definitionRepository.DeleteAsync(definition.ID);

📘

Note

A definition cannot be deleted if running approvals exist. All approval instances must be completed or aborted before deleting an approval definition.

Get an approval definition

Gets a definition in a couple of different ways:

using EPiServer.Approvals.ContentApprovals;
    
    ContentReference contentLink;
    ApprovalDefinition definition;
    Approval approval;
    IApprovalDefinitionRepository definitionRepository;
    IApprovalDefinitionVersionRepository definitionVersionRepository;
    
    // Gets the latest version of a definition using a definition id.
    var definition1 = await definitionRepository.GetAsync(definition.ID);
    
    // Gets a specific version of a definition using a version id.
    var definition2 = await definitionVersionRepository.GetAsync(approval.VersionID);
    
    // Gets the latest version of a definition using a ContentReference. 
    var definition3 = await definitionRepository.GetAsync(contentLink);
    
    // Gets the latest version of a definition by resolving a ContentReference.  
    var definitionResolveResult = await definitionRepository.ResolveAsync(contentLink);
    // The Resolve-method returns a result with a definition and a flag specifying if the definition was found on an ancestor
    var definition4 = definitionResolveResult.Definition as ContentApprovalDefinition;
    var isInherited = definitionResolveResult.IsInherited;