Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Hi Fuji,
I am not sure but it might be related to HttpContext so can you please try this-
https://world.episerver.com/blogs/Magnus-Rahl/Dates/2010/12/Run-a-scheduled-job-as-a-specific-role/
Scheduled job runs as your current user if you trigger it manually and as an anonymous user if trigger it automatically. You can set the role it uses when running automatically using the link Ravindra sent above. Very common problem.
If you have trouble falling asleep - you can read this post (https://blog.tech-fellow.net/2020/12/07/episerver-scheduled-jobs-under-the-hood/) and see what's under the hood and why it has different context when ran manually vs scheduled.
Hello all! I have this scheduled job:
[ScheduledPlugIn(DisplayName = "Archive Expired Descendants", Description = "")] public class ArchiveExiredDescendants : ScheduledJobBase { private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly IContentRepository ContentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); private bool _stopSignaled; public ArchiveExiredDescendants() { IsStoppable = true; } public override void Stop() { _stopSignaled = true; } public override string Execute() { try { ParentPage parentPage = ContentRepository.GetChildren<ParentPage>(Global.StartPage.ContentLink).FirstOrDefault(); if(parentPage == null) { return "Can't Find Parent Page."; } List<ChildPage> childPages = parentPage.GetDescendants<ChildPage>().ToList(); if(!childPages.Any()) { return "No Child Pages Were Found."; } int i = 0; int ii = 0; foreach(ChildPage childPage in childPages) { try { if(childPage == null) continue; ii++; if(childPage.CheckPublishedStatus(PagePublishedStatus.Published)) continue; ContentRepository.Move(childPage.ContentLink, parentPage.ArchiveFolder); i++; } catch(Exception ex) { Logger.Error(ex.Message); } } return _stopSignaled ? "Stop of Job Was Called." : $"Job Completed. {i} ({ii}) Child Pages Were Archived."; } catch(Exception ex) { Logger.Error(ex.Message); return $"Job Failed. {ex.Message}"; } } }
GetDescendants method:
public static class PageDataExtentions { public static IEnumerable<T> GetDescendants<T>(this PageData page) { var repository = ServiceLocator.Current.GetInstance<IContentTypeRepository>(); var contentType = repository.Load(typeof(T)); int? pageTypeID = contentType.ID; if (pageTypeID.HasValue) { PropertyCriteria pageTypeCriteria = new PropertyCriteria(); pageTypeCriteria.Condition = CompareCondition.Equal; pageTypeCriteria.Name = "PageTypeID"; pageTypeCriteria.Type = PropertyDataType.PageType; pageTypeCriteria.Value = pageTypeID.Value.ToString(); PropertyCriteriaCollection criterias = new PropertyCriteriaCollection(); criterias.Add(pageTypeCriteria); var pageCriteriaQueryService = ServiceLocator.Current .GetInstance<IPageCriteriaQueryService>(); var descendants = pageCriteriaQueryService .FindPagesWithCriteria(page.ContentLink.ToPageReference(), criterias) .OrderByDescending(i => i.StartPublish ?? i.Created) .Cast<T>(); return descendants; } return null; } }
Basically the purpose of the job is to find ParentPage. It has ArchiveFolder property. And to move every expired ChildPage of ParentPage into ArchiveFolder.
Job triggers by schedule successfully, but the issue is when it's triggered automatically, it ignores expired child pages, but when triggered manually - everything works as supposed to.
Any ideas why? Thank you very much!