Copied to clipboard

Flag this post as spam?

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


  • Dan Evans 629 posts 1016 karma points
    Sep 10, 2018 @ 16:01
    Dan Evans
    0

    Updating a property using ContentService.Published

    I want to update a document property after the page is published with a value from a 3rd party web service.

    When I use contentService.SaveAndPublishWithStatus i end up in an infinite loop.

    When I use contentService.Save(node, 0); it works fine but then the page is flagged as having unpublished changes and is greyed out to the user which is confusing for them.

    How can I set a property value, publish the page and not end up in an infinite loop?

    private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args, UmbracoHelper helper)
        {
            foreach (var node in args.PublishedEntities)
            {
                        if (node.ContentType.Alias == "mGProperty")
                        {
    
                            string retVal = AppLib.sendPropLinkUmbraco(node.Id, "publish");
                            node.SetValue("advertRef", retVal);
                            //contentService.SaveAndPublishWithStatus(node,0, false);
                            contentService.Save(node, 0);
                        }
    
            }
        }
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Sep 10, 2018 @ 19:57
    Alex Skrypnyk
    2

    Hi Dan

    Try to use Publishing event, have a look at content service events - https://our.umbraco.com/documentation/reference/events/contentservice-events

    Alex

  • Dan Evans 629 posts 1016 karma points
    Sep 12, 2018 @ 12:42
    Dan Evans
    0

    As I understand it both Publishing and Published both ignore the "raiseEvents" flag and both end up in an infinite loop so not a solution sadly.

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Sep 12, 2018 @ 14:39
    Frans de Jong
    1

    You don't need to save the content at least when you are using ContentSerice.Saving.

    for example:

     protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
     {
       ContentService.Saving += ContentService_Saving; // rasied before the content has been saved.
     }
    
     private void ContentService_Saving(IContentService contentService, SaveEventArgs<IContent> eventArgs)
     {
       foreach (IContent node in eventArgs.SavedEntities)
         {
           if (node.ContentType.Alias == Filtergroup.ModelTypeAlias)
           {
              CheckMainFilterGroup(node, eventArgs);
           }
         }
      }
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Sep 12, 2018 @ 14:53
    Alex Skrypnyk
    2

    Exactly, Frans, thank you so much.

  • Dan Evans 629 posts 1016 karma points
    Sep 12, 2018 @ 15:18
    Dan Evans
    0

    But i am updating a 3rd party web service. I need to do this when the document is published. I don't want to do it when a user may just be saving a draft of the document. My code needs to do 2 things. Update a 3rd party web service when the document is published, then update an Umbraco property with a returned value...

    //update 3rd party web service:
    string retVal = AppLib.sendPropLinkUmbraco(node.Id, "publish");
    
    //take returned value and update umbraco property
     node.SetValue("advertRef", retVal);
    
  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Sep 13, 2018 @ 06:53
    Frans de Jong
    100

    Ok, now I understand. The infinite loop is logical in that case. You ask tell it to publish when publishing tho the event triggers again and again.

    What happens if you use the publishing event? If you do a normal save there it will happen before the actual publishing. I expect that is the effect you are looking for?

  • Dan Evans 629 posts 1016 karma points
    Sep 13, 2018 @ 11:01
    Dan Evans
    0

    Yes, that's perfect! I thought I'd tried the Publishing event but clearly not! Thanks very much for your help.

Please Sign in or register to post replies

Write your reply to:

Draft