Trying to create a page through an Admin Plugin

I'm trying to create a page through a json import Admin Plugin that I created. But I get the following error:

System.ComponentModel.DataAnnotations.ValidationException occurred
  HResult=0x80131500
  Message=Property 'Title' is required.
  Source=EPiServer
  StackTrace:
   at EPiServer.Core.ContentProvider.ThrowValidationException(ICollection`1 errors)
   at EPiServer.Core.ContentProvider.Validate(IContent content, ContentSaveValidationContext saveValidationContext)
   at EPiServer.Core.Internal.DefaultContentRepository.Save(IContent content, SaveAction action, AccessLevel access)
   at CPH.Plugins.Admin.ImportNews.ImportNewsPlugin.ImportPages(Object sender, EventArgs e) in C:\Users\bso\Documents\cph-epi10\src\Cph.Website\Plugins\Admin\ImportNews\ImportNewsPlugin.cs:line 44

My code looks like this:

using System;
using System.IO;
using CPH.Models.Pages;
using CPH.Website.Plugins.Admin.ExportNews;
using EPiServer;
using EPiServer.Core;
using EPiServer.PlugIn;
using EPiServer.ServiceLocation;
using Newtonsoft.Json;
 
namespace CPH.Plugins.Admin.ImportNews
{
    [GuiPlugIn(DisplayName = "Import News Pages", Description = "Import the news pages through a json file", Area = PlugInArea.AdminMenu, Url = "~/Plugins/Admin/ImportNews/ImportNews.aspx")]
    public partial class ImportNewsPlugin : SimplePage
    {
        protected void ImportPages(object sender, EventArgs e)
        {
            string inputContent;
            using (StreamReader inputStreamReader = new StreamReader(FileUpLoad1.PostedFile.InputStream))
            {
                inputContent = inputStreamReader.ReadToEnd();
            }
 
            NewsStructure ns = JsonConvert.DeserializeObject<NewsStructure>(inputContent);
 
            if (ParentID.Text == "")
            {
                ProgressText.Text = "Parent ID missing";
            }
 
            PageReference parent = new PageReference(int.Parse(ParentID.Text));
 
            var cRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            NewsPage nPage = cRepository.GetDefault<NewsPage>(parent);
            var firstPage = ns.YearFolders[0].MonthFolders[0].ExportedPages[0];
            nPage.ArticleText = firstPage.ArticleText;
            nPage.Header = firstPage.Header;
            nPage.Subheading = firstPage.Subheading;
            nPage.OriginalPublishDate = firstPage.ModifiedDateTime;
            nPage.PageName = firstPage.PageTitle;
            nPage.PageTitle = firstPage.PageTitle;
            nPage.Name = firstPage.PageTitle;
 
            cRepository.Save(nPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
        }
    }
}
The error points to the line:
cRepository.Save(nPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);


[Pasting files is not allowed]

#180450 Jul 11, 2017 11:36
  • Member since: 2007

    The error message says that 'Title' is required. You can skip validation in the Save method by calling cRepository.Save(nPage, EPiServer.DataAccess.SaveAction.Publish | EPiServer.DataAccess.SaveAction.SkipValidation, EPiServer.Security.AccessLevel.NoAccess); A better soultion would be to specify the title for every page that you create though, if it's required that is.

    #180455 Edited, Jul 11, 2017 13:28