sitefinitysitefinity-10

Get parent item from blogpost viewmodel - Sitefinity


I need to get the parent (blog) item in the razor view from the viewmodel object for any given blog post.

I'm doing this in the standard List.BlogPostList.cshtml file.

More specifically, I need either the set URL or title. Since the controller is wrapped up in the resources for this application, I don't have direct access to it.


Tried

item.ParentItem().DataItem.GetDefaultUrl()

Got this in the error log:

Could not find a parent item property for the given item. This extension method should only be used for accessing a parent item of DynamicContent items.


Tried

item.GetRelatedParentItems("Blog").ToString()

Got this in the error log:

Type "Blog" cannot be resolved.

Looks like that's close, but I cannot guess as to the parentItemsTypeName magic string it wants, and of course the Sitefinity documentation is attrocious.


Solution

  • Something like this should work:

    @foreach (var item in Model.Items)
    {
           var parent = (item.DataItem as BlogPost).Parent;
           var parentTitle = parent.Title;
           var parentUrl = parent.UrlName;
    }
    

    Make sure to add the following using clauses as well:

    @using Telerik.Sitefinity.Blogs.Model;