EPiServer 9 - 以编程方式将块添加到新页面

时间:2017-01-26 08:40:16

标签: block episerver episerver-9

我已经找到了一些关于如何向页面添加块的建议,但无法按照我想要的方式工作,所以也许有人可以提供帮助。
我想要做的是让一个预定的作业读取文件,创建具有特定页面类型的新页面,并在新页面中添加一些块到内容属性。块字段将使用读取的文件中的数据进行更新。

我在预定作业中有以下代码,但在

处失败
repo.Save((IContent) newBlock, SaveAction.Publish);

给出错误

  

页面名称必须至少包含一个可见字符。

这是我的代码:

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";
}

1 个答案:

答案 0 :(得分:3)

您可以设置名称

((IContent) newBlock).Name = "MyName";