Knowledge Base

Working With Custom Hooks: OnPasteSpecial

Copy/paste is one of the most commonly used actions in the Site Tree. Learn how to enhance this seemingly simple functionality with the OnPasteSpecial custom hook.


When working in the CMS, it is a fairly common practice for users to copy and paste Content Items in the Site Tree.  

Users can trigger this custom hook manually using the following methods: 

  • Click-to-select a Content Item or multiple Content Items in the Site Tree, hold ALT, and drag the Content Items to a new location in the Site Tree.  

Most of the time, it’s easy and practical to copy/paste Content Items in the Site Tree. But there can be unforeseen downsides to doing this.  

Let’s imagine a user copy/pastes an article in their site with the intention of swapping out the title and body copy but forgets that the taxonomy categories associated with that article page were also copied over. Then, they forget to update the taxonomy categories on the new page. When this user publishes their new article, it will be categorized incorrectly, which could negatively impact the user experience and search experience on their site.  

The above scenario illustrates the importance of remembering that when a content item is copied, everything about that item is included in the new/copied version including all content, components, applied taxonomy categories, child pages, relevant workflows, and more. 

Considerations 

Unlike a normal copy/paste action in the Site Tree, for this hook to trigger users must hold the ALT key while dragging the content to a new location. This action will trigger a script to process the Content Item in the desired way. This is essential, as it allows the user to determine when the custom hook is triggered on a copy/paste and when it is not. When you wish to perform a normal copy/paste without triggering the hook, simply use another copy/paste method (like right-clicking the Content Item and selecting copy/paste from the Context Menu).  

We provide examples of custom hooks for both the copied Content Item and the new copy of the Content Item below.  

It's possible that a user could accidentally hold ALT while dragging an item when meaning to hold CTRL. So, it’s best to make sure that the process you are triggering is not irreversible or highly impactful. 

Examples 

Clone a Copied Page to Other Region Root 

In this example, the CMS Worldview system is being used to manage either lingual or region-specific websites. The base content of the websites is cloned from the master site and then adjusted in the clones. The CMS provides the connection between the master and clones to allow for translation or manual adjustment of the content.  

In this scenario not all content needs to be cloned from the master site, especially content that might be specific to a region such as events or directories of personnel. To accommodate this, the script takes advantage of the ability to prompt a special copy action (by holding ALT while dragging and dropping content) to determine that a new page in the master site needs to be copied to the clone website. This is one of many potential actions that may assist in the management of content across multiple websites.  

The first portion of the script happens in the hook and prepares the original Content Item for cloning. The item to be cloned is saved and checked in, and then a function (CreateClonePageAfterPaste) is called to create a clone of the new page. 

The CreateClonePageAfterPaste function finds the parent of the newly copied item and collects the Content Items that are mapped as clones of that item. If there are any, the script attempts to make copies of the new item into those areas of the site and map them back as clones to the new item, catching errors as it makes the attempts. 

Inside of OnBeforePasteSpecial custom hook:

int count; 

var pubTargets = session.PublishingManager.Targets(out count); 

//page needs to be saved and checked in after being pasted 

newPage.Save(); 

newPage.CheckIn(pubTargets, false); 

CreateClonePageAfterPaste(newPage, session); 

At the bottom of customhooks.cs, below the list of hooks:

///Function for Creating clone  

public void CreateClonePageAfterPaste(IPage page, IUserWriteSession session) 

{ 

int count; 

IPage parentPage = page.Parent(); 

if (parentPage == null) 

{ 

return; 

} 

IEnumerable<ILingualPageMap> maps = session.LingualMappingManager.MapsByMaster(parentPage.Id, out count); 

foreach (ILingualPageMap map in maps) 

{ 

CrossLocaleUtilities util = new CrossLocaleUtilities(session as IUserWriteSession); 

string[] badRefs; 

IPage cloneParent = session.Site.Page(map.ClonePageID); 

if (cloneParent == null) 

{ 

continue; 

} 

try 

{ 

string locale = map.ClonePageLocale; 

var cloneMaps = session.LingualMappingManager.MapsByMaster(page, out count); 

if (cloneMaps.Any(c => c.ClonePageLocale == locale)) 

{ 

continue; 

} 

util.CrossLocaleCopy(page, cloneParent, EnumCopyActions.IGX_MAKE_CHILD, locale, out badRefs); 

} 

catch (Exception e) 

{ 

session.Info("New page cloning failed for item: " + page.Id + " - " + page.Name + ". Reason: " + e.Message + ". Stack: " + e.StackTrace); 

} 

} 

The above scenario is a great example of how you might write scripts to streamline your work in the Worldview system. Worldview is a complex and variable system so plan your usage of custom hooks accordingly while considering how your users will work within the system. 

Possible Enhancements 

There are a lot of variables to consider before setting up automation within sites that are implemented to meet unique requirements. You could consider using similar scripts in new item creation as well as copying, since users are likely to employ many different methods of creating new content.  

Since you cannot message your users to let them know the item was completed successfully, you should consider integrating logging and reporting that is accessible to your users. Workflow could also be a useful tool to notify the person that needs to work on the newly created clones that work is needed.  

  • PRODUCT: CMS
  • VERSION: CMS 10
  • RELEASE: 10.x
  • Published: March 23, 2023
  • LAST UPDATED: September 18, 2023
  • Comments: 0

Please login to comment

Comments


There are no comments yet.