Copied to clipboard

Flag this post as spam?

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


  • asaaheim 9 posts 39 karma points
    Jul 04, 2013 @ 23:56
    asaaheim
    0

    Menu item on node creation and relations/states

    Hi all!

    I have a snippet of code that I use to (try to) hook into events in Umbraco BackOffice.

    So far I have successfully been able to let the users create menu items while creating content, and also establised/saved the relation between the newly created menu item and the content created at the same time. The new menu item is placed where the user chooses by picking in the menu tree and gets the name/title/.Text of the content node.

    I have some additional functionality that I hope to be able to achieve though:

    1. Avoid duplicate entries of menu items on new saves/publishes of an already existing node.
    2. Give the menu item published/unpublished state equal to that of the content node.
    3. If node title is updated, how can I update the corresponding menu item, rather than have the code create an entirely new menu item?
    4. If, from the menu item, the relation is changed (ie the content node is removed from the content picker), how can I make sure to update the actual relation and also remove the choice that has previously been made within the picker (menu tree) on the content node?I have started looking into hooking into the BeforeSave event for this, but this is not reflected in the below code snippet.

    Here is the code I currently have:

    namespace Umbraco.Extensions.EventHandlers
    {
        public class SaveContentHook : IApplicationEventHandler
        {
            public void OnApplicationInitialized(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext)
            {
            }

            public void OnApplicationStarting(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext)
            {
            }

            public void OnApplicationStarted(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext)
            {
                Document.AfterSave += Document_AfterSave;
            }

            void Document_AfterSave(Document sender, EventArgs e)
            {
              
                umbraco.cms.businesslogic.property.Property parentMenuProperty = sender.getProperty(Convert.ToString("PickParentMenuInMenuTreePicker"));
                if (parentMenuProperty == null)
                {
                    return;
                }
                string pmpString = parentMenuProperty.Value.ToString();

                if (pmpString != null && pmpString != "")
                {
                    int parentMenuItemId = int.Parse(pmpString);
                    if (parentMenuItemId > 0)
                    {
                        var cs = ApplicationContext.Current.Services.ContentService;

                        IContent strParent = cs.GetById(int.Parse(pmpString));

                        string strTittel = sender.Text;
                        int strUserID = sender.Writer.Id;
                        var menuItem = cs.CreateContent(strTittel, strParent, "MenuItem", strUserID);       
                       
                        cs.SaveAndPublish(menuItem);

                        // Menu item relationship
                        Document doc = new Document(menuItem.Id);
                        doc.getProperty("velgInnhold").Value = sender.Id;
                        User author = User.GetUser(sender.Writer.Id);
                        doc.Publish(author);
                        umbraco.BasePages.BasePage.Current.ClientTools.SyncTree(doc.Path, true);
                        umbraco.library.UpdateDocumentCache(doc.Id);

                        //Existing relations
                        umbraco.cms.businesslogic.relation.RelationType relationType = umbraco.cms.businesslogic.relation.RelationType.GetByAlias("RelationshipAlias");
                        umbraco.cms.businesslogic.relation.Relation[] relations = umbraco.cms.businesslogic.relation.Relation.GetRelations(sender.Id, relationType);
                        if (relations != null)
                        {
                            foreach (umbraco.cms.businesslogic.relation.Relation relation in relations)
                            {
                                int mmiId = relation.Child.Id;
                                if (mmiId == parentMenuItemId)
                                {
                                    relation.Delete();
                                }
                                else
                                {
                      
                                    relation.Delete();
                                }
                                cs.Delete(cs.GetById());
                            }
                        }
                        umbraco.cms.businesslogic.relation.Relation.MakeNew(sender.Id, menuItem.Id, relationType, "[\"PropertyTypeId\":RELTYPEID]");
                    }
                }
            }
        }
    }

    Please excuse any errors, this is my first endavour hooking into Umbraco like this.

    Any hints or tips to get me in the right direction concerning any of my pointers above, are received gratefully.

    Thanks in advance,
    Anna

  • asaaheim 9 posts 39 karma points
    Jul 05, 2013 @ 00:02
    asaaheim
    0

    I completely forgot, but version info here: Umbraco v6.0.2

    //Cheers

Please Sign in or register to post replies

Write your reply to:

Draft