Copied to clipboard

Flag this post as spam?

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


  • Nick 19 posts 39 karma points
    Jul 20, 2010 @ 02:51
    Nick
    0

    How to change Document conteny Type using Event handler

    I am trying to change content type of document using Event handler. I have tried before and after publish method but dosent seem to work..

     

     ipublic ContentEvent()
            {
          //      Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
                Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
            }

          

            void Document_BeforePublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
            {
                if (sender.ContentType.MasterContentType == 1085)
                {
                    User myUser = User.GetUser(0);
                    //string status = sender.getProperty("status").Value.ToString();
                    umbraco.cms.businesslogic.ContentType ct = new umbraco.cms.businesslogic.ContentType(1107);
                    sender.ContentType = ct;
                    sender.ContentType.Save();
                    sender.Save();
                    umbraco.library.UpdateDocumentCache(sender.Id);
                    // sender.Publish(myUser);

                }

            }

    Thats the code Ihave used..I tried that code in afterPublish as well but didnt work..Can anyone help me with that?

     

     

     

     

  • Nick 19 posts 39 karma points
    Jul 20, 2010 @ 03:42
    Nick
    0

    I tired this code as well

     

    public class ContentEvent : ApplicationBase
        {
            public int published { get; set; }
            public ContentEvent()
            {
                Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
            //    Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
            }

         

            void Document_BeforePublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
            {
                if (sender.ContentType.MasterContentType == 1085)
                {
                    User myUser = User.GetUser(0);
                    //string status = sender.getProperty("status").Value.ToString();
                    umbraco.cms.businesslogic.ContentType ct = new umbraco.cms.businesslogic.ContentType(1105);
                    sender.ContentType = ct;
                    sender.ContentType.Save();
                    sender.Save();
                    umbraco.library.UpdateDocumentCache(sender.Id);
                    //sender.Publish(myUser);

                }

            }

            void Document_AfterPublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
            {
                if (published != 1)
                {
                    if (sender.ContentType.MasterContentType == 1085)
                    {
                        User myUser = User.GetUser(4);
                        //string status = sender.getProperty("status").Value.ToString();
                        umbraco.cms.businesslogic.ContentType ct = new umbraco.cms.businesslogic.ContentType(1105);
                        sender.ContentType = ct;
                        //sender.ContentType.Save();
                        //sender.m
                        sender.Save();
                        published = 1;
                        //umbraco.library.UpdateDocumentCache(sender.Id);
                        sender.Publish(myUser);

                    }
                }

            }
        }
    }

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 20, 2010 @ 05:26
    Aaron Powell
    0

    You can't change the content type per-say, the only way to go about it is to create a new document, copy the content across and delete the old one.

    I'm pretty sure that's how this package works: http://our.umbraco.org/projects/developer-tools/doc-type-extensions

  • Nick 19 posts 39 karma points
    Jul 20, 2010 @ 06:40
    Nick
    0

    hmmm..cool...thanks

  • Nick 19 posts 39 karma points
    Jul 20, 2010 @ 07:20
    Nick
    0

    I found the solution..I wanted to change the Icon of the node base on coniditon and i thought only way is to change content type base on condition.

    but then realised that i can use BaseTree_BeforeNodeRender and change the Icon on the fly

Please Sign in or register to post replies

Write your reply to:

Draft