How to create workflow users and roles programmatically?

Primary tabs

Replies

There are multiple ways, just use Create method directly on classes Security.Roles, and Security.Users

do ##class(Security.Roles).Create(Name, Description, Resources, GrantedRoles)
do ##class(Security.Users).Create(UserName, Roles, Password)

Or, you can use %Installer manifest

<Resource
    Name="%accounting_user" 
    Description="Accounting"
    Permission="RW"/>
<Role 
    Name="%DB_USER"
    Description="Database user"
    Resources="MyResource:RW,MyResource1:RWU"
    RolesGranted= />
<User 
    Username="Clerk1"
    PasswordVar="clerk1pw"
    Roles="Dataentry"
    Fullname="Data Entry Clerk"
    Namespace=
    Routine=
    ExpirationDate=
    ChangePassword=
    Enabled=
    Comment=""/>

I've asked about Workflow users and roles. But I can use your answer to improve my script. Thank you!

set user = $username
set role = "MyOperation"
set sc = ##class(EnsLib.Workflow.UserDefinition).CreateUser(user)
set sc = ##class(EnsLib.Workflow.RoleDefinition).CreateRole(role)
set sc = ##class(EnsLib.Workflow.RoleDefinition).AddUserToRole(role, user)

Export role/user globals as xml.