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!

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...