Kentico: How to redirect based on document type field
Question
I have a certain document type in Kentico that has a boolean field that when true i need the page to redirect to another URL (in this case a 404 page).
Where is the best place to do this?
and how do i access the kentico data context in code so that i can write code that pulls the document types field and redirects based on it (because currently trying to access Dataitem("MyFieldName") errors because Kentico doesn't use DataItem for data binding, even though Eval("MyFieldName") still works.
Solution
You can access Kentico context data via the CMSContext object.
<%
if ((bool)CMSContext.CurrentDocument.DataRow["MyFieldName"])
Response.Redirect("PageNotFound.aspx");
%>
OTHER TIPS
If you only want to be able to redirect a page to another page. Kentico already have something built in, just go to the Page->Properties->Menu
. In the menu actions section you can specify a URL for redirection.
Otherwise for requirement described you can get the boolean value by:
if(ValidationHelper.GetBoolean(
CMSContext.CurrentDocument.GetValue("MyFieldName"), false)))
{
Response.Redirect("/404.aspx");
}