Using the EntityService from JavaScript
The EntityService is a standalone XHR (XMLHttpRequest) library for creating, fetching, saving, and deleting Sitecore entities. The library is /sitecore/shell/client/Services/Assets/lib/entityservice.js.
This topic describes:
Overview
Overview
The EntityService has many utilities and helpers that assist in the data transaction between the front end and back end. For example:
The structure of an entity object is based on metadata (a schema). The EntityService requests this metadata only once from the server, using the OPTIONS request.
This example shows how you instantiate the EntityService and create an entity:
-
An
EntityServiceis instantiated. -
When the
EntityServiceis asked to execute any server related request (createEntity, fetchEntity, and so on), the supplied URL is accessed with anOPTIONSrequest. All subsequent requests are queued until the server responds with a valid metadata object. -
After the metadata object is returned, it is attached to the context
EntityServiceand all entities can be validated and sanitized based on this metadata.The following code examples are based on this metadata:
The EntityService.Entity
The EntityService.Entity
Entity represents an Entity client side
Constructor
EntityService.Entity ( sanitizedData, entityServiceSchema, options )
Parameters
-
sanitizedData: object. An object that meets has been validated by the schema
-
entityServiceSchema: object. The entity schema
-
options: object. The entity options
Returns
Entity: an entity
Creating entities
Creating entities
You create an entity by passing an object, setting the path in the Sitecore content tree where you want the item to be created, and calling the execute method:
You create a dirty entity without a call to the server by not giving an object to the createEntity method. All dirty entities are considered new. You can check this with the isNew property.
Fetching a single entity
Fetching a single entity
You fetch a single entity with the fetchEntity method, giving the entity id/guid. fetchEntity returns a query so you need to call execute.
Fetching multiple entities
Fetching multiple entities
You fetch all entities based on the given URL with the fetchEntities method.
Saving entities
Saving entities
You must fetch or create an entity before you can save it.
Destroying entities
Destroying entities
You must fetch or create an entity before you can destroy it.
Dirty entities
Dirty entities
A dirty entity is an entity you create in browser memory without saving it. This is useful, for example, if you are waiting for user input.
Checking if an entity is dirty
You check if an entity has not been saved (it is “dirty”) with the isNew property:
Retrieving raw JSON
Retrieving raw JSON
There are situations where you need to retrieve the data stored in an entity as raw JSON without any of the additional methods and properties. The following method is used internally to retrieve the data that is sent to the server:
Tracking entities
Tracking entities
You can extend an entity with tracking to add additional functionality, for example:
-
Automatically save when a property changes.
-
Call hasChanged() to see if the entity has changed.
-
Call revertChanges() to revert property values.
Add tracking to your entity with the trackable option:
Saving automatically
Saving automatically
When you turn trackable on, changes are automatically saved. You can listen to the save event using emitter on or once.
Checking if an entity has changed
Checking if an entity has changed
When you turn trackable on, Sitecore adds the hasChanged method so you can check if an entity has changed:
The hasChanged method does not return true if a value changes between null, undefined, and '' the empty string).
Reverting changes
Reverting changes
When you turn trackable on, Sitecore adds the revertChanges method, so you can revert changes.