28 April 2015

Refresh the page after deletion in Page Editor mode using Sitecore

(Working for Sitecore 7 and 6)

Recently, I have been working on Page Editor using Edit Frame in Sitecore. It works pretty well but one issue. As default, after delete item in Page Editor, the page will not refresh automatically which leave the Deleted item still appearing on the page. Apparently, it confuses Content Author and it is a bad User experience. I have found how to get over this issue nicely.

I have tried to create and add the custom webedit command after the command section to reload the parent page.


It is not working since the " <uiDeleteItems>" section in web.config will fire up after. It means if the item have any link associated with, it will pop up the "remove link" window which get refreshed by my code and cannot go further.

So I put my custom command code after "<processor method="Execute" mode="on" type="Sitecore.Shell.Framework.Pipelines.DeleteItems,Sitecore.Kernel">" in the section of
"<uiDeleteItems>" in web.config

Code:

public class CustomDeleteItems : ItemOperation
    {
        public void Reload(ClientPipelineArgs args)
        {
            SheerResponse.Eval("window.parent.location.reload();");
        }
    }


It is a progress but not enough. We need a a way to tell it only works for "Page Editor" mode since you don't want the page to be refreshed in Content Editor. I could not find a new way to do so it ends up with a lot of decompiling the .dll and it ends up with

public class CustomDelete : WebEditCommand
    {
        /// 
        /// Executes the command in the specified context.
        /// 
        /// 
        /// The context.        public override void Execute(CommandContext context)
        {
            Assert.ArgumentNotNull((object)context, "context");

            NameValueCollection parameters = new NameValueCollection();
            parameters["id"] = context.Parameters["id"];      
            Context.ClientPage.Start((object)this, "Run", parameters);
        }

        /// 
        /// Queries the state of the command.
        /// 
        /// 
        /// The context.
        ///                     /// 
        /// The state of the command.
        /// 
        /// 
        public override CommandState QueryState(CommandContext context)
        {
            Assert.ArgumentNotNull((object)context, "context");
            SiteContext site = Context.Site;
            bool flag = false;
            if (site != null && Context.PageMode.IsPageEditorEditing)
                flag = true;
            if (!flag && WebUtil.GetQueryString("mode") != "edit")
                return CommandState.Disabled;
            Item obj = context.Items.Length > 0 ? context.Items[0] : (Item)null;
            if (obj == null || !obj.Access.CanDelete() || obj.Appearance.ReadOnly)
                return CommandState.Disabled;
            return base.QueryState(context);
        }

        /// 
        /// Runs the pipeline.
        /// 
        /// 
        /// The arguments.        protected void Run(ClientPipelineArgs args)
        {
            Assert.ArgumentNotNull((object)args, "args");
            Item obj = Client.ContentDatabase.GetItem(args.Parameters["id"]);
            if (obj != null)
            {
                if (WebEditCommand.IsStartItem(obj))
                {
                    SheerResponse.Alert("The operation cannot be applied to the site start item.");
                }
                else
                {
                    if (!SheerResponse.CheckModified())
                        return;
                  
                    Start("uiDeleteItems", args, obj.Database, new Item[1] { obj }, new NameValueCollection())["message"] = "item:deleted(id=" + (object)obj.ID + ")";
                }
            }
            else
                SheerResponse.Alert("The item could not be found.\n\nYou may not have read access or it may have been deleted by another user.");
        }

        private static NameValueCollection Start(string pipelineName, ClientPipelineArgs args, Database database, Item[] items, NameValueCollection additionalParameters)
        {
            Assert.ArgumentNotNull((object)pipelineName, "pipelineName");
            Assert.ArgumentNotNull((object)args, "args");
            Assert.ArgumentNotNull((object)database, "database");
            Assert.ArgumentNotNull((object)items, "items");
            Assert.ArgumentNotNullOrEmpty(pipelineName, "pipelineName");
            NameValueCollection nameValueCollection = new NameValueCollection();
            string str1 = string.Join("|", Enumerable.Select((IEnumerable)items, (Func)(item => item.ID)));
            string str2 = items[0].Language.ToString();
            nameValueCollection.Add("database", database.Name);
            nameValueCollection.Add("items", str1);
            nameValueCollection.Add("language", str2);
            nameValueCollection.Add("ispageediting","1");

            args.Parameters = nameValueCollection;
            if (additionalParameters != null)
            {
                foreach (string index in additionalParameters.AllKeys)
                    args.Parameters[index] = additionalParameters[index];
            }
            Context.ClientPage.Start(pipelineName, args);
            return nameValueCollection;
        }
    }




public class CustomDeleteItems : ItemOperation
    {

        /// 
        /// Executes Pipeline Postaction.
        /// 
        /// 
        /// The arguments.
        ///                     public void Reload(ClientPipelineArgs args)
        {
            //Sitecore.Client.ContentDatabase..
            if (args.Parameters["ispageediting"] == "1")
            {
                SheerResponse.Eval("window.parent.location.reload();");
            }
        }
    }

It works perfectly!

6 comments:

  1. What version of Sitecore are you running? I'm having the same issue on Sitecore 8 Update 2. Your solution did not work on our Sitecore instance. However i did notice that from the server i get a response of "/sitecore/shell/Applications/WebEdit/WebEditRibbon.aspx" with the contents:

    {"command":"Confirm","value":"An item was deleted. Do you want to refresh the page?","url":"/sitecore/shell/default.aspx?xmlcontrol=Confirm"}

    So apparently it's a Sitecore bug as there is some provisioning for reloading the page.

    ReplyDelete
    Replies
    1. I am running Sitecore 7.2 and it works. I think “SheerResponse” might not working properly due to the Sitecore 8. Please have a look at if “SPEAK UI” can help.

      Delete
  2. Yes, with Sitecore 7.2 everything works. I tried it and it is ok. I'm using some of your hints for my blog about data room due diligence . Waiting for your new articles! thanks!

    ReplyDelete
  3. I suggest that you utilize precisely the same wording each place in your User Document where you are alluding to a similar thing. video editing

    ReplyDelete
  4. Appreciating the persistence you put into your blog and detailed information you provide. I’ve bookmarked your site and I’m adding your RSS feeds to my Google account. dokument

    ReplyDelete
  5. I had a birthday yesterday and I tried my luck superior online slot machines and with the choice of the site I am not mistaken

    ReplyDelete

WFFM Web form for marketer fail to submit in Sitecore

I have setup a standard form using WFFM in Sitecore 8.2. It always fail when submit. Error as below, Error: Exception: System.Data.DataE...