Related data operations

Get the related items of a content item

To get the value of the related data field, you must specify the entity type, the item’s ID, and pass the related data field name.

If you have a related data field of your items and you want to get the related data of a specific item, you must execute a GET request to the following endpoint:

{baseurl}/api/default/{entity}(itemId)/{relatedDataFieldName}

Parameters

  • {entity} is the entity of the module you are working with
  • {itemId} is the ID of the specific item whose related data you want to get
  • {relatedDataFieldName} is the field name of the related data

Sample request

GET http://mysite.com/api/default/newsitems(3b177186-8b09-497c-8def-58613183d670)/RelatedEvents

Sample response

This service call returns only the related data, not the item itself. If you want to return both the item and its related data items you must use the $expand system query option, described below.

Get a content item and its related items

To get both the item and its related data, you must execute a GET request with the $expand system query option to the following endpoint:

{baseurl}/api/default/{entity}({itemId})$expand={relatedDataFieldName}

Parameters

  • {entity} is the entity of the module you are working with
  • {itemId} is the ID of the specific item whose related data you want to get
  • {relatedDataFieldName} is the field name of the related data

Sample request

GET http://mysite.com/api/default/newsitems(3b177186-8b09-497c-8def-58613183d670)?$expand=RelatedEvents

Sample response

Get a related media item in multisite

If you have multiple sites in your Sitefinity CMS instance and you want to get a media item, such as image or video, when you have defined its Source property to be default site source, you must pass an additional parameter to make the service call execute in the context of the current site.
This is necessary, because when the default site source option is selected, the related data field needs to resolve the default media content provider for the current site. This is because the requests to the web services are not executed in the context of a site.
For more information about setting the Source property of related media fields, see Add related media custom field to content items

To make the service, you must execute a GET request in the context of a site and you must use the sf_site parameter in the following way:

{baseurl}/api/default/{entity}({itemId})?sf_site={siteId}&$expand={relatedMediaFieldName}

Parameters

  • {entity} is the entity of the module you are working with
  • {itemId} is the ID if the item whose related media you want to get
  • {siteId} is the id of the desired site you want to get the item from
  • {relatedMediaFieldName} is the name of the related media field
EXAMPLE: Consider the following example:
  • You have two sites – Site A and Site B
  • You use the news items module (newsitems entity) with a related images field, named RelatedImages.
  • When creating the RelatedImages field, you have specified its Source property to be default site source.
  • In Site A you have created several news items and uploaded an image in the RelatedImages field for each of the items.
  • You have shared the news module with Site B.
  • Site B has been configured to use a default provider for media content, different than the one for Site A (the default option when creating a new site)
  • When you go to the news module in Site B and access some of the shared news items form Site A, you will not see the related image, because the default media content provider for this site is different, and it does not contain the related images.
  • If you want to display the news items from Site A on the Site B frontend, together with their related images, you need to make the following service call:
    {baseurl}/api/default/newsitems?sf_site={SiteAId}&$expand= RelatedImages.

Sample request

GET http://mysite.com/api/default/newsitems(3b177186-8b09-497c-8def-58613183d670)?sf_site=3f9c551c-1154-43ea-a856-0fde976af047&$expand=RelatedImages

Sample response

Relate two items

To create an item relation, you must execute a POST request to the required content item. The content item, which you want to relate another item to, must have a related data field that you want to populate. You must specify this related data field name in the URL of the request. In the request body you must specify the service URL to the item that you want to relate in JSON format using the @odata.id parameter.

To relate a specific item to another content item that has a related data field, execute a POST request to the following endpoint:

{baseurl}/api/default/{entity}({itemToAddRelatedContentToID})/{relatedDataFieldName}/$ref

Parameters

  • {entity} is the entity of the module that has a related data field
  • {itemToAddRelatedContentToID} is the ID of the content item you want to add the relation to
  • {relatedDataFieldName} is the name of the related data field of the entity

Sample request

POST http://mysite.com/api/default/newsitems(6f3e92cf-ab2a-48b9-8c37-f8a1f0dcbb88)/RelatedEvents/$ref

Sample response

No response body

Delete a related item

To delete a related item, you must execute a DELETE request to the same endpoint that you use to access the related data item: /{relatedDataFieldName}({relatedItemId})/$ref.

To delete one of the items related to a content item through a related data field, you must execute a GET request to the following endpoint:

{baseurl}/api/default/{entity}({itemId})/{relatedDataFieldName}({relatedItemID})/$ref

Parameters

  • {entity} is the entity of the module you are working with
  • {itemId} is the ID of the content item whose related item you want to delete
  • {relatedDataFieldName} is the name of the related data field
  • {relatedItemID} is the ID of the related item that you want to delete

Sample request

DELETE http://mysite.com/api/default/newsitems(6f3e92cf-ab2a-48b9-8c37-f8a1f0dcbb88)/RelatedEvents(a88a0475-0e4f-4f8d-bd2b-688fa8d997e2)/$ref

Sample response

No response body

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Web Security for Sitefinity Administrators

The free standalone Web Security lesson teaches administrators how to protect your websites and Sitefinity instance from external threats. Learn to configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?

Next article

Taxonomies operations