Working With Custom Hooks: OnBeforeCopy and OnAfterCopy

Product: CMS

Version: CMS 10

Release: 10.x

Published: July 22, 2022

Last updated: 7/22/2022

Comments:
0 Comments

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 action manually using the following methods:

  • Right-clicking on the content item in the Site Tree and selecting copy, then right-clicking on another content item (e.g., a folder or page) and selecting paste or paste without children.
  • Highlighting a content item or multiple content items in the Site Tree, holding CTRL, and dragging the content item(s) to a new location in the Site Tree.

There are many useful and timesaving reasons to do this, but sometimes copying and pasting a content item can lead to unanticipated problems. For example, let’s imagine a user copy/pastes an article page 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.

You can take advantage of this custom hook to help users avoid some of the mistakes or oversights commonly made when copying and pasting content in the Site Tree on both the copied content item and the new version of the content item.

Considerations

Keep in mind that the hook does not trigger on the action of copy being selected from menus, but rather the subsequent paste action. Additionally, this hook is not called by using cut and paste from the right-click context menu – it only applies to copy and paste actions.

As with all hooks that can simultaneously be called by many triggers, you should take care to think about scenarios where a user has inadvertently copied many content items. When a content item with child content items is copied, all of the child content items are also copied. So, when that content item is subsequently pasted, the system automatically wants to paste all of the child content items as well and the hook will process against all of the items that were copied. In order to avoid this, users will need to select paste without children when they paste the copied content item.

If a user is copying and pasting a large number of content items, it may take the hook a while to process all of the copies.

Examples

Check out and assign the copy

If you are working in Ingeniux CMS Version 10.3 or below, when you copy and paste a content item the new version will appear in the Site Tree checked-in and unassigned to any user. Often, because of permission restrictions, a user is not allowed to check pages out, which could lead to them being unable to work on the copy they just created. To fix this you can use this hook to check out the new item and assign it to the operating user from the session.

if (!newPage.CheckedOut)

            {

                newPage.CheckOut(false);

                newPage.AssignUser(session.OperatingUser, "Copied paged automatically assigned to copying user.");

            }

There are other ways you can enhance this hook, including assigning it to a new workflow or checking that the user has access before assigning and assigning it to a group if the user does not have access.

Clear out content in the new item that shouldn’t be copied

When a user copies a content item they may forget about some of the schema fields that contain content that will get copied over, things like SEO content or social media meta data. You can use this hook to ignore or remove content in certain fields or set them back to a default setting, so the user doesn’t send content live that they didn’t realize existed. You can individually get all the elements that you want to clear, or in this example you will grab an entire group element and clear all of fields contained inside.

if(newPage.Element("TestGroup") != null)

            {

                var testGroup = newPage.Element("TestGroup") as IGroupElement;

                foreach (var ele in testGroup.ChildItems())

                {

                    ele.Value = "";

                }

            }

To enhance this script, you may consider that the test group in the example only has text and checkbox fields. Not all fields are cleared by setting their value to an empty string so you may need to use different methods for different elements. You may also be setting the items back to default values as well, such as setting a search Priority field back to 0.5 instead of to blank.


Please login to comment

Comments

There are no comments yet.