Dynamic redirect to first child page

Jignesh Patel asked on October 15, 2015 07:03

Hi,

I need to redirect to child page dynamically. For an example if one of the child page has custom field (field type is datetime) and value is future date than redirect to page which is first in tree.

Can you please help me and direct me what I have to do?

I have done following thing but not sure that is a best practice or not.

(1) Created and registered Kentico Web Part control (2) Add newly created webpart control into page

Logic written in .cs file is

var doc = CurrentDocument.Children.FirstOrDefault(x => x.GetDateTimeValue(FieldNames.ValidUntil, DateTime.MinValue) > DateTime.Now);
if (doc != null) {
    URLHelper.Redirect(doc.RelativeURL); 
}

but not luck as doc has null value.

Thanks

Correct Answer

Maarten van den Hooven answered on October 15, 2015 09:35

Hi Jignesh you can better change your code like explained in the article of Jaroslav Kordula : http://devnet.kentico.com/articles/kentico-8-technology-documentquery-api

This worked for me where DOcumentCreatedWhen is your custom DateTime field :

var doc = DocumentHelper.GetDocuments()
                  .TopN(1)
                  .Path(CurrentDocument.NodeAliasPath, PathTypeEnum.Children)
                  .OnSite(CurrentSiteName)
                  .Where("DocumentCreatedWhen", QueryOperator.GreaterOrEquals, DateTime.Now);
// Go through the documents 
foreach (var document in doc)
{
    URLHelper.Redirect(document.RelativeURL);
}

Good luck!!!

0 votesVote for this answer Unmark Correct answer

Recent Answers


Jignesh Patel answered on October 15, 2015 08:33

Looks like I am doing something wrong. I think I should fetch data from custom table. I do have view called View_Custom_Jigs_Joined and I can see all data I need are there.

Now can you please help me to fetch data from view? I am quite new in Kentico so any suggestions should help me lots.

Thanks

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.