Copied to clipboard

Flag this post as spam?

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


  • Ian Robinson 79 posts 143 karma points
    Nov 23, 2011 @ 01:05
    Ian Robinson
    0

    Stopping whole content tree from refreshing when document is moved

    Hi,

    When the user publishes a particular document type in the Umbraco back office, I move this document to a particular content node depending on the contents of a custom category property defined in the document. 

    This is very similar to the Classifieds example in the Umrbaco User Guide (written by Nik Wahlberg and Paul Sterling).

    The .Net class code runs in the Document.AfterPublish event, and is simply Document.Move(newParentId) code, where newParentId is the id of the document I want to place the published document under.  The code refreshes the ENTIRE content tree but I only want it to refresh the document node where the published document has been moved to (i.e. newParentId).

    Is there a way to do this?  If not, can I stop the content tree from refreshing at all?

    If I comment this line of code out, the content tree is not refreshed, so I'm assuming it must be this command that's refreshing the tree, and not the publish action itself.

    If I move the document myself from the back office UI, by right clicking on the document and choosing "Move", the document is moved and only the parent node is reloaded, not the entire content tree.  This is the functionality I want to replicate when moving the document using .Net code.

    Has anyone come across the same problem when moving documents using code?

  • Ian Robinson 79 posts 143 karma points
    Nov 23, 2011 @ 11:06
    Ian Robinson
    0

    I forgot to mention that I'm using Umbraco v 4.7.1 (Assembly version: 1.0.4281.20201).

    I haven't included any call to ClientTools.SyncTree, but Umbraco is refreshing the content tree itself when I call  "sender.Move(doc.Id);"

    I'm at a bit of a loss as to what to do next.  So any help would be appreciated.

    private void DocumentAfterPublish(Document sender, PublishEventArgs e) {

        if (sender.ContentType.Alias == "ClassifiedAd")  {

            try
            {

                var classifieds = new Document(ClassifiedSettings.ParentNodeId);

                // find the child document type (category) that matches the ad category
                Document[] children = classifieds.Children;

                var categoryStr = ClassifiedSettings.Categories.Where(x => x.Value.Equals(sender.getProperty("adCategory").Value.ToString())).First().Text;

                var foundCategoryFolder = false;

                foreach (Document doc in children)
        {

                    if ((doc.ContentType.Alias == "ClassifiedCategory") && (doc.Text.Equals(categoryStr, StringComparison.CurrentCultureIgnoreCase)))
            {

                        foundCategoryFolder = true; 

                        sender.Move(doc.Id);

                        break;

                    }

                }

                if (!foundCategoryFolder)  {

                    throw (new Exception("No category folder was found that matches this item's category.  Does the category folder exist?"));

                }

            }
    catch (Exception ex)
    {

                umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, -1, "Error moving classifieds ad.  " + ex.Message.ToString());

                throw (ex);

            }

        }

    }

     

     

  • Ian Robinson 79 posts 143 karma points
    Nov 23, 2011 @ 20:07
    Ian Robinson
    0

    I've managed to find a solution.  So if anyone else has the same issue, here's the answer.

    I downloaded and looked in the source code of Umbraco and found where I think the move action is called, to see what that action code was doing differently to my own code.  There's a file called moveOrCopy.aspx.cs that contains the command "ClientTools.MoveNode(currentNode.Id.ToString(), newNode.Path);".

    I then searched the forum for "MoveNode" and found this helpful post - http://our.umbraco.org/forum/developers/extending-umbraco/18607-How-to-focus-a-moved-Document-from-event-handler

    I tried using this command instead of "sender.Move(doc.Id)" and while it did just refresh the node I required and not the whole content tree, it didn't actually move the node to the folder in the content tree I needed it to move the node to.

    Using both commands has given me the desired effect though.  The published node is now moved to the sub folder I need it to be moved to, and then although the whole content tree is refreshed, I then call ClientTools.MoveNode, so the area of the content tree where the node has been moved to, is re-opened.  This happens so quickly, it's not too noticeable and is a good compromise.

    So in the code above, I've just replaced:

      sender.Move(doc.Id);

    With: 

      sender.Move(doc.Id);
      BasePage.Current.ClientTools.MoveNode(sender.Id.ToString(), doc.Path);

    If anyone knows of a better, more efficient or elegant solution, please let me know.

Please Sign in or register to post replies

Write your reply to:

Draft