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;
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
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.
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
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).
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);
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);
I see now, the item is not a writeableClone to begin with
not sure if you have reasons not to, but it sounds like listening to Saving/Publishing event would make things easier to you