volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

StopPublish is readonly?

I'm fairly new to EpiServer so maybe this question has been answered before. I couldnt find any easily but here goes;

When my page is saved, I check certain fields and when a field is set, the page should be archived. I want to set StopPublish to a date in the past but when I excute the code in the page I get a validator error; 

  • The property PageStopPublish is read-only.

I don't get this, why was the property settable in C# in the first place? I don't have any custom validators regarding StopPublish

#198393
Oct 26, 2018 10:16

Have you created a writable clone of the page? Try setting the property directly with page["PageStopPublish"] (or MetaDataProperties.PageStopPublish).

At least previously this built in property had to be set like this I think.

#198395
Oct 26, 2018 10:41

Hi Johan,

The page is a new instance all together. I've tried setting the property directly; still the same exception is thrown direclty. The MetaDataProperties doesn't seem to be accessible. That property doesn't exisist on my PageData object

Thanks for the effort tho

#198407
Oct 26, 2018 12:26

Hmm can you provide some more info about the context in which you are doing this? Is it in the save event? The following code works:

Edit: should be noted, this is with cms version 11

var c = ServiceLocator.Current.GetInstance<IContentRepository>();
var p = c.GetDefault<ArticlePage>(ContentReference.StartPage);
p.Name = "asjdlkajsd";
p.StopPublish = DateTime.Now;
c.Save(p, SaveAction.Publish);

(It is created as expired).

#198408
Edited, Oct 26, 2018 12:39

I hook on the 'EventsSavedContent' in a IInitializableModule  
Of course my accutal code is more elaborate

  private void EventsSavedContent(object sender, ContentEventArgs e)
        {
//((PageData)e.Content).StopPublish = DateTime.Now.AddSeconds(-1);
e.Content["PageStopPublish"] = DateTime.Now.AddSeconds(-1);

}
#198410
Oct 26, 2018 13:42
Vote:

In that event I believe the page has already been created and saved to the repository so based on the code you provide you need to create a new editable version and save that yourself i.e.: 

var newPage = (e.Content as PageData).CreateWritableClone();
newPage.StopPublish = DateTime.Now;
EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>().Save(newPage, SaveAction.ForceCurrentVersion | SaveAction.Publish);
#198413
Oct 26, 2018 15:57

I see now, the item is not a writeableClone to begin with

#198414
Oct 26, 2018 16:01

not sure if you have reasons not to, but it sounds like listening to Saving/Publishing event would make things easier to you

#198415
Oct 26, 2018 16:16
error This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.