0
votes

I am very new to Umbraco and trying to fix an issue where the TypedContentAtRoot() is returning null. I have to get the URL of a child node that the user has to be redirected to after clicking a button. The child page has been added to the parent page as a descendant and I have confirmed the same.

Here is the code I am using to retrieve the parentNode of the current page.

     var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); 
      var homeNode = umbracoHelper.TypedContentAtRoot().Where(w => 
                                    w.ContentType.Alias.Equals("HomePage"));
     var descendants =homeNode.DescendantsOrSelf("SubmittedPage");
     var confirmationPage = descendants.FirstOrDefault();
return confirmationPage?.Url;

To also verify this, I performed a check where I can get the current PageID and that is also being returned as null

var currentPageId = UmbracoContext.Current.PageId;

Basically I have a main HomePage as a document type. It has various child pages that I have verified exists. There is some jQuery validation that sends a get Request to retrieve the confirmationPage Url as shown in the code below.

$.get('/Umbraco/Api/ConfirmationPageUrl',
function(data) {      
  window.location.replace(data);
});
2
Are you absolutely sure your aliases are upper-camelcase? By default, Umbraco will give a document type of name "HomePage" an alias of "homePage" :)Mikkel
@Mikkel I rewrote the word "homePage" in the question while posting in on StackOverflow just to prevent using actual code on a public forum. However I recheck and it was upper-camelcase.user2140740

2 Answers

1
votes

Is it perhaps your UmbracoContext that hasn't been set? Or maybe you could try passing the current page ID to your controller, get the node, traverse up the tree and find the single "true" parent that way?

0
votes

I found the solution to my problem. The page I was looking for was not available as a ContentType and hence it was returning "null". When I created the respective page it came up in the list and could solve it. But your inputs definitely helped. Thanks