Copied to clipboard

Flag this post as spam?

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


  • Vinod 22 posts 83 karma points
    Jul 20, 2015 @ 05:58
    Vinod
    0

    Umbraco unpublish Event not working for the current node

    I am developing Umbraco 7 MVC application and my requirement is to add Item inside Umbraco. Item name should be unique. For that used the below code but I am getting the error "Oops: this document is published but is not in the cache (internal error)"

          protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication,
                ApplicationContext applicationContext)
            {
                ContentService.Publishing += ContentService_Publishing;
            }
    
    
            private void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
            {
                try
                {
                   if(newsItemExists)
                   {
                      e.Cancel = true;
                   }
                }
                catch (Exception ex)
                {
                    e.Cancel = true;
                    Logger.Error(ex.ToString());
                }
            }
    

    Then I tried adding code to unpublish but its not working i.e the node is getting published. Below is my code

    private void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
            {
                try
                {
                   int itemId=1234; //CurrentPublishedNodeId
                   if(newsItemExists)
                   {
                      IContent content = ContentService.GetById(itemId);
                      ContentService.UnPublish(content);
                      library.UpdateDocumentCache(item.Id);
                   }
                }
                catch (Exception ex)
                {
                    e.Cancel = true;
                    Logger.Error(ex.ToString());
                }
            }
    

    But with the above code, if you give the CurrentPublishedNodeId=2345 //someOthernodeId its unpublished correctly.

    Can you please help me on this issue.

Please Sign in or register to post replies

Write your reply to:

Draft