PRINT 18Meet us at Print 18. Chicago, IL. Sept. 30 - Oct. 2

State Files

Every time you call saveProduct or finishProductDesign, Customer's Canvas creates a state file representing all objects loaded in the editor. By default, these state files are saved in the ~\UserDataFolder\<someUserId>\states\ folder for a single user and they are kept forever until you delete these files manually. Customer's Canvas does not automatically clean up them. At the same time, you can use the Web API to read names of state files, delete them, or reload images obtained from direct URLs.

This StateFiles controller provides the following methods:

Function Request type URL
Get a list of user's state files GET ~/api/users/{userId}/states
Delete a user's state file DELETE ~/api/users/{userId}/states/{stateId}
Reload images in a state file PATCH ~/api/users/{userId}/states/{stateId}

URL Parameters

The StateFiles controller requires the following parameters:

  • userId - a user identifier.
  • stateId - a file name of a state without an extension.

It is recommended to download a code sample written in ECMAScript 2015 that demonstrates how you can work with the API and refer to this sample when reading this topic.

Important

The security model of Customer's Canvas requires you to pass X-CustomersCanvasAPIKey in request headers. The snippet below defines the API security key in JavaScript code. It could be highly insecure if it runs on a public site. However, you can use it this way in your admin panel, or just for demonstration purposes.

Getting a List of User's State Files

The Success Response

For example, you can get the following response for the case when the default user has a state file.

Status Code: 200 OK
Content:
[{
  "stateId":"0777bb82-6f89-4e72-93b1-aad1b875602b",
  "dateModified":"5/26/2018 8:29",
  "surfaceCount":2,
  "tag":null
}]

The Request Example

JavaScript
const userId = "default";
const url = "https://localhost:44300/api/users/${userId}/states";
fetch(url).then(response => {
    return response.json();
}).then(json => {
    for (let i = 0; i < json.length; i++) {
        const s = json[i];
        console.log(s.stateId);
    }
});

Deleting a User's State File

The Success Response

As a successful result, this API can return the following:

  • The state file was not found.
    Status Code: 200 OK
    Content: false
    
  • The state file deleted successfully.
    Status Code: 200 OK
    Content: true
    

The Request Example

JavaScript
const userId = "default";
const stateId = "1f190673-226d-4c31-87b4-0140805cc445";
const url = "https://localhost:44300/api/users/${userId}/states/${stateId}";
fetch(url, {
        method: "DELETE",
        headers: {
            "X-CustomersCanvasAPIKey": "UniqueSecurityKey",
            "Content-Type": "application/json"
        }
    })
    .then(
        response => { return response.json(); },
        e => console.log("Delete state failed")
    )
    .then(json => {
        if (json === true)
            console.log("State deleted successfully");
        else if (json === false)
            console.log("Requested state was not found");
    });

Reloading Images in a State File

You can reload MemorySource images that obtained from external sources through direct URLs. After your users have added images through the Asset Manager, the original files may be converted or transformed. To update these images with the actual content, you can use this Web API. Reloading images invalidates cache for links to previews and hi-res output files, so that the next time they are accessed, they are recreated based on new versions of the original images.

To reload all images in a state file, you should specify the MemorySourceReloadAll type in the request body.

The Request Example

JavaScript
const userId = "default";
const stateId = "1f190673-226d-4c31-87b4-0140805cc445";
const url = "https://localhost:44300/api/users/${userId}/states/${stateId}";
fetch(url, {
    method: "PATCH",
        headers: {
            "X-CustomersCanvasAPIKey": "UniqueSecurityKey",
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            type: "MemorySourceReloadAll"
        })
    })
    .then(
        response => {
            if (response.ok)
                console.log("Images reloaded successfully");
            else
                console.log("Failed to reload images");
        }
    );

See Also

Manual

Downloads