EPiServer: Remember to set a PageName for programmatically created Pages

When attempting to import an exported EPi Server site I got the error message Exception: “Name” cannot be empty.[] appearently when importing a page.

Add two together and you’ll quickly realize that a Page lacks a PageName.. or do like me and spend hours on figuring it out the cause.

By enabling the error logs the following stack shows where the exception is triggered:

EPiServer.Core.RequiredPropertyValueException: "Name" cannot be empty.
   at EPiServer.Core.PropertyData.Clear()
   at EPiServer.Core.PropertyData.SetPropertyValue(Object value, SetPropertyValueDelegate doSet)
   at EPiServer.Core.PageData.set_PageName(String value)
   at EPiServer.Core.Transfer.PageTransfer.CreateDefaultPage(Dictionary`2 propertyStringLookup, Boolean& isUsingPrototype)
   at EPiServer.Core.Transfer.PageTransfer.HasPageChanged(Dictionary`2 propertyStringLookup, PageData& page, Boolean& newPage)
   at EPiServer.Core.Transfer.PageTransfer.Import(RawPage rawPage, AccessLevel requiredDestinationAccess, Guid& importedPageGuid)
   at EPiServer.Core.Transfer.PageTransfer.<>c__DisplayClass6.<Import>b__0()
   at EPiServer.Util.CommandHandler.RetryExecuteIfException(Action command, Type handledExceptionType, Int32 numberOfRetry, TimeSpan waitFor)
2013-07-02 15:40:57,084 ERROR [5] EPiServer.Core.Transfer.TransferLogger.Error - 10.5.3 Export/import error: [Importing page 45_45] Exception: "Name" cannot be empty.[]
EPiServer.Core.RequiredPropertyValueException: "Name" cannot be empty.
   at EPiServer.Core.PropertyData.Clear()
   at EPiServer.Core.PropertyData.SetPropertyValue(Object value, SetPropertyValueDelegate doSet)
   at EPiServer.Core.PageData.set_PageName(String value)
   at EPiServer.Core.Transfer.PageTransfer.CreateDefaultPage(Dictionary`2 propertyStringLookup, Boolean& isUsingPrototype)
   at EPiServer.Core.Transfer.PageTransfer.HasPageChanged(Dictionary`2 propertyStringLookup, PageData& page, Boolean& newPage)
   at EPiServer.Core.Transfer.PageTransfer.Import(RawPage rawPage, AccessLevel requiredDestinationAccess, Guid& importedPageGuid)
   at EPiServer.Core.Transfer.PageTransfer.<>c__DisplayClass6.<Import>b__0()
   at EPiServer.Util.CommandHandler.RetryExecuteIfException(Action command, Type handledExceptionType, Int32 numberOfRetry, TimeSpan waitFor)
   at EPiServer.Core.Transfer.PageTransfer.Import(ITransferPageData page, AccessLevel requiredDestinationAccess)
   at EPiServer.Enterprise.DataImporter.ImportPages(XmlTextReader reader, ZipPackage package)
   at EPiServer.Enterprise.DataImporter.ImportRaw(ZipPackage package)
   at EPiServer.Enterprise.DataImporter.Import()

My problem was caused by me programmatically creating EPiServer pages (more specifically comments as Pages) without specifying a PageName.

Hopefully I’ll (and you too) avoid this problem in the future by remembering this little detail.

The code below illustrates my point:

// This is wrong. WE HAVE TO SET A PAGENAME D:
var commentPage = DataFactory.Instance.GetDefaultPageData(parentPageRef, "Comment");
commentPage["Content"] = comment;

DataFactory.Instance.Save(commentPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

//== END WRONG CODE

// This is right, yay a pagename!
var commentPage = DataFactory.Instance.GetDefaultPageData(parentPageRef, "Comment");

commentPage.PageName = "Comment - " + DateTime.Now.ToString(); // The PageName!
commentPage["Content"] = comment;

DataFactory.Instance.Save(commentPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

//== END WORKING CODE
This entry was posted in EPiServer and tagged , , , . Bookmark the permalink.