Try our conversational search powered by Generative AI!

Programmatically build block dissappears when added to content area

Vote:
 

I struggled for ages to find how to build blocks programmatically, but I finally 'got' it. Now when I come to do what I thought was the 'easy' bit I've hit a problem.

This is the gist of my code:

var savBlk = Locate.ContentRepository().GetDefault<ServiceStatusBlock>(ContentReference.GlobalBlockFolder);

fill in savBlk properties

var contentrepo = Locate.Advanced.GetInstance<IContentRepository>();

setup access if needed

var asContent = savBlk as IContent;
asContent.Name = "thenname";
var blkRef = contentrepo.Save(asContent, EPiServer.DataAccess.SaveAction.Publish);
var loaded = contentrepo.Get<IContent>(blkRef);
if (loaded != null)
{
   ServiceStatus wrServiceStatus = CurrentPage.CreateWritableClone() as ServiceStatus;

   IContent asContent2 = wrServiceStatus as IContent;

   wrServiceStatus.ServicesContentArea.Add(loaded); // At this point the new block shows as being added to the contentarea

   var ContentRef2 = contentrepo.Save(asContent2, EPiServer.DataAccess.SaveAction.Publish, AccessLevel.NoAccess);

}

 

No errors etc, but when the page reloads the new block is not present on the page but does exist in the global block folder.

Any help is much appreciated.

 

#79184
Dec 10, 2013 17:21
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.