Adding/removing permissions to create documents of a certain type in a script

This article provides examples of scripts for adding/removing permissions to create documents of a certain type. 

Adding permissions to a user to create documents of a certain type

Script without using PublicAPI

Context variable:

context.User - context variable of the User type

For the script to work correctly, add assemblies:
EleWise.ELMA.Documents
EleWise.ELMA.Documents.Docflow

Namespaces:

using EleWise.ELMA.Documents;
using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Documents.Metadata;
using EleWise.ELMA.Documents.Models;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Metadata;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Security;
using EleWise.ELMA.Services;
 

Script text:

//load data on permissions of a specify document type: instead of DocType specify your document type 
 var metadata = (DocumentMetadata)MetadataLoader.LoadMetadata(InterfaceActivator.TypeOf<IDocType>());
 var profile = DocumentMetadataProfileManager.Instance.LoadByDocumentType(metadata.ImplementationUid);  
 //check if the user has permissions to create documents
 var hasPermission = profile.Permissions.FirstOrDefault(p => p.User != null && 
       p.User.Id ==  context.User.Id && 
       p.PermissionId == EleWise.ELMA.Documents.PermissionProvider.DocumentMetadataCreateInstance.Id) != null;
 //if there are no permissions, add them
 if (!hasPermission) {
  profile.Permissions.Add(new InstanceOf<IDmsObjectDefaultPermissions> {
     New = {
    PermissionId = EleWise.ELMA.Documents.PermissionProvider.DocumentMetadataCreateInstance.Id,
    DocumentMetadataProfile = profile,
     TypeRoleId = CommonRoleTypes.User.Id,
    User = context.User
     }     
    }.New);
  profile.Save();
    }
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here

Context variable:

context.User - context variable of the User type

Namespace:

using EleWise.ELMA.API;
using EleWise.ELMA.Documents.Contracts.Models;
 

Script text:

PublicAPI.Docflow.Document.AddDocumentCreatePermission(context.User, InterfaceActivator.TypeOf<IContract>());
 

 

Removing permissions to create documents of a specific type

Script without using PublicAPI

Context variable:

context.User - context variable of the User type

For the script to work correctly, add assemblies:
EleWise.ELMA.Documents
EleWise.ELMA.Documents.Docflow

Namespaces:

using EleWise.ELMA.Documents;
using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Documents.Metadata;
using EleWise.ELMA.Documents.Models;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Metadata;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Security;
using EleWise.ELMA.Services;
 

Script text:

//load data on permissions of a specific document type: instead of Testblock insert your document type
        var metadata = (DocumentMetadata)MetadataLoader.LoadMetadata(InterfaceActivator.TypeOf<ITestblock>());
var profile = DocumentMetadataProfileManager.Instance.LoadByDocumentType(metadata.ImplementationUid);
//check if the user has permissions to create a document
var perm = profile.Permissions.FirstOrDefault(p => p.User != null &&
                                              p.User.Id ==  context.Poljzovatelj.Id && 
                                              p.PermissionId == EleWise.ELMA.Documents.PermissionProvider.DocumentMetadataCreateInstance.Id);
//if there are permissions, remove them
if (perm != null)
{
profile.Permissions.Remove(perm);
profile.Save();
}
 
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here

 

(Removing permissions to create documents of the Contract type).

Namespace:

using EleWise.ELMA.API;
using EleWise.ELMA.Documents.Contracts.Models;
 

Script text:

PublicAPI.Docflow.Document.DeleteDocumentCreatePermission(context.User, InterfaceActivator.TypeOf<IContract>());