Get block list item in surface controller
# help-with-umbraco
i
Hi, In a surface controller if you have the Guid of an item in a block list, can you retrieve it easily and cast to a model type? I've tried using ContentService.GetById but it returns null when I pass in block list item's Guid. I want to perform some particular tasks based on the property values held in the block list item. Would appreciate any help to point me in the right direction.
l
Not sure if there is a way directly with only the block list id but you should be able to get all the properties from the page id, then filter down to the target block list from there
i
Thanks Lewis, yeah that's what I've had to do but I was wondering if there was an easier way, which I wasn't aware of? It feels like I may be missing something?
Copy code
c#
 var parentPage = _contentService.GetById(model.CurrentPageID);

 foreach (var property in parentPage!.Properties)
 {

     if (property.PropertyType.PropertyEditorAlias == "Umbraco.BlockList")
     {
         foreach (var val in property.Values)
         {
             string rawJson = val.PublishedValue.ToString();

             BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(rawJson);

             // there should only be one itme with a matching UniqueId value
             foreach (BlockItemData blockItemData in blockValue.ContentData.Where(x => x.Key == Guid.Parse(model.UniqueID)))
             {

                 var emailBodyJson = blockItemData.RawPropertyValues.Single(x => x.Key == "registrationConfirmationEmail").Value.ToString();

                 if (!emailBodyJson.IsNullOrEmpty()) {
                     var emailBodyJObj = (JObject)JsonConvert.DeserializeObject(emailBodyJson);
                     string emailBodyString = emailBodyJObj["markup"].Value<string>();
                 }

             }
         }

     }
}
Here I have the page id and the node id of the block list item (in UniqueID) and I want the value held in the block list item property "registrationConfirmationEmail". This works (and is a bit quick and dirty), but wondered if there was a better approach?
73 Views