Dienstag, 2. Februar 2010

Redirect to a PublishingPage in edit-mode from code-behind

After a long period of not-posting because of high stress at the current project at work, finally I found some time to write a little hint. I really hope to shorten the period of time between future posts. I promise, that I'll try! :-)

In the current project it's a requirement to create PublishingPages on a code-behind of an application page and redirect to that Page afterwards.

There're two little stumbling blocks you have to watch out. The first is, that you have to check out the newly created PublishingPage before redirecting, otherwise you'll get a nice error-message of type "You have not checked out this page. Click 'Edit Page' to edit the page.".

The next obstacle is - you wouldn't believe it -  the correct use of case-sensitive parameters in the querystring. So much to say for case-insensitiveness of querystring-parameters.

Use this code-snippet to redirect to a PublishingPage:

PublishingPage newPage = <Method that creates thePublishingPage>();
...
newPage.CheckOut();

string controlMode = "Edit";
string displayMode = "Design";

Page.Response.Redirect(string.Format("{0}?ControlMode={1}&DisplayMode={2}", newPage.Uri.AbsoluteUri, controlMode, displayMode), true);


Be sure to write "Edit" and "Design" with capitalized letters or else it wouldn't work.