You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sitecore 8.1 Update 2:非管理员自定义解锁按钮添加及报错解决

为非管理员作者在Sitecore 8.1 Update 2的Experience Editor中添加自定义解锁项按钮及错误修复

我来帮你一步步解决这个问题,包括实现自定义解锁按钮,以及修复你遇到的请求处理错误:

1. 创建自定义解锁命令类

默认的UnlockItem命令仅允许管理员执行,我们需要编写一个自定义命令,让指定角色(比如作者角色)的用户能解锁其他用户锁定的项。创建如下C#类:

using Sitecore;
using Sitecore.Shell.Applications.WebEdit.Commands;
using Sitecore.Security.Accounts;

namespace YourProject.WebEdit.Commands
{
    public class CustomUnlockItem : UnlockItem
    {
        public override void Run(Sitecore.Shell.Framework.Commands.CommandContext context)
        {
            var currentUser = Context.User;
            // 允许管理员和作者角色执行解锁操作
            if (currentUser.IsInRole(@"sitecore\Author") || currentUser.IsAdministrator)
            {
                foreach (var item in context.Items)
                {
                    if (item.Locking.CanUnlock())
                    {
                        item.Locking.Unlock();
                    }
                }
                // 刷新Experience Editor界面
                Context.ClientPage.SendMessage(this, "item:refreshchildren");
            }
            else
            {
                // 无权限时弹出提示
                Context.ClientPage.ClientResponse.Alert("你没有权限执行此操作");
            }
        }
    }
}

编译这个类,将生成的DLL放到网站的bin目录下。

2. 注册自定义命令

创建一个Sitecore patch配置文件(比如App_Config/Include/CustomWebEditCommands.config),注册我们的自定义命令:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <commands>
      <command name="webedit:customunlockitem" type="YourProject.WebEdit.Commands.CustomUnlockItem, YourProject"/>
    </commands>
  </sitecore>
</configuration>

注意替换YourProject.WebEdit.Commands.CustomUnlockItem和YourProject为你实际的命名空间和程序集名称。

3. 在Experience Editor中添加自定义按钮

切换到core数据库,完成按钮的创建和配置:

  • 导航到/sitecore/content/Applications/WebEdit/Custom Experience Buttons,右键插入一个Web Edit Button。
  • 设置按钮属性:
    • Name: CustomUnlockItem
    • Display Name: 解锁项
    • Command: webedit:customunlockitem
    • Icon: 选择合适的图标(比如Office/32x32/lock_open.png)
    • Tooltip: 解锁当前项(即使被其他用户锁定)
  • 保存项后,导航到你想放置按钮的工具栏位置(比如/sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Tabs/Page/Edit/Controls),插入一个按钮控件,将Click字段设置为webedit:customunlockitem,匹配之前的配置。

4. 修复你遇到的请求错误

你碰到的错误System.InvalidOperationException: Could not retrieve request class for url:/-/speak/request/v1/expeditor/ExperienceEditor.CustomUnlockItem,核心原因是:

按钮命令配置错误,使用了Speak请求的URL格式,但你的自定义命令并未注册为Speak请求处理类,且命令名不匹配。

修复步骤:

  • 确保所有按钮的Command/Click字段都设置为我们注册的webedit:customunlockitem,而非ExperienceEditor.CustomUnlockItem。
  • 检查patch配置文件中的命令名是否和按钮配置完全一致,避免拼写错误。
  • Sitecore 8.1 Update 2的WebEdit场景下,使用传统命令方式即可,无需额外实现Speak请求接口。

5. 测试验证

用作者角色的用户登录Sitecore,打开Experience Editor找到一个被其他用户锁定的项,点击自定义解锁按钮,应该能成功解锁且无权限报错。

内容的提问来源于stack exchange,提问作者Ravi Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:54:57