Working With Custom Hooks: OnMarkForPublishAsset
Marking a content item for publish is a crucial step in the publishing process. This custom hook can help administrators control how assets are published - and who is allowed to publish them.
In the context of the Ingeniux CMS platform, assets refer to content items like images, PDF documents, and even sometimes code that are stored in the CMS and published out to a publishing target. Assets differ from page types and components – which are created, edited, and managed directly in the CMS Site Tree – in that they are uploaded, or brought into the CMS, from outside of the platform. Once uploaded, they are stored in the Asset Management System (and usually placed into an Asset Schema) where they can be managed, edited in certain ways, and have metadata assigned to them.
Marking a content item for publish is one of the most important actions in the CMS because it determines which content items can be published.
Marking an item in the Asset Tree for publish flags a previously checked-in version of that item as available for publish, whereas items that are not marked for publish cannot be published at all.
Users in the CMS can decide which version of the item gets marked for publish. In most cases, the CMS will automatically select the most recent version of the content item to be marked for publish, though this can be changed to a previous version if desired.
Only content items in the Asset Tree that have been checked-in at least once can be marked for publish.
Administrators can control marking assets for publish through combinations of group permissions, security to Asset Tree areas, and security to publishing targets.
How To Trigger the Hook
This hook may be triggered when a user takes the following action(s):
- Right-clicking on an Asset in the Asset Tree and selecting Mark/Unmark for Publish, or on an Asset Folder and selecting Mark/Unmark Descendant Assets for Publish. (Note that all descendants will trigger the hook individually if done from an Asset Folder.)
- Clicking on an Asset in the Asset Tree and selecting the Mark/Unmark for Publish button in the Main Pane Toolbar.
- Changing Mark for Publish settings in the Asset Properties dialogue by adding a new checked mark. (Note that unchecking and checking the box in the same dialog session won’t trigger the hook.) The Asset Properties dialog is reached by right-clicking on a page and selecting Properties; or by clicking an asset, navigating to the Properties tab, and selecting the Publish Management section.
- Moving an Asset through a transition in Workflow that includes a Mark for Publish action.
- Taking an action that triggers a Mark for Publish action using the CSAPI or the Ingeniux REST API.
When to Use This Custom Hook
You can take advantage of this trigger to enhance your CMS functionality in many ways, including doing checks on associated content items, verifying that data in the content item is complete in advanced ways, or performing other accompanying actions that need to be completed as the content item gets ready to publish.
Considerations
This hook has the ability to trigger on a Site Tree content item and all of its descendants, so be careful to select only the content you want to publish in order to prevent slowing down your CMS. As always you should watch out for calling a Mark for Publish action from within the hook, as you could easily trigger infinite loops.
Example
Notify a Group when an asset is Marked for Publish
In this example, let’s pretend that there are a group of stakeholders at your company that want to know when a certain type of content is marked for publish. In this case, a group of marketing managers want to know every time an asset content item that uses the code schema is marked for publish.
First, test to make sure the newly marked item is of the specified schema. Then, get the user group object to notify and collect the users from in that group. As long as there are users in that group, put all of those users’ emails into a string separated by commas so you can parse through it later. In a try, set the values for the variables necessary for sending an email through the SMTPClient, set up and connect to the SMTP server, and feed the values to the emailer. Catch any errors that come up when trying to go through the SMTP emailer, as is best practice when connecting to an outside service.
//OnAfterMarkForPublish
//Send a notification to another department
//If the content item is Code > send email notification to a Dev Review (UserGroup/1) group.
//using System.Net.Mail; this namespace is required to send emails with SMTP
if (contentItem.SchemaName == "Code")
{
var groupUsers = session.UserManager.UserGroup("UserGroup/1").Users(out int count);
List<string> usersEmail = new List<string>();
string emailList = "";
if(groupUsers != null)
{
foreach (var user in groupUsers)
{
if (user != null && !string.IsNullOrWhiteSpace(user.EmailAddress))
{
usersEmail.Add(user.EmailAddress);
}
}
emailList = string.Join(",", usersEmail.ToArray());
}
try
{
var formsSmtpServer = "mx.ingeniuxondemand.com";
var formsSmtpServerPort = 25;
var formsSmtpEnableSSL = false;
var formsSmtpUsername = "";
var formsSmtpPassword = "";
var fromEmail = "noreply@ingeniux.com";
SmtpClient smtpServer = new SmtpClient(formsSmtpServer);
if (formsSmtpEnableSSL)
{
smtpServer.Credentials = new System.Net.NetworkCredential(
formsSmtpUsername,
formsSmtpPassword);
smtpServer.EnableSsl = true;
}
smtpServer.Port = formsSmtpServerPort;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromEmail);
mail.To.Add("paredesjaime@icloud.com");
if (!string.IsNullOrWhiteSpace(emailList))
{
mail.CC.Add(emailList);
}
mail.Subject = "OnAfterMarkForPublish";
mail.Body = "<h2>Hello Your Code has been Marked for Publish 2</h2>";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
smtpServer.Send(mail);
}
catch (Exception e)
{
session.Info("Error sending email" + e);
}
}
Ways to Extend the Functionality Further
There are many ways to extend this hook. Here are a few examples:
Give yourself a more manageable connection between the content that is changing and the group that needs to be notified, instead of hardcoding it into the script. For instance, you could:
- Add a field on the asset that specifies which group owns the content.
- Tag the content item with a category that ties the group and content together.
- Store a CSV of groups and content IDs.
- Set schema and group connections if they always match.
Set up the emailer as a function that you can call instead of instantiating it directly in this hook, since you will most likely want to use it with other scripts.
There are no comments yet.