8

How to create Sitecore Context Item to copy Item Path using Sitecore PowerShell Extensions.

Like in below screen we have for Layout Copy Renderings and Quick download package. Sometimes it requires to copy item path and paste in the item's field such as Image field types.

Sitecore Item Context Menu

It would be really quick for editors if they can copy Item Path or Item ID with right-click instead of opening the item and going back & forth many times.

1
  • I tried to do this exact thing a few months ago and unfortunately @kasaku is right. One solution might be to create an alert that pops up an input dialog so the user can copy from the dialog, although not much better than just copying from the Quick Info section. Oct 3, 2016 at 23:28

2 Answers 2

13

I wouldn't use Sitecore Powershell Extensions for this. SPE doesn't have anything for interacting with the clipboard and also doesn't execute custom JS from scripts (yet!).

Additionally, there already exists a command within Sitecore for copying the path to the clipboard, so all you need to do is assign it to a new button in the context menu.

John West has blogged about this here - https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/repost-add-a-command-to-the-sitecore-item-context-menu - but to summarise:

  1. Create a new item of template System/Menus/Menu item under the item /Sitecore/Content/Applications/Content Editor/Context Menues/Default in the Core database.

  2. This item should have the message field set to clipboard:copypathtoclipboard(id=$Target).

  3. The command should then be executable from the context menu.

This message actually invokes a command, which is specified in Commands.config as Sitecore.Shell.Framework.Commands.Clipboard.CopyPathToClipboard,Sitecore.Kernel.

However, this command provided by Sitecore operates in Internet Explorer only. It specifically checks the browser and hides the menu option if it fails:

if (context.Items.Length != 1 || !UIUtil.IsIE())
       return CommandState.Hidden;

This is because traditionally only IE had support for interfacing with the OS clipboard. Other browsers had to make use of Flash solutions.

However, there looks to be more modern solutions for handling clipboard usage in other browsers, if that is important to you. For instance there is this library - https://github.com/zenorocha/clipboard.js

If supporting other browsers is important, you will need to implement your own custom command. I would look at the source for the CopyPathToClipboard type and implement something similar that executes your custom JS when executed.

4
  • IE only seems to be a big restriction. Would powershell allow to access system clipboard easier? Sep 15, 2016 at 15:50
  • 1
    No, through the browser the only means of interacting with the clipboard will be through JavaScript (or Flash). Using a command doesn't mean it's IE only, it's just the one that comes with Sitecore out-of-the-box is IE only. You'll have to implement a custom command, but it shouldn't be too difficult.
    – Kasaku
    Sep 15, 2016 at 16:21
  • @SzymonKuzniak this is the right way; just replace the Sitecore.Shell.Framework.Commands.Clipboard.CopyPathToClipboard with your own implementation. HTML5 Clipboard APIs have moderately good cross-browser support. See caniuse.com#search=clipboard for more info. Sep 18, 2016 at 0:34
  • 1
    You can now use the command Invoke-JavaScript. Jun 23, 2017 at 11:48
0

Based on Adam Najmanowicz blog post:

  1. Create a menu item for your script

Create a context menu item of template /sitecore/templates/System/Menus/Menu item within the /sitecore/content/Applications/Content Editor/Context Menues/Default/PowerShell/ branch in the core database. If you have more than just a few scripts, a more elaborate structure might need to be created to group your scripts by functionality/purpose.

  1. Bind it to your script to the menu item

Within the menu item you’ve just created edit the Message field and bind the script to it using the following pattern:

item:executescript(id=$Target,script={1680E211-BD28-49BE-82FB-DA7232814C62}, scriptDb=core)

where the script guid is the ID of your script and the scriptDb is the database the script is located in.

Since this is a simple Sitecore command – similarly you can use the PowerShell functionality to expose your script on any ribbon or other kinds of menus!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.