Copied to clipboard

Flag this post as spam?

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


  • Peter Cort Larsen 347 posts 798 karma points
    27 days ago
    Peter Cort Larsen
    0

    Hi all,

    I can figure out why this code dosnĀ“t work. Can you? The values called 'lat' and 'lng' are not updated on the node.

    Its a Umb version 7.14

    public class MyApplicationEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Published += ContentService_Publishing;
            base.ApplicationStarted(umbracoApplication, applicationContext);
        }
    
        void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            foreach (var content in e.PublishedEntities)
            {
                if (content.ContentType.Alias == "DOC TYPE")
                {
                    var streetName = content.Getvalue<string>("streetName");
                    var postalCode = content.Getvalue<string>("postalCode");
                    var cityName = content.Getvalue<string>("cityName");
                    var address = "";
    
                    address = streetName + ", " + postalCode + " " + cityName + ", Denmark";
    
                    if (content.GetValue<string>("lat") == string.Empty)
                    {
    
                        var contentService = new Umbraco.Core.Services.ContentService();
                        var venueDoc = contentService.GetById(content.Id);
                        venueDoc.SetValue("lat", "SOME LAT VALUE");
                        venueDoc.SetValue("lng", "SOME LNG VALUE");
                        contentService.SaveAndPublishWithStatus(venueDoc);
                    }
                }
            }
        }
    }
    
  • Frans de Jong 485 posts 1653 karma points c-trib
    27 days ago
    Frans de Jong
    0

    Shouldn't this if (content.GetValue<string>("lat") == string.Empty) be if (content.GetValue<string>("lat") != string.Empty)

  • Peter Cort Larsen 347 posts 798 karma points
    27 days ago
    Peter Cort Larsen
    0

    No, if it already has a value, nothing shall happen.

  • Frans de Jong 485 posts 1653 karma points c-trib
    27 days ago
    Frans de Jong
    0

    Than I think you are checking the wrong object.

    Content is what comes in. VenueDoc is what you are trying to save. your check should be if (venueDoc.GetValue<string>("lat") == string.Empty) than.

    Otherwise you are checking the lat you just submitted and that is never empty because you tried to save it.

  • Peter Cort Larsen 347 posts 798 karma points
    27 days ago
    Peter Cort Larsen
    0

    I just removed this part, to test, if something was being saved. No luck, nothing is saved.

    if (content.GetValue<string>("lat") == string.Empty)
    
Please Sign in or register to post replies

Write your reply to:

Draft