[Solved]Access a Block Item from a View Component
# help-with-umbraco
m
I am trying to access a Block Item from a View Component but blocks keeps coming back empty. Am I on the wrong track entirely. I'm actually using uSkinned but I think their components are just essentially Block Items? Maybe it's different when accessing from a ViewComponent? Thanks
Copy code
cs
public async Task<IViewComponentResult> InvokeAsync()
 {
     var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
     //deleted for brevity

             // Find the Services landing page
             var servicesNodeId = 2039; // ID of the Services landing page
             var servicesLandingPage = umbracoContext.Content.GetById(servicesNodeId);

             if (servicesLandingPage != null)
             {
                 var servicesChildren = servicesLandingPage.Descendants();

                 foreach (var servicePage in servicesChildren)
                 {
                     _logger.LogInformation("-- servicePage - {0}", servicePage.Name);

                     var blocks = servicePage.Value<IEnumerable<BlockListItem>>("CB_relatedPeopleStrip");
                     if (blocks != null)
                     {
                         foreach (var block in blocks)
                         {
                             var blockContent = block.Content;
                             var doctypeAlias = content.ContentType.Alias;
                             _logger.LogInformation(doctypeAlias);
                            
                         }
                     }
Basically, in the "Services" pages the client can add a component which is a mntp that selects a Person. Then on the Person pages they want to show which Services have picked those Persons. There is already a View Component in the Person pages that does it from the opposite direction, on the Person page they can pick a service, but they want it to work the other way too. Maybe I'm over-complicating, might be easier just to do it from Razor?
m
looks like it aught to work.. might help to strongly type things with modelbuilder?
ServicesChildren = servcicesLandingPage.Descendants<{ContentTypeOfServicePage}>();
then you should just be able to do
servicePage.CB_relatedPeopleStrip
although actually now I've written the modelsbuilder example... do you have the alias correct? usually umbraco would have
cB_relatedPeopleStrip
for a generated alias lower case first char. With modelsbuilder having
CB_relatedPeopleStrip
uppercase first char?
m
Thank you for having a look at this. I've just been having another look myself and I was one step removed. I should have been looking for "mainContent" first and then looping through the components that are in there. I am still a ways away but more on the right track now than I was, hopefully now it will be more successful. Unfortunately these pages were not created as a particular Document Type.
Copy code
cs
foreach (var servicePage in servicesChildren)
{
    _logger.LogInformation("-- servicePage - {0}", servicePage.Name);
    var pageComponents = servicePage.Value<IEnumerable<BlockListItem>>("mainContent");
    if (pageComponents != null && pageComponents.Where(x => !x.Settings.Value<bool>("hideFromWebsite")).Any())
    {
        foreach (var item in pageComponents.Where(x => !x.Settings.Value<bool>("hideFromWebsite")))
        {
            if (item?.Content != null && item.Content.ToString() == "Umbraco.Cms.Web.Common.PublishedModels.Cb_relatedPeopleStrip")
            {
                _logger.LogInformation("-- content - {0}", item.Content);

            }                                    
        }                                
    }
}
https://cdn.discordapp.com/attachments/1235985567955812422/1236288167301087283/image.png?ex=66377694&is=66362514&hm=cf985083e4eb03137b68c96296316a5d624bb6e8782f745bf27e56f4f2b2765c&
22 Views