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.
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.