简体   繁体   中英

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 ie 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.

You don't have to do this, Umbraco will automatically append (1) to the name if the item already exists (so it IS unique).

If you don't want this behavior you can check in the following way:

protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
    ContentService.Publishing += ContentService_Publishing;
}

private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
    var contentService = UmbracoContext.Current.Application.Services.ContentService;

    // It's posible to batch publish items, so go through all items 
    // even though there might only be one in the list of PublishedEntities
    foreach (var item in e.PublishedEntities)
    {
        var currentPage = contentService.GetById(item.Id);

        // Go to the current page's parent and loop through all of it's children
        // That way you can determine if any page that is on the same level as the 
        // page you're trying to publish has the same name
        foreach (var contentItem in currentPage.Parent().Children())
        {
            if (string.Equals(contentItem.Name.Trim(), currentPage.Name.Trim(), StringComparison.InvariantCultureIgnoreCase))
                e.Cancel = true;
        }
    }
}

I think your problem might be that you're not looping through all PublishedEntities but using some other way to determine the current page Id.

Note: Please please please do not use the library.UpdateDocumentCache this, there's absolutely no need, ContentService.UnPublish will take care of the cache state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM