Access individual Blocks using Content Api

Vote:
 

Hi,

I have setup the basics for using content api to work on some headless cms requirement. The setup is working as expected.One of the requirement is to use episerver blocks in some front end app.Is there a way to achieve the same using content api.

For instance ,i have a ContactBlock with few properties like(Name,Header,Email,Phone,etc).

If I have to call this ContactBlock on some front end application using content api,how can i achieve it without including the block in some page.

Another thing is once i fire the api calls using

/api/episerver/v2.0/content/{guid}

the output i get it in json format which is expected but is there is a way to customize the json output with only the relevant details like contentlink,name,url etc(referring the below response)

{"contentLink":{"id":6,"workId":0,"guidValue":"627d4fba-685b-4d05-8115-e76f5fed1a59","providerName":null,"url":"https://loca.abc.com/","expanded":null},"name":"Abc Start Page","language":{"link":"https://loca.abc.com/","displayName":"English","name":"en"},"existingLanguages":[{"link":"https://loca.abc.com/","displayName":"English","name":"en"}],"masterLanguage":{"link":"https://loca.abc.com/","displayName":"English","name":"en"},"contentType":["Page","StartPage"],"parentLink":{"id":1,"workId":0,"guidValue":"43f936c9-9b23-4ea3-97b2-61c538ad07c9","providerName":null,"url":null,"expanded":null},"routeSegment":"abc-start-page","url":"https://loca.abc.com/","changed":"2018-12-20T14:30:33Z","created":"2018-12-20T14:30:00Z","startPublish":"2018-12-20T14:30:00Z","stopPublish":null,"saved":"2021-03-31T11:24:00Z","status":"Published","category":{"value":[],"propertyDataType":"PropertyCategory"}}

Will this be achieved through mapper class or is there any other approach for the same.

Kindly advice.

Regards.

#252785
Apr 09, 2021 9:27
Vote:
 

Hi,

You should be able to access blocks in pretty much the same way as you access content via the API using the /api/episerver/v2.0/content/{contentReference} endpoint. You'll just need to pass in either the ID or the GUID of the block you want to retrieve. As with any request to the content delivery API, make sure you pass in an Accept-language HTTP header otherwise you might not get the content item returned.

The simple way to limit the fields returned is to use the "select" querystring parameter which accepts a comma delimited set of fields to return. For example, to return the "Heading" and "IncludePublishDate" properties of a block with ID = 68, your request URL would look like this:

/api/episerver/v2.0/content/68?select=heading,includePublishDate

#252928
Apr 09, 2021 13:01
Vote:
 

Thanks for the response.

The issue with accessing blocks is ,it needs to be associated with some page & only then i can access the block.What if i just want to get the properties of a block in this case a Contact Block & use it in my front end app?

#252934
Apr 09, 2021 14:07
Vote:
 

You can access the block directly using the method Paul has described. You need the ID of the block that you can get when viewing/editing it in the UI: 

#252939
Apr 09, 2021 15:29
Farhin - Apr 09, 2021 17:48
This again is associated with a particular page(in the above ,its associated with Home Page). Why is it needed to be a part of any page in terms of block added to the page. Cant I just create a block & use it properties elsewhere in some other app using content api?
David Knipe - Apr 09, 2021 18:15
Hello Farhin the block doesn’t need to be associated to a page or any other content for that matter. The id for the block is the same regardless of whether it’s used on a page or not. Try it, create a block, get the id as I showed above and use the API to get the content as Paul described.
Paul Gruffydd - Apr 09, 2021 18:18
I'm not entirely sure I follow. The block in the screenshot is standalone. It may be used on a page but it doesn't have to be used on a page in order to be retrievable in the API. The "_Demo/Home" folder it sits in isn't important, it could be anywhere, and the fact that the home page is highlighted in the site tree isn't important either. All that's needed to access that block is its ID. You can either get a reference to it as David's pointed out in which case you can access it directly using the content API (/api/episerver/v2.0/content/{contentReference}) or you can use the search API (/api/episerver/v2.0/search/content) if you want to search for it using its properties rather than its ID.
Vote:
 

Think we relied at the same time saying the same thing there Paul :) 

#252945
Apr 09, 2021 18:21
Paul Gruffydd - Apr 12, 2021 10:12
Ha. It would appear so. I need to type faster 😁
Vote:
 

Thanks Paul & David.That makes it much more clear to understand.However,i have a follow up question on this.How does it works in case of a block with a hierarchy.

Going with the example of Contact Block,this contains some content areas in the form of multiple addresses.So the response that i get using /api/episerver/v2.0/content/{contentReference} endpoint is related to contact block(Name,comments for instance being the properties of Contact block).For Content Areas, below is the response with another id given in the link ie 13411 in this case.

"Addresses":{"value":[{"displayOption":"","tag":null,"contentLink":{"id":13411,"workId":0,"guidValue":"b1581035-9337-455d-bdb6-149e60c450ba","providerName":null,"url":null,"expanded":null}}],"propertyDataType":"PropertyContentArea"}

So, to use this block with multiple hierarchy, will it be the same approach with multiple endpoint calls or we get the id using contentlink.id property from first call & initiate a second call?

Note:The example on the above contact block is a made up so please excuse if it makes no logical sense.

Regards.

#253060
Edited, Apr 12, 2021 9:52
Vote:
 

Hi,

If your content has references to other content such as content in ContentAreas and you want to automatically retrieve that content in the same request, you can use the "expand" parameter. You can limit it to just expanding certain fields by naming them or you can expand all expandable fields by passing * as the value for the expand field. For example, to request a content with ID=123 and expand two content area properties, your query would look like this:

/api/episerver/v2.0/content/68?expand=MyContentAreaProperty,MyOtherContentAreaProperty

#253061
Apr 12, 2021 10:10
Vote:
 

Thanks again Paul.

The expand parameter is helpful only when there is 2 level ie Contacts---- Addresses in this case.

/api/episerver/v2.0/content/68?expand=MyContentAreaProperty,MyOtherContentAreaProperty

/api/episerver/v2.0/content/68?expand=*

The above is fine but what happens when it goes to level more than 2.If I have another contentarea within Address.How will i achieve this?

Below is the Block Structure that i have:

ContactBlock:

String: Name

String:Comments

ContentArea: Addresses

Address Block:

String : Street

ContentArea: Cities

City Block:

String: Name

This is a 3 level structure for a block.

#253064
Apr 12, 2021 10:53
Vote:
 

Hello Farhin

By default the API expands one level deep. If you want to expand beyond one level deep you can you extend it as described in the documentation here:

https://world.episerver.com/documentation/developer-guides/content-delivery-api/getting-started/how-to-customize-data-returned-to-clients/#ContentAreaPropertyModel

David

#253065
Edited, Apr 12, 2021 10:57
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.