Copied to clipboard

Flag this post as spam?

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


  • Jits 3 posts 73 karma points
    Aug 10, 2017 @ 12:51
    Jits
    0

    Eventhandler webapi request

    Hello all, I'm trying to use a ContentService.Published hook to trigger some events. Which sends a request to DependencyResolver.Current.GetService<IRequestHandler>() .SendRequest(new Uri("Umbraco/Api/Agenda/UpdateBirthdays", UriKind.Relative));

    This works for my other webrequest which does not change/save or publish any content.

    But when i Use this class to handle what i want to do on the call:

        public static void UpdateBirthdays()
        {
            DeleteBirthdays();
            CreateBirthdays();
        }
    
        public static void ArchiveAgendaItems()
        {
            // Implement automatic archiving here.
        }
    
        private static void CreateBirthdays()
        {
            var helper = new UmbracoHelper(UmbracoContext.Current);
            var content = helper.TypedContentAtRoot().ToList();
    
            var employees = content.DescendantsOrSelf("employee").Where("Visible").ToList();
            var createdBirthdays = new List<IContent>();
            var agendaId = helper.TypedContentSingleAtXPath("//home/trending/agenda").Id;
            foreach (var employee in employees)
            {
                var birthday = employee.GetPropertyValueExists<DateTime>("EmployeeBirthdate");
                var firstName = employee.GetPropertyValueExists<string>("EmployeeFirstName");
                var lastName = employee.GetPropertyValueExists<string>("EmployeeLastName");
                var title = $"Verjaardag van {firstName} {lastName}";
    
                var birthdayItemDate = birthday.Month < DateTime.Now.Month && birthday.Day < DateTime.Now.Day ? new DateTime(DateTime.Now.Year + 1, birthday.Month, birthday.Day) : new DateTime(DateTime.Now.Year, birthday.Month, birthday.Day);
    
                var birthdayItem = ContentService.CreateContentWithIdentity(title, agendaId, "birthdayItem");
                birthdayItem.SetValue("itemTitle", title);
                birthdayItem.SetValue("itemDate", birthdayItemDate);
                birthdayItem.SetValue("itemDescription", title);
                ContentService.Save(birthdayItem);
                createdBirthdays.Add(birthdayItem);
            }
    
            foreach (var createdBirthday in createdBirthdays)
            {
                ContentService.PublishWithStatus(createdBirthday);
            }
        }
    
        private static void DeleteBirthdays()
        {
            var contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
            var birthdayContentType = contentTypeService.GetContentType("birthdayItem");
            var itemsToDelete = ContentService.GetContentOfContentType(birthdayContentType.Id).ToList();
    
            foreach (var item in itemsToDelete)
            {
                ContentService.Delete(item);
            }
        }
    }
    

    It works on some occasions but it tends to timeout on the On all the ContentService calls.

    Any Help?

  • Jits 3 posts 73 karma points
    Aug 10, 2017 @ 14:08
    Jits
    0

    I already tried calling this directly in the code as a temporary check. My class seems to work just fine, but not when it calls contentservice in the request.

    Something seems to prevent the contentservice from doing its work.

  • Marcio Goularte 374 posts 1346 karma points
    Aug 10, 2017 @ 14:16
  • Jits 3 posts 73 karma points
    Aug 10, 2017 @ 14:21
    Jits
    0

    Thank you for your response, I don't exactly understand how i can solve my problem with this though. Do you mean make everything async?

Please Sign in or register to post replies

Write your reply to:

Draft