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
I created a scheduled job to reset the display options for all blocks within a specific page type within our site. The job identified a list of all pages of the required type and then iteratred through the blocks in the content area to amend their display option. Below is a copy of the job I created:
[ScheduledPlugIn(DisplayName = "ResetDisplayTypes")] public class ResetDisplayTypes : ScheduledJobBase { private bool _stopSignaled; public ResolveDisplayTypes() { IsStoppable = true; } public override void Stop() { _stopSignaled = true; } public override string Execute() { OnStatusChanged(String.Format("Starting execution of {0}", this.GetType())); List<ContentAreaItem> items = new List<ContentAreaItem>(); var pages2Check = GetAllPages(); var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); foreach (var page in pages2Check) { ArticlePage writablePage = (ArticlePage)art_page.CreateWritableClone(); ContentArea area = writablePage.MainContentArea; foreach (ContentAreaItem cai in area.Items) { var renderSettings = new Dictionary<string, object> { ["data-epi-content-display-option"] = "Half", ["data-contentgroup"] = "", ["tag"] = "Half" }; var replace = new ContentAreaItem { ContentLink = cai.ContentLink, RenderSettings = renderSettings }; replace.LoadDisplayOption(); items.Add(replace); } area.Items.Clear(); foreach (ContentAreaItem item in items) { area.Items.Add(item); } var saveAction = SaveAction.CheckIn | SaveAction.Publish | SaveAction.ForceNewVersion; DataFactory.Instance.Save(writablePage, saveAction, EPiServer.Security.AccessLevel.Administer); items.Clear(); } if (_stopSignaled) { return "Stop of job was called"; } return "Display types updated for each block."; } private static IEnumerable<ArticlePage> GetAllPages() { var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>(); var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>(); var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>(); var pageType = contentTypeRepository.Load("ArticlePage"); var pages = contentModelUsage.ListContentOfContentType(pageType); var publishedPages = pages.Select(u => contentRepository.Get<IContent>(u.ContentLink)) .Where(x => (x as IVersionable).Status.Equals(VersionStatus.Published) && !x.IsDeleted) .Cast<ArticlePage>(); return publishedPages; } }
Hi,
I would like to update the display options ("data-epi-content-display-option") of all blocks on the whole website to "half".
I start with one block. This is what I have done so far:
But this creates a new copy of the block instead of saving existing block's display option.
If I remove .CreateWritableclone() then I get exception that RenderSettings property is readonly.
Any one has done something similar or knows how to deal with this?
Thanks,
CMS Version: 11