Ich habe einige Vorschläge zum Hinzufügen eines Blocks zu einer Seite gefunden, kann aber nicht so funktionieren, wie ich es möchte, also kann vielleicht jemand helfen.
Ich möchte einen geplanten Job haben, der eine Datei durchsucht, neue Seiten mit einem bestimmten Seitentyp erstellt und in der neuen Seite einige Blöcke zu einer Inhaltseigenschaft hinzufügt. Die Blockfelder werden mit Daten aus der gelesenen Datei aktualisiert.EPiServer 9 - Block programmgesteuert zu neuer Seite hinzufügen
Ich habe den folgenden Code in den Job eingeplant, aber es funktioniert nicht bei
repo.Save((IContent) newBlock, SaveAction.Publish);
der Seitenname Angabe der Fehler muss mindestens ein sichtbares Zeichen enthalten.
Dies ist mein Code:
public override string Execute()
{
//Call OnStatusChanged to periodically notify progress of job for manually started jobs
OnStatusChanged(String.Format("Starting execution of {0}", this.GetType()));
//Create Person page
PageReference parent = PageReference.StartPage;
//IContentRepository contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
//IContentTypeRepository contentTypeRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentTypeRepository>();
//var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
//var slaegtPage = repository.GetDefault<SlaegtPage>(ContentReference.StartPage);
IContentRepository contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
IContentTypeRepository contentTypeRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentTypeRepository>();
SlaegtPage slaegtPage = contentRepository.GetDefault<SlaegtPage>(parent, contentTypeRepository.Load("SlaegtPage").ID);
if (slaegtPage.MainContentArea == null) {
slaegtPage.MainContentArea = new ContentArea();
}
slaegtPage.PageName = "001 Kim";
//Create block
var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
var newBlock = repo.GetDefault<SlaegtPersonBlock1>(ContentReference.GlobalBlockFolder);
newBlock.PersonId = "001";
newBlock.PersonName = "Kim";
newBlock.PersonBirthdate = "01 jan 1901";
repo.Save((IContent) newBlock, SaveAction.Publish);
//Add block
slaegtPage.MainContentArea.Items.Add(new ContentAreaItem
{
ContentLink = ((IContent) newBlock).ContentLink
});
slaegtPage.URLSegment = UrlSegment.CreateUrlSegment(slaegtPage);
contentRepository.Save(slaegtPage, EPiServer.DataAccess.SaveAction.Publish);
_stopSignaled = true;
//For long running jobs periodically check if stop is signaled and if so stop execution
if (_stopSignaled) {
return "Stop of job was called";
}
return "Change to message that describes outcome of execution";
}
Große einstellen - die es gelöst. Vielen Dank – kilarsen
@kilarsen Wenn eine Antwort Ihr Problem gelöst hat, markieren Sie es bitte als Antwort und stimmen Sie auch dafür ab –