在Page Editor模式下添加自定义按钮

       在Sitecore中,我们已经很熟悉使用Page Editor模式来编辑页面内容。但当在Page Editor模式下新建节点时,Sitecore只能新建一个Item,除了设置过Standard Value值的字段外,不能立即对Item的字段赋值。本文我们就通过添加自定义按钮的方式,实现新建一个item后立即修改item字段的功能。

       1. 在项目中新建一个类,用来实现编辑字段的弹出窗功能:

internal class FieldEditInfosCommand : FieldEditorCommand
    {
        protected override PageEditFieldEditorOptions GetOptions(ClientPipelineArgs args, NameValueCollection form)
        {
            ItemUri uri = ItemUri.Parse(args.Parameters["uri"]);
            Item item = Database.GetItem(uri);
            return new PageEditFieldEditorOptions(form, new List<FieldDescriptor>
            {
                new FieldDescriptor(item, "Title"),
            })
            {
                Title = "Info Field Editor",
                Text = "Make changes to info item fields",
                Icon = "people/16x16/cubes_blue.png"
            };
        }

        protected new void StartFieldEditor(ClientPipelineArgs args)
        {
            HttpContext current = HttpContext.Current;
            if (current != null)
            {
                var page = current.Handler as Page;
                if (page != null)
                {
                    NameValueCollection form = page.Request.Form;
                    if (form != null)
                    {
                        if (!args.IsPostBack)
                        {
                            string javascript = "scSave()";
                            SheerResponse.Eval(javascript);
                            SheerResponse.ShowModalDialog(GetOptions(args, form).ToUrlString().ToString(), "720", "480",
                                string.Empty, true);
                            args.WaitForPostBack();
                            return;
                        }
                        if (args.HasResult)
                        {
                            Item item = null;
                            foreach (FieldDescriptor current2 in PageEditFieldEditorOptions.Parse(args.Result).Fields)
                            {
                                Item item2 = Context.ContentDatabase.GetItem(current2.ItemUri.ToDataUri());
                                if (item == null &&
                                    item2.TemplateID.ToString().Equals("{2AFEDA7B-2D79-4778-9CB4-6E090667C001}"))
                                {
                                    item = item2;
                                }
                                using (new SecurityDisabler())
                                {
                                    item2.Editing.BeginEdit();
                                    item2[current2.FieldID] = current2.Value;
                                    item2.Editing.EndEdit();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

       2. 新建一个类,用来实现添加一个item的功能:

      

class CreateNewEditFrameCommand : New
    {
        public override void Execute(CommandContext context)
        {
            context.Parameters["itemid"] = context.Parameters["id"];
            base.Execute(context);
        }
        protected new void Run(ClientPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            string itemPath = args.Parameters["itemid"];
            Language language = Language.Parse(args.Parameters["language"]);
            Item itemNotNull = Client.GetItemNotNull(itemPath, language);
            if (args.IsPostBack)
            {
                if (args.HasResult)
                {
                    string[] array = args.Result.Split(new char[]
					{
						','
					});
                    string masterName = array[0];
                    string name = array[1];
                    Database database = Factory.GetDatabase("master");
                    BranchItem branchItem = database.Branches[masterName, itemNotNull.Language];
                    Assert.IsNotNull(branchItem, typeof(BranchItem));
                    this.ExecuteCommand(itemNotNull, branchItem, name);
                    Client.Site.Notifications.Disabled = true;
                    Item item = Context.Workflow.AddItem(name, branchItem, itemNotNull);
                    Client.Site.Notifications.Disabled = false;
                    if (item != null)
                    {
                        this.PolicyBasedUnlock(item);
                        item.Editing.BeginEdit();
                        CommandContext commandContext = new CommandContext(item);
                        commandContext.Parameters.Add("uri", item.Uri.ToString());
                        Command command = CommandManager.GetCommand("fieldedit:editinfo");
                        command.Execute(commandContext);
                        return;
                    }
                }
            }
            else
            {
                if (!itemNotNull.Access.CanCreate())
                {
                    SheerResponse.Alert("You do not have permission to create an item here.", new string[0]);
                    return;
                }
                UrlString urlString = ResourceUri.Parse("control:Applications.WebEdit.Dialogs.AddMaster").ToUrlString();
                itemNotNull.Uri.AddToUrlString(urlString);
                SheerResponse.ShowModalDialog(urlString.ToString(), true);
                args.WaitForPostBack();
            }
            
        }
    }

        3. 在Commands.config文件中注册新command:

        <command name="fieldedit:createinfo" type="Demo.Services.CreateNewEditFrameCommand, Demo.Services"/>
      <command name="fieldedit:editinfo" type="Demo.Services.FieldEditInfosCommand, Demo.Services"/>

        4. 登陆到sitecore后台,切换到core数据库,在/sitecore/content/Applications/WebEdit/Custom Experience Buttons下面,以“/sitecore/templates/System/WebEdit/WebEdit Button”为模板新建一个item,并修改其属性:

       


       5. 保存,切换回master数据库。在需要的sublayout上添加该按钮。

       6. 打开Page Editor ,测试该按钮。

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值