1. Script examples

Action script to create a project from a template

This example shows how to create a project from a template using an action script. The script applies a copy profile, sets the new project's name, and assigns its owner. Users run the script through an external action on the template project's details page. Note that project name and owner are used to demonstrate this script, but you can use your own properties and relations.

Before you begin
  • Set a project as a template.

  • Ensure that the copy profile with identifier M.EntityCopyProfile.M.Project.Block.Newentitiesfromtemplate is available. Alternatively, you can use your own copy profile.

  • Ensure that the user with identifier User.Superuser exists, or update the script to reference the user you want to assign as the project owner.

Script

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Stylelabs.M.Sdk.Contracts.Base;
using Stylelabs.M.Sdk.Models.Content;

var templateBlock = Context.Target as IEntity;
var templateBlockId = templateBlock.Id;
if (!templateBlockId.HasValue) return;

var userQuery = Query.CreateQuery(entities =>
    from e in entities
    where e.Identifier == "User.Superuser"
    select e);

long? userId = await MClient.Querying.SingleIdAsync(userQuery);
if (!userId.HasValue) return;

// Available copy methods: Ignore, Keep, Overwrite, Remove
var copyOptions = new EntityCopyOptionsBuilder()
    .WithProperty("BlockName", PropertyCopyMethod.Overwrite, "From template " + templateBlockId)
    .WithRelation("UserOwnerToBlock", RelationCopyMethod.Overwrite, null, new List<long> { userId.Value })
    .WithCopyProfile("M.EntityCopyProfile.M.Project.Block.Newentitiesfromtemplate")
    .Build();

long copyBlockId = await MClient.Entities.CopyAsync(templateBlockId.Value, copyOptions);

MClient.Logger.Info($"Created new M.Project.Block entity with id {copyBlockId} from template {templateBlockId}.");

Script explanation

This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.

  1. The sample reads the template entity from Context.Target and exits if its ID has no value. A template ID can alternatively be supplied directly or retrieved using an identifier.

  2. The query then retrieves the ID of the user with identifier User.Superuser. The script needs this ID to overwrite the UserOwnerToBlock relation and exits if the query returns no ID.

  3. Configure the copy options:

    • WithProperty sets BlockName to From template followed by the template ID.
    • WithRelation sets UserOwnerToBlock to the retrieved user ID.
    • WithCopyProfile selects M.EntityCopyProfile.M.Project.Block.Newentitiesfromtemplate.
    • Build creates the options passed to CopyAsync.

    You can configure copy options directly, through a copy profile, or by combining both approaches. When you combine them, direct copy options take precedence over the copy profile.

    Some project member values cannot be overwritten when copying a project entity. These members are identified by comments in the default copy profile.

Setup

To set up your script, action, and entity operation:

  1. Create, publish, and enable an Action script.
  2. Create an action of type Action script and link it with the script.
  3. On the template project’s details page, configure the Entity operations:
    • Add an External action operation.
    • Under Actions, click + Add, and select the action created in step 2.
    • Click Save and close.
  4. Open the template project’s details page and run the external action to create a project.
If you have suggestions for improving this article, let us know!