2017-01-26 16 views
0

Я нашел несколько предложений о том, как добавить блок на страницу, но не могу заставить его работать так, как я хочу, поэтому, возможно, кто-то может помочь.
Что я хочу сделать, это иметь запланированное задание, которое читает файл, создавая новые страницы с определенным типом страницы и на новой странице, добавляя некоторые блоки к свойству контента. Поля блоков будут обновляться данными из прочитанного файла.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"; 
} 

ответ

2

Вы можете установить имя по

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

Великого - что решить ее. Большое спасибо – kilarsen

+0

@kilarsen, если ответ решил вашу проблему, отметьте его как ответ, а также проголосуйте за него –