How to modify and publish a page programmatically in EPiServer
by Rizo on apr.28, 2010, under Developing
When you want to alter a page programmatically you need to use EPiServers CreateWriteableClone on the working page. This is because the Page itself is readonly. Now, there’s plenty of info out there how to do this, but just to refresh your mind, here’s some code:
[sourcecode language=’c#’]
PageData clone = pd.CreateWritableClone();
clone.Property[”MyProperty”] = ”New value”;
DataFactory.Instance.Save(clone, SaveAction.Publish);
[/sourcecode]
Simple enough, right?
Now, here’s the catch. If you are for example saving page ID’s into a customproperty, simply like this ”42 43 553 23” and so forth and you want to change this programmatically by removing one and adding one. So for example, going from ”42 43 553 23” to ”42 43 55” won’t work with the code above. The result will be ”42 43 553 23 55” as it will only add it up to your current value. So you want to clear your value before you set it although that is also not enough. You’ll have to save the page after you clear it and THEN you can set your new value and save the page. Something like this
[sourcecode language=’c#’]
PageData clone = pd.CreateWritableClone();
clone.Property[”MyProperty”].Clear();
DataFactory.Instance.Save(clone, SaveAction.Publish);
clone.Property[”MyProperty”] = ”New value”;
DataFactory.Instance.Save(clone, SaveAction.Publish);
[/sourcecode]
If someone has a better way to override values without having to doublesave, please do let me know.
2 Comments for this entry
1 Trackback or Pingback for this entry
-
How to modify and publish a page programmatically in EPiServer
mars 11th, 2023 on 09:50CashSweep
I found a great…
juli 26th, 2013 on 12:56
Thank you for your post, this solved stupid problem I tried to solve for some hours.
augusti 20th, 2014 on 22:59
Pretty nice post. I just stumbled upon your weblog and wished to say
that I’ve truly enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and I hope you write again very soon!