Using the ItemService from JavaScript
The ItemService is a standalone XHR (XMLHttpRequest) library for creating, fetching, saving, and deleting Sitecore items. It has many built-in utilities and helpers to assist you with data transactions between the front end and the back end. The library is /sitecore/shell/client/Services/Assets/lib/itemService.js.
Many of the examples use functionality from Unit.js. If you want to run the examples literally, you must add a reference to Unit.js in your code.
This topic describes the following:
Creating items
Creating items
You create an item by passing an object and the path in the Sitecore tree where you want the server to create the item, and then calling the execute method:
You create a dirty item by not calling the execute method:
Fetching items
Fetching items
You fetch an item by ItemID with the fetchItem() method:
Fetching the children of an item
Fetching the children of an item
You fetch the children of an item with the fetchChildren() method:
Saving items
Saving items
You must either fetch or create an item before you can save it:
Destroying items
Destroying items
You must either fetch or create an item before you can destroy it:
Searching for items
Searching for items
You can search for an item in two ways: you can use search or you can use query.
Using search()
You search for an item by passing a search term and then calling the execute method:
Using query()
You query for an item by passing a query item and then calling the execute method:
“xxxx-xxxx-xxxx-xxxx” is the Sitecore ID of the query item.
Creating a dirty item
Creating a dirty item
A dirty item is an item that is only in browser memory and not saved on the server. This can be useful if your application has to wait for user input. Use the following code to create a dirty item:
Checking if an item is dirty
Checking if an item is dirty
You can check if an item has not been saved (it is a “dirty item") with the isNew property:
When an item comes from the server, isNew is always false:
Retrieving an item as raw JSON
Retrieving an item as raw JSON
You may need to retrieve the data of an item as raw JSON (without all of the additional methods and properties). Use the following code to retrieve an item as raw JSON:
Tracking items
Tracking items
You can add tracking to an item. This makes it possible to add additional functionality:
-
The item can be saved automatically when a property changes.
-
You can call hasChanged() to see if the item has changed.
-
You can call revertChanges() to revert property values.
You set the trackable option to true to add tracking to your item:
Saving items automatically
Saving items automatically
You can specify that Sitecore.Services.Client saves changes to an item automatically. Set trackable to true to turn this feature on. You can listen to the save event by using the emitter “on” or “once”:
The hasChanged() method
The hasChanged() method
You can specify that the ItemService adds the hasChanged() method by setting trackable to true. You use the hasChanged() method to check if an item has changed:
The hasChanged method does not return true if a value changes between null, undefined, and ‘’ (the empty string):
The revertChanges() method
The revertChanges() method
You can specify that the ItemService add the revertChanges() method by setting trackable to true. You use the revertChanges() method to revert changes to the item:
The Query object
The Query object
Each ItemService method returns a query object:
Handling promises
Handling promises
Almost all asynchronous calls return a promise. Specifically, and prominently, the execute() method returns a promise. The ItemService uses the q module to handle this.
Working with middleware
Working with middleware
The ItemService provides middleware functionality, using the sc-useify module:
When you have integrated middleware, then the next time you receive data from the server, this data will have a timestamp property:
You can clear all middleware workers (or clear by key):
Because you cleared all middleware, the data will not have a timestamp property the next time you receive it from the server:
Working with event emitters
Working with event emitters
You can extend items with event emitters:
ItemService and validation
ItemService and validation
The ItemService does not provide any client-side validation. The following example changes the ID of the item to an invalid GUID and then triggers a save. The client will allow the save to execute because there is no validation. However, when the server receives the request with invalid data, it will not resolve and the promise will fail: