Copied to clipboard

Flag this post as spam?

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


  • Rick 92 posts 278 karma points
    Nov 26, 2014 @ 11:25
    Rick
    0

    Publish node within ContentService.Created event

    Hi,

    I am trying to publish a page programmatically during the ContentService.Created event using Umbraco 7.1.8.

    I have a NewsArticle page that upon SaveAndPublish drops the page into a doctype called "YearFolder" based on the "PostedDate" property on "NewsArticle" - I have this working which is great (thanks to the marvellous DateFolders package by @kipusoep).

    However, I've hooked into the ContentService.Created event so that the "YearFolder" doctype can be published just after it is created within the ContentService.Created event. 

    Here's my code:

            private void ContentService_Created(IContentService content, NewEventArgs<IContent> sender)
            {
                if (sender.Entity.ContentType.Alias == "YearFolder")
                {
                    sender.Entity.ChangePublishedState(PublishedState.Published);
                    content.PublishWithStatus(sender.Entity);
                }
            }

    I'm running a check to make sure that only doctypes of "YearFolder" (for now) will publish during the ContentService.Created event.

    The published state is successfully changed for the "YearFolder" doctype and it looks as though the page is published (the node is no longer greyed out). However, there is no link generated because I get the notification:

    " Oops: this document is published but is not in the cache (internal error) "

    ... in the Properties tab of the doctype.

    I know with Umbraco 4 events you used to have to do: 
    library.UpdateDocumentCache(sender.Entity.Id);

     

    ...which is obsolete now and doesn't work anyway.

    Has anyone else come across this?

    Many thanks,

    Rick

  • Sören Deger 733 posts 2844 karma points c-trib
    Nov 26, 2014 @ 11:57
    Sören Deger
    0

    Hi Rick,

    ContentService_Created event is also obsolete. Please read the last section of this docu:
    http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events

    You can use the hasIdentity-method in ContentService_Saving event to check this.

     

    Hope this helps.

     

    Cheers

    Sören 

  • Rick 92 posts 278 karma points
    Nov 26, 2014 @ 13:05
    Rick
    0

    Hi Soren,

    Thanks for your very quick response. I didn't realise the ContentService_Created event was obsolete :-(

    However, I have now refactored the code like so (very funny interface name by the way!) :

           private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> doc)
           {
               foreach (var item in doc.SavedEntities.Where(x => x.ContentType.Alias == "YearFolder"))
               {
                   var dirty = (IRememberBeingDirty)item;
                   var isNew = dirty.WasPropertyDirty("Id");

                   if (isNew)
                       item.ChangePublishedState(PublishedState.Published);
               }
           }

    When I step through the code, I can see that it correctly hits the "item.ChangePublishedState(PublishedState.Published);" line for the "YearFolder" doctype, however, still doesn't publish the node - it remains greyed out and I see the message "The item is not published".

    I feel I am missing something obvious here!

    Many thanks,
    Rick

  • Odd Veibust 5 posts 27 karma points
    Nov 26, 2014 @ 13:22
    Odd Veibust
    1

    Hi, have you tried sender.Publish(item) ? I have used it, and it works for us.

    Also you can use item.IsNewEntity() to check if it's a new entity

  • Rick 92 posts 278 karma points
    Nov 26, 2014 @ 13:36
    Rick
    0

    Hi Odd Veibust,

    Thanks for the comment - I've just tried the following code:

    private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> doc)
    {
               foreach (var item in doc.SavedEntities.Where(x => x.ContentType.Alias == "YearFolder"))
               {
                   if (item.IsNewEntity())
                       sender.Publish(item);
               }
    }

    Unfortunately, it's the same result; the "YearFolder" doctype is still greyed out and remains "Unpublished".

    If I add in "item.ChangePublishedState(PublishedState.Published);" just before "sender.Publish(item);", the "YearFolder" is no longer greyed out but I get the message " Oops: this document is published but is not in the cache (internal error)" on the "YearFolder" doctype as per my original post.

    Also, what is the difference between item.IsNewEntity() and the IRememberBeingDirty interface?

    Thanks,
    Rick

  • Sören Deger 733 posts 2844 karma points c-trib
    Nov 26, 2014 @ 13:42
    Sören Deger
    0

    Hi Rick,

    I don't know the exact difference of this both.

    But I would use the following code instead of sender.Publish(item):

    sender.SaveAndPublishWithStatus(item, false); 

    Can you say me if this works for you?

     

    Sören

  • Rick 92 posts 278 karma points
    Nov 26, 2014 @ 13:55
    Rick
    0

    Hi Soren,

    I'm using Umbraco 7.1.8 and I've now changed to:

            private void ContentService_Saved(IContentService sender, SaveEventArgsdoc)
           {
               foreach (var item in doc.SavedEntities.Where(x => x.ContentType.Alias == "YearFolder))
               {
                   if (item.IsNewEntity())
                   {
                       item.ChangePublishedState(PublishedState.Published);
                       sender.SaveAndPublishWithStatus(item, 0, false);
                   }
               }
           }

    I had to pass in "0" as the UserId for sender.SaveAndPublishWithStatus

    I still get the error message - even though the icon appears to not be greyed out anymore: Oops: this document is published but is not in the cache (internal error)"  as stated in my original post

     

    Thanks,
    Rick

  • Sören Deger 733 posts 2844 karma points c-trib
    Nov 26, 2014 @ 14:28
    Sören Deger
    100

    Hi Rick,

    have you any mandatory properties in this node and have you set a value to this properties before you save the node?

     

    Sören

  • Rick 92 posts 278 karma points
    Nov 26, 2014 @ 14:36
    Rick
    0

    Hi Soren,

    That's it! Thank you so much!

    I had a rich text editor that had a mandatory field - as soon as I removed it - it worked!

    Thank you to you and Odd Veibust for your comments/support!

    #h5yr

  • Eric Wilkinson 21 posts 91 karma points
    Apr 24, 2018 @ 17:47
    Eric Wilkinson
    0

    Had this same issue in 7.3.4 - don't think it's version dependent!

    Be careful, you can "force" it to be published with mandatory fields - which you don't want to do.

    If you run into this issue and find yourself trying to force it to publish (manually changing published status, etc) - stop and find out why Umbraco's SaveAndPublishWithResult function isn't working - then solve the root of the issue.

    We logged the returned "attempt" object from the publish call and it pointed us directly to a specific field which was a "required" true/false field (which is null - but on the front/end save/publish would change that to "false" which is why this only occurred from code in our case).

    Hope to help somebody else! :)

Please Sign in or register to post replies

Write your reply to:

Draft