Try our conversational search powered by Generative AI!

StopPublish is readonly?

Vote:
 

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
Vote:
 

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
Vote:
 

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
Vote:
 

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
Vote:
 

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
Vote:
 

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

#198414
Oct 26, 2018 16:01
Vote:
 

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
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.