AssetLibrary
AssetLibrary
In Contentstack, any files (images, videos, PDFs, audio files, and so on) that you upload get stored in your repository for future use. This repository of uploaded files is called Assets.
sort
The sort method sorts the asset library based on order criteria.
| Name | Type | Description |
|---|---|---|
| keyOrderBy (required) | String | the key order by |
| orderby (required) | ORDERBY | the orderby can be applied using ORDERBY enums |
import com.contentstack.sdk.*; Stack stack = Contentstack.stack( apiKey, deliveryToken, environment); AssetLibrary assets = stack.assetLibrary() assets.sort(keyOrderBy, ORDERBY.ASCENDING);
includeCount
The includeCount method includes the total count of assets in the asset library response.
import com.contentstack.sdk.*; Stack stack = Contentstack.stack( apiKey, deliveryToken, environment); AssetLibrary assets = stack.assetLibrary() assets.includeCount();
includeRelativeUrl
The includeRelativeUrl method includes the relative URLs of assets in the asset library response.
import com.contentstack.sdk.*; Stack stack = Contentstack.stack( apiKey, deliveryToken, environment); AssetLibrary assets = stack.assetLibrary() assets.includeRelativeUrl();
includeMetadata
Includes asset metadata along with response body.
Stack stack = Contentstack.stack("apiKey", "deliveryToken", environment_name);
AssetLibrary assets = stack.assetLibrary();
assets.includeMetadata();where
The where() method retrieves the assets from the stack using any other field UID of the assets.
| Name | Type | Description |
|---|---|---|
| field_name (required) | String | Field UID of the Asset |
| Value (required) | String | Value of the Asset |
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack(apiKey, deliveryToken, environment);
AssetLibrary assets = stack.assetLibrary().where("field_name","value");
assets.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<asset> assets, Error error) {
System.out.println(assets);
} });addParam
The addParam method adds a query parameter to the query.
| Name | Type | Description |
|---|---|---|
| paramKey (required) | String | Pass the key |
| paramValue (required) | Object | Pass the value |
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
AssetLibrary assetLibObject = stack.assetlibrary();
assetLibObject.addParam(paramKey, paramValue);removeParam
The removeParam method removes a query parameter from the query.
| Name | Type | Description |
|---|---|---|
| paramKey (required) | String | Pass the key to be removed as a param |
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
AssetLibrary assetLibObject = stack.assetlibrary();
assetLibObject.removeParam(paramKey);limit
The limit method will return a specific number of assets in the output.
| Name | Type | Description |
|---|---|---|
| number (required) | init | Enter the number of assets to be returned. |
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
AssetLibrary assetLibObject = stack.assetlibrary().limit(4);skip
The skip method will skip a specific number of assets in the output.
| Name | Type | Description |
|---|---|---|
| number (required) | init | Enter the number of assets to be skipped. |
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
AssetLibrary assetLibObject = stack.assetlibrary().skip(4);Pagination
In a single instance, a query will retrieve only the first 100 items in the response. You can paginate and retrieve the rest of the items in batches using the skip and limit methods.
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
AssetLibrary assetLibObject = stack.assetlibrary();
int skip = 0, limit = 100;
final int[] totalAssetsFetched = {0};
final boolean[] hasMore = {true};
while (hasMore[0]) {
assetLibrary.skip(skip).limit(limit).fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error != null) {
System.err.println("Error: " + error.getErrorMessage());
return;
}
for (Asset asset : assets) {
System.out.println(asset.toJSON());
}
totalAssetsFetched[0] += assets.size();
}
});
skip += limit;
System.out.println("Total assets fetched: " + totalAssetsFetched[0]);
if (totalAssetsFetched[0] % limit != 0) {
hasMore[0] = false;
}
}setLocale
The setLocale method sets the locale used for asset queries, so results are returned for a specific language or region.
| Name | Type | Description |
|---|---|---|
| locale | String | Locale code for the query (for example, "en-us"). Used in requests built with fetchAll() to determine which localized asset data is returned. |
This is an instance of method chaining. You can chain it with other AssetLibrary methods (for example, setLocale("en-us").includeCount().fetchAll(...)).
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack("API_KEY", "DELIVERY_TOKEN", "ENVIRNOMENT");
AssetLibrary assets = stack.assetLibrary();
assets.setLocale("en-us");
assets.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error != null) {
System.err.println("Error: " + error.getErrorMessage());
return;
}
// use assets
}
});