Copied to clipboard

Flag this post as spam?

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


  • keilo 568 posts 1023 karma points
    Nov 24, 2014 @ 18:02
    keilo
    0

    How to change DocumentType programmatically

    I believe this is the right place to ask this;

    --

    I have a documentType A which I have copied to B (exact same properties),

    where I would like to change about 500+ nodes of DocumentType A to B, programmatically, as using the GUI is not practical.

    Not sure if Im using the right approach, trying the following saves the nodes (i.e. code executes) but does not change their document type;

    Anyone can shed some light?

    const int blogNodeId = 1289; // ROOT node where doctypes A are children.

     //Get the ServiceContext and the two services we are going to use

    var contentService =     Services.ContentService;

    var contentTypeService = Services.ContentTypeService;           

    var contentType = contentTypeService.GetContentType(1273); // doctype B's ID

    var blogNode = contentService.GetById(blogNodeId);

                foreach (var post in blogNode.Descendants())

                {

                    if (post.ContentType.Alias == "blogArticle")

                    {

    var content = contentService.GetById(post.Id);

    content.ChangeContentType(contentType); // this doesnt seem to do anything

    contentService.Save(post); // all parsed nodes are marked as saved

    // all works except .ChangeContentType

                    }

                }

  • jivan thapa 194 posts 681 karma points
    Nov 24, 2014 @ 19:23
    jivan thapa
    101

    Hi, contentService.Save(post) should be => contentService.Save(content)`. You are modifying the variable 'content' but you are saving the variable 'post' at the end.

  • keilo 568 posts 1023 karma points
    Nov 24, 2014 @ 19:33
    keilo
    0

    Doh.. didnt notice that past midnight..

    Many thanks for the heads-up. You have been very helpfull in numerous occasions, much appreciated!

     

    cheers

  • keilo 568 posts 1023 karma points
    Nov 25, 2014 @ 09:03
    keilo
    0

    Hi Jivan

    On the very same topic, after I change the document type via the code all seems OK on the backend office UI - i.e. I see the document type has been updated and icon reflects the corresponding to changed document type.

    I have changed one node document type manually via the backend office UI, then run the code for the remaining 500.

    When I try to iterate all the nodes from its main node with CurrentPage.Descendants("blogArticle")

    there is only one item returned - the one I manually changed. No clue why the others are not returning as the document type seems to be changed. Published all.

    If I change to code to CurrentPage.Descendants() then I get all of them.

    Is there a step that I am missing? Would you have any ideas?

  • denisedelbando 141 posts 339 karma points
    Jul 05, 2017 @ 15:37
    denisedelbando
    0

    have you solved this issue? republishing doesnt seem to work

  • jivan thapa 194 posts 681 karma points
    Nov 25, 2014 @ 10:15
    jivan thapa
    0

    Have you try Content->right click -> Republish entire site?

  • denisedelbando 141 posts 339 karma points
    Jul 05, 2017 @ 15:29
    denisedelbando
    0

    this doesnt work :(

  • Thomas 49 posts 78 karma points c-trib
    Nov 08, 2017 @ 05:56
    Thomas
    0

    I would try checking the /app_data/Umbraco.config and seeing if the XML cache for your modified nodes has been updated correctly - that is, the modified nodes have the correct tags (the name of the new doctype) around them. If not - you need to rebuild the Umbraco XML cache (which is, or at least was, a very common Umbraco troubleshooting step). This can time out and/or slow down your site - so be careful on production sites.

    /umbraco/dialogs/republish.aspx?xml=true

Please Sign in or register to post replies

Write your reply to:

Draft