Programmatically update the start page error

Vote:
 
public ActionResult Index(ShippersPage currentPage)
        {
            var startPage = _contentRepository.GetDefault<StartPage>(ContentReference.StartPage).CreateWritableClone() as StartPage;
            startPage.Shippers = currentPage.ContentLink;
            _contentRepository.Save(startPage, EPiServer.DataAccess.SaveAction.CheckOut, EPiServer.Security.AccessLevel.Edit);

            var model = new ShippersPageViewModel(currentPage);

            model.Shippers = _loader.GetChildren<ShipperPage>(currentPage.ContentLink);


            return View(model);
        }

When i call .Save i get the error: 'Content type "StartPage" is not allowed to be created under parent of content type "StartPage"'

Im trying to update property on the StartPage but it wont let m e because it tries to create new page? I just wnat to update the existing one.

#261798
Sep 07, 2021 11:21
Vote:
 

That looks correct. Are you sure there's not been any changes with page type restrictions with the structure of your site that would mean errors would occur when saving. You don't have a startpage under a startpage already?

Also you could try to change the SaveAction to SkipValidation

#261799
Sep 07, 2021 11:34
Vote:
 

Make sure no AvailableContentTypes defined on the startpage

[AvailableContentTypes(Include = new[] {
                                        typeof(StartPage),
                                        typeof(ContainerPage))]
public class SysRoot : PageData
#261801
Sep 07, 2021 12:18
Vote:
 

You should use

var startPage = (StartPage)_contentRepository.Get<StartPage>(ContentReference.StartPage).CreateWritableClone();

Not

var startPage = _contentRepository.GetDefault<StartPage>(ContentReference.StartPage);

GetDefault gives you a new instance, while Get gives you an existing instance. No need to call CreateWriteableClone() on the instance you get from GetDefault either, since it's already writeable.

#261825
Edited, Sep 07, 2021 20:44
Scott Reed - Sep 08, 2021 7:59
good spot, I totally glazed over that :p
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.