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
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?
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:
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.
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
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.
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:
David
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.