Copied to clipboard

Flag this post as spam?

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


  • trfletch 598 posts 604 karma points
    Jun 19, 2012 @ 19:51
    trfletch
    0

    AfterUnPublish event handler, updating a cache not working

    Hi All,

    I have an Umbraco version 4.7.1.1 website that contains a large number of articles, I have therefore created a cache of all articles and a seperate cache for each article category. I already have similar AfterPublish events that are setup to update the cache when a user publishes a node but I am trying to setup something similar for when a user deletes or unpublishes the node. 

    I have created the following code to do this which strangely enough words if you delete a node, the cache gets updated successfully, I am assuming that when you do a delete it calls the unpublish code event handler first. 

    The issue is that the code doesn't actually work for when someone just unpublishes a node, also to see the node has been unpublished you need to refresh the node above and click back onto node to see that the link to document has been removed.

    I have already tried adding:

                    // Update article cache
                    umbraco.library.UpdateDocumentCache(documentObject.Id);

    But not only does this not resolve the unpublish event issue it stops it working when trying to delete a node (I assume because it does have an id after being deleted or something?

    Can anyone shed any light on this issue and what I can do to try and resolve it? Below is what I have in my event handler so far:

            void Document_AfterUnPublish(Document sender, UnPublishEventArgs e)
            {
                Document documentObject = sender;
                string documentTypeAlias = documentObject.ContentType.Alias;
    
                // Check DocType is Article
                if (documentTypeAlias == "Article")
                {
                    // Get categoryid for this node
                    int categoryId = Convert.ToInt32(documentObject.getProperty("category").Value);
    
    
                    // ***** START UPDATE CACHES  ******//
    
                    // SETUP CATEGORY ARTICLES CACHE 
                    da.UpdateCategoryArticlesCache(categoryId);
    
                    // SETUP ALL ARTICLES CACHE 
                    da.UpdateAllArticlesCache();
    
                    // _____ END UPDATE CACHES  ________//
                }
            }
  • Jesper Hauge 298 posts 487 karma points c-trib
    Jun 20, 2012 @ 00:11
    Jesper Hauge
    0

    Hi trfletch,

    I'm not entirely sure why you would want to call UpdateDocumentCache, in the AfterUnpublish event. Updating the document cache (which is the umbraco.config file and it's in memory cache copy) should be taken care of by the Umbraco framework itself. So unless your articles caches, is somehow related to the document cache, you shouldn't need to update the document cache.

    Also I'm not entirely sure, what the exact problem is: Does the code in your AfterUnPublish event handler run or not, or is it an error happening in your da.UpdateX methods?

    I'm not absolutely sure, but I've looked a bit in the code of the library.UpdateDocumentCache, and as far as I can tell, the code actually doesn't update the xml in the document cache, when the document you're sending to the method is not published. I guess the updating of the DocumentCache, has taken place before AfterUnPublish is called when unpublishing a document. I don't know if this has any bearing on your issue.

    Not much direct help I'm afraid, but I would be interested to hear a bit more about your problem.

    Regards
    Jesper Hauge 

     

  • trfletch 598 posts 604 karma points
    Jun 20, 2012 @ 10:57
    trfletch
    0

    Hi Jesper,

    Thank you for the response. Sorry if I did not explain myself very well. The code to rebuild the cache of nodes is running but the article that is has just been unpublished is still part of that cache and is visible in lists on the front end (although you cannot actually open it because its been unpublished). It is almost as if by the time the code to rebuild the cache is run the node hasn't yet been unpublished of removed from the umbraco.config file even though this is the after unpublish event. Hence why I tried to run UpdateDocumentCache but as well as this not resolving the issue its stopped it working when doing a delete.

    This is what is strange, the fact that the code above works when deleting the a node is strange. Unless what happens is the node is deleted first which removes it from the umbraco.config file then it is unpublished which fires my code to update the cache? If that makes sense?

    Let me know if you need me to explain further.

    Regards

    Tony

  • Jesper Hauge 298 posts 487 karma points c-trib
    Jun 20, 2012 @ 11:56
    Jesper Hauge
    0

    Hi again Tony,

    In that case I think I know what the problem is, and how to work around it.

    In Umbraco the actual updating of the umbraco.config file cannot be relied upon in real-time. I think the actual updating of the on-disk file is run in an asynchronous job in another thread. 

    If you need real-time access to the xml, you should access the in-memory cache instead of the umbraco.config. file. You can get to the in-memory cache xml content using code like this:

    using System.Xml;
    using System.Xml.Linq;
    using umbraco;

    // Get the published xml content from in memory cache as an XmlDocument
    XmlDocument xmlDoc = content.Instance.XmlContent;

    // If you'd rather work with an XDocument instead
    XDocument xDoc = XDocument.Load(new XmlNodeReader(content.Instance.XmlContent)); 

    If you're not already using the document instance in your code, I think you'd also experience som performance enhancements using this apporach.

    Regards
    Jesper Hauge 

  • trfletch 598 posts 604 karma points
    Jun 20, 2012 @ 13:20
    trfletch
    0

    Hi Jesper,

    Thanks again for the response. I am already using the following code to access the XML which I believe is similar to what you have suggested:

            public static List GetNodesFromXpath(string xPath)
            {
                List nodes = new List();

                XPathNavigator xPathNavigator = umbraco.content.Instance.XmlContent.CreateNavigator(); //get all umbraco xml
                XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPath); //TODO: check xpath string is valid

                int id;
                Node node;

                while (xPathNodeIterator.MoveNext())
              {
                    if (int.TryParse(xPathNodeIterator.Current.Evaluate("string(@id)").ToString(), out id)) //check id is a numberic                {
                        node = new Node(id);
                        if (node != null) { nodes.Add(node); }
                    }
                }
                return nodes;
            }

     

    I have actually got around this issue by passing the node id I am unpublishing through to my update cache method and removing it from the list that way like so:

                    // SETUP ALL ARTICLES CACHE 
                    da.UpdateAllArticlesCache(documentObject.Id);
    public object UpdateAllArticlesCache(int removeNodeId)
    {
    ********** get nodes into list code **************

    // Remove unpublished item from cache
    cachedItem.CachedObject = articlesByModified.Where(c => c.NodeId != removeNodeId).ToList();

    ********** rest of code for creating cached object ***********


    Another related issue that I do have which you or someone else may be able to help with. Since implementing this AfterPublish event handler, when unpublishing a node the unpublish button disappears but the "Link to document" does not and tree is not updated to show the node greyed out. If I refresh the parent node then the node is shown as greyed out and if I click on the unpublished node to refresh it then the "Link to document" has been removed. Any ideas how to solve this?

    Regards
    Tony 

     

Please Sign in or register to post replies

Write your reply to:

Draft