Rizo's

Tag: CreateWritableClone

How to modify and publish a page programmatically in EPiServer

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

11 Comments :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!