How to get Umbraco Element as page?
# help-with-umbraco
t
Hi I'm using UmbracoHelper to get pages by guid. UmbracoHelper.Content(someGuid); This works with all pages and guids I pass in (using Umbraco 13). When I pass in a guid for an Element it returns null. To get the element guid, I have it declared under the components folder in my cshtml file and in debug mode I noticed the key value the element I created had. I passed that value in but came back as null. Am I doing this wrong? How could I get all the information in an element I created?
l
Elements are not pages and cannot exist in the Content Tree, so do behave a bit differently. Each one is a 'block' value in a Block List/Grid property and will be of a 'BlockListItem' model which you can then check/cast its type for any particular views. Not sure what/where you're trying to get their details... but just a general example, you'd have a Block List like:
Copy code
@foreach (var block in Model) {
    @await Html.PartialAsync($"~/Views/Partials/blocks/{block.Content.ContentType.Alias}.cshtml", block)
}
And then in the view(s):
Copy code
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockListItem>
@{
    if (Model.Content is not ElementTypeHere content)
    {
        // error message/handling/etc
        return;
    }

    // else, content now a typed variable for your element
}
And you just access everything as a property with strongly typed models. Or, if you don't want strongly typed, Elements can just be generically IPublishedContent.
18 Views