Knowledge Base

Working With Custom Hooks: OnMarkForPublish

Marking for publish is one of the most important actions in the CMS because it determines which content items can be published to the live site(s). Learn how to work with this custom hook.


Marking for publish is one of the most important actions in the CMS because it determines which content items can be published to the live site(s).  

Marking an item in the Site 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 Site Tree that have been checked-in at least once can be marked for publish. 

Users can trigger this action manually using the following methods: 

  • Right-clicking on the content item in the Site Tree and selecting Mark/Unmark for Publish > Page or Page and Children. Note that all descendants will trigger the hook individually if Page and Children are selected. 
  • Changing Mark for Publish settings in the Page 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. 
  • Moving a Site Tree content item through a transition in workflow that includes a Mark for Publish action. 
  • Calling a Mark for Publish method using the CSAPI or the Ingeniux REST API. 

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. 

Examples 

Notify a Group when a Page 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 a blog post 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 a Blog Post > send email notification to a Blog Owners group. 

//using System.Net.Mail;  this namespace is required to send emails with SMTP 

 

if (page.SchemaName == "Event") 

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 Blog Post 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); 

There are many ways to extend this hook. Here are a few examples:  

  1. Give yourself some 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: 
  2. Add a field on the page that specifies which group owns the content. 
  3. Tag the content item with a category that ties the group and content together. 
  4. Store a CSV of groups and content IDs. 
  5. Set schema and group connections if they always match. 
  6. 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. 
  • Published: December 7, 2022
  • LAST UPDATED: September 18, 2023
  • Comments: 0

Please login to comment

Comments


There are no comments yet.