Update display options of particular block programatically instead of updating from CMS

Vote:
 

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:

            var pageInstance = (CampaignPage)instances.Where(x => x.Name.Equals("Somepage")).FirstOrDefault().CreateWritableClone();
            var contentAreaItem = instances.Where(x => x.Name.Equals("Somepage")).FirstOrDefault().MainContentArea.FilteredItems
                .Where(x => x.GetContent().Name.Equals("Someblock"))
                .FirstOrDefault().CreateWritableClone();
            //DisplayOption loadDisplayOption = contentAreaItem.LoadDisplayOption();
            contentAreaItem.RenderSettings.Remove("data-epi-content-display-option");
            contentAreaItem.RenderSettings.Add("data-epi-content-display-option", "half");
            pageInstance.MainContentArea.Items.Remove(contentAreaItem);
            pageInstance.MainContentArea.Items.Add(contentAreaItem);
            contentRepository.Save(pageInstance, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.Administer);

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

#197974
Edited, Oct 18, 2018 10:54
Vote:
 

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;
	}
}
#197980
Oct 18, 2018 14:15
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.