Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

Add block to ContentArea programmatically C# Episerver 11.x

$
0
0

Hi Team,

I was trying to add the blocks to the ContentArea programmatically and used the following code.

DisplayOption threw an exception of type 'System.NotImplementedException'

//Save the page

contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish,EPiServer.Security.AccessLevel.Read);

//Save the Block

ContentAssetHelper contentAssetFolder = ServiceLocator.Current.GetInstance<ContentAssetHelper>();
var contentAsset = contentAssetFolder.GetOrCreateAssetFolder(myPage.ContentLink);

var newBlock = contentRepository.GetDefault<RawHTML>(parentLink: contentAsset.ContentLink);

var writableClonePage = (InnerPage)myPage.CreateWritableClone();

var rep = ServiceLocator.Current.GetInstance<IContentRepository>();

newBlock.HTMLContent = new XhtmlString(WebUtility.HtmlDecode(c.Content));

IContent icontentBlock = (IContent)newBlock;
icontentBlock.Name = c.PaneName;

ContentReference savedReference = rep.Save(icontentBlock, SaveAction.Publish, AccessLevel.NoAccess);

//Add the Block to ContentArea

var AddblockItem = new ContentAreaItem
{
ContentGuid = Guid.NewGuid(),
ContentLink = savedReference
};

writableClonePage.ContentArea.Items.Add(AddblockItem);
rep.Save((IContent)writableClonePage, SaveAction.Publish, AccessLevel.Read);

Can you please help me where I made a mistake?

Thanks

Ashley


Viewing all articles
Browse latest Browse all 8020

Trending Articles