SaaS CMS has officially launched! Learn more now.

Get Block Property Data in Api Controller

Vote:
 

Hi Everyone,

I want data in Controller from specific block which is basically used in one of the landing page (page type) and inside EPiServer.Core.ContentArea page type field.

Can anyone let me know with classes I need to use in my Controller to fetch block property values?

Thanks

#285814
Edited, Aug 19, 2022 20:18
Vote:
 

Is this B2B Commerce with content cloud or spire?

#285816
Aug 19, 2022 21:09
Vote:
 

No - This is a MVC implemented solution.

#285851
Aug 20, 2022 10:07
Vote:
 

You posted in B2B forums,, but I am assuming you meant content cloud.  If you want to use content delivery api you can query the block and the get the properties through rest.  

https://docs.developers.optimizely.com/content-cloud/v1.5.0-content-delivery-api/docs/quick-start

If you want to get in the controller then you use the content repository to load the content reference.  You can get the the refereneces from the content area used the items.

 var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
    if (page != null && page.ContentArea != null && page.ContentArea.Items.Any())
    {
        foreach (var contentItem in page.ContentArea.Items)
        {
            var item = contentLoader.Get<YOUR_CONTENT_TYPE>(contentItem.ContentLink);
            // output necessary properties from content instance
        }
    }
#285854
Edited, Aug 20, 2022 17:55
Vote:
 

Hi Mark, What is page instance here ? Can you please help me with real time code ?

#285939
Aug 22, 2022 7:59
Scott Reed - Aug 23, 2022 9:54
The example that mark has given assumes page is some page in the CMS with a ContentArea where you're trying to load the information from a block. If you're trying to load a specific block such as a global one in the assets area you would need to load it via a ContentLink reference to the block which you can then load the block out with the example Mark has given. What exactly is the scenario of where the block is you're trying to load, is it in a page or global and are you doing this code in a page controller or an API controller (as your forum title suggests)?
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.