Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tommy Albinsson 121 posts 254 karma points
    Oct 23, 2012 @ 09:26
    Tommy Albinsson
    0

    Creating your own "save and publish" ?

    Hi,

    I have been searching for an example on how to create your own save and publish button. Say for example, i have got a document type that needs to handle alot of different states. This can be quite tricky to do with managing the different states of the page life cycle. So say I wan't to create a "Save" button that resides in my usercontrol and when the button is clicked I save away my custom data in the value property of the IUsercontrolDataEditor interface and the save and publish is triggered.

    I have looked at the umbraco sourcecode and it seems a bit trivial, so if someone has done this today I would love an example :)

    I have almost got it to work, but as of now I have to press the save button som "serialize" my custom data and set the "value" property, then press save and publish.

    Cheers!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Oct 23, 2012 @ 18:19
    Jeroen Breuer
    0

    Here are some examples on how you can do it in a custom section or dashboard:

    http://our.umbraco.org/wiki/reference/umbraco-best-practices/standard-ui-controls/umbracouicontrols-page-samples

    http://our.umbraco.org/forum/developers/extending-umbraco/35410-Dashboard-tab-buttons

    Not sure if that will also work with a UserControl which is a datatype.

    Jeroen

  • Tommy Albinsson 121 posts 254 karma points
    Oct 26, 2012 @ 13:32
    Tommy Albinsson
    0

    Thanks for the answer.

    I can't really say that helped me alot. All i wan't to do is trigger the Save And Publish event, but is seems to be much harder than i thought from the beginning :P

    Looking through the umbraco code, I am trying to use this bit of code. It is used to Save and Publish in umbraco.

                var liveEditingContext = UmbracoContext.Current.LiveEditingContext;

                liveEditingContext.Updates.SaveAll();

                liveEditingContext.Updates.PublishAll();

    No exception is thorown, but nothing is saved.

  • Tommy Albinsson 121 posts 254 karma points
    Oct 29, 2012 @ 13:36
    Tommy Albinsson
    0

    Eventually I got it to work. Combining the Save and The Publish method from the editContent.aspx soruce did the trick. I will supply the courcecode below if someone else is interested in implementing their own "Save and Publish". I choose to work with a json viewmodel for my usercontrol. This seems to work very well for me, the content is saved, the document is refreshed (with the cache) and all is good.

    private void SaveAndPublish()
    {
        var pageId = Request.QueryString["id"];
    
        if (!string.IsNullOrEmpty(pageId))
        {
            var umbracoDocument = new Document(true, Convert.ToInt32(pageId));
    
            if (umbracoDocument.GenericProperties != null)
            {
                            // Set the value of the document that is stored in your value property 
                            // from the IUsercontrolDataEditor interface
                            // I have choosen to create a custom object that I serializes to a JSON 
                            //string when saving and to hold my datamodel
                umbracoDocument.GenericProperties.FirstOrDefault().Value = value;
    
                Action.RunActionHandlers(umbracoDocument, ActionUpdate.Instance);
                umbracoDocument.Save();
                umbracoDocument.UpdateDate = DateTime.Now;
    
                BasePage.Current.ClientTools.SyncTree(umbracoDocument.Path, true);
    
                if (umbracoDocument.Level == 1 || new Document(umbracoDocument.Parent.Id).Published)
                {
                    if (umbracoDocument.PublishWithResult(BasePage.Current.getUser()))
                    {
                        BasePage.Current.ClientTools.ShowSpeechBubble(
                                                                            BasePage.speechBubbleIcon.save,
                                        ui.Text("speechBubbles",
                                        "editContentPublishedHeader", null),
                                        ui.Text("speechBubbles",
                                        "editContentPublishedText", null));
                        library.UpdateDocumentCache(umbracoDocument.Id);
    
                        _documentHasPublishedVersion = umbracoDocument.HasPublishedVersion();
                    }
                    else
                    {
                        BasePage.Current.ClientTools.ShowSpeechBubble(
                                                                            BasePage.speechBubbleIcon.warning,
                                        ui.Text("publish"),
                                        ui.Text("contentPublishedFailedByEvent"));
                    }
                }
                else
                    BasePage.Current.ClientTools.ShowSpeechBubble(
                                                                    BasePage.speechBubbleIcon.warning,
                                    ui.Text("publish"),
                                    ui.Text("speechBubbles",
                                    "editContentPublishedFailedByParent"));
    
                            //Reloads the node and its data
                BasePage.Current.ClientTools.ChangeContentFrameUrl("editContent.aspx?id=" + pageId);
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft