2017-01-26 85 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如果答案解決了您的問題,請將其標記爲答案,併爲此投票贊成 –

相關問題