Contentstack DotNet SDK: Implementation and Guidelines

Shailesh Mishra
4 min readJul 25, 2023

The following documentation describes the methods and their usage for performing entry operations using the Contentstack SDK. The Contentstack SDK is a powerful tool that allows you to interact with the Contentstack Content Management System (CMS) and perform various operations on entries.

Below is the step-by-step documentation for the provided code:

  • Step 1: Import Required Libraries

Before running the code, make sure you have the necessary libraries referenced. In this case, the code uses Contentstack Management SDK, and import below to start using contentstack services:

`System.Net`, `Contentstack.Management.Core`, and `Contentstack.Management.Core.Models` namespaces. These libraries are required for making API calls and managing Contentstack assets.

  • Step 2: Set Your Contentstack Credentials

Replace the placeholder values with your actual Contentstack credentials. You need to provide your email address, password, and API key to authenticate with Contentstack.

string EMAIL = "your_email@example.com";
string PASSWORD = "your_password";
string API_KEY = "your_api_key";
  • Step 3: Create a Contentstack Client Instance

Create an instance of the `ContentstackClient` class, which will be used to interact with the Contentstack API.

ContentstackClient client = new ContentstackClient();
  • Step 4: Log in Using Credentials

Authenticate yourself with Contentstack using your email and password by creating a `NetworkCredential` object and passing it to the `Login` method of the `ContentstackClient`.

NetworkCredential credentials = new NetworkCredential(EMAIL, PASSWORD);
client.Login(credentials);
  • Step 5: Get Access to the Stack

Obtain the stack instance using the client object and your API key.

Stack stack = client.Stack(API_KEY);
  • Step 6: Create an Asset

To create an asset (like an image) in Contentstack, you need to provide some essential information about the asset. This code creates an AssetModel object and sets the required data, such as the asset’s name, file path, and content type.

AssetModel model = new AssetModel("shailesh", "/Users/shailesh.mishra/Projects/dotnet/TeseAsset/TeseAsset/apple.svg", "multipart/form-data");
  • Step 7: Perform Asset Creation

Use the stack instance to create the asset by calling the `Create` method on the asset with the provided model.

AssetModel model = new AssetModel("shailesh", "/Users/shailesh.mishra/Projects/dotnet/TeseAsset/TeseAsset/apple.svg", "multipart/form-data");
ContentstackResponse response = stack.Asset().Create(model);
Console.WriteLine(response.ResponseBody);
Photo by Caspar Camille Rubin on Unsplash

Entry Operations: Fetch, Create, Update, and Delete an Entry

Fetching an Entry

// Fetch An Entry
ContentstackResponse fetchEntry = stack.ContentType("CONTENT_TYPE_UID").Entry().Fetch();

Description:
This method allows you to fetch a single entry from a specific content type in your Contentstack CMS.

Parameters:
CONTENT_TYPE_UID: Replace this with the unique identifier (UID) of the content type from which you want to fetch the entry.

Return Value:
The `ContentstackResponse` object contains the response of the API call, including the entry data if the fetch operation is successful.

Usage Example:

ContentstackResponse fetchEntryResponse = stack.ContentType("blog_post").Entry().Fetch();
if (fetchEntryResponse.isSuccess()) {
// Fetch successful, retrieve entry data
Entry entry = fetchEntryResponse.entry();
// Process the entry data
} else {
// Handle error
}

Creating an Entry

// Create An Entry
ContentstackResponse createEntry = stack.ContentType("CONTENT_TYPE_UID").Entry().Create(entry);

Description:
This method allows you to create a new entry within a specific content type in your Contentstack CMS.

Parameters:
CONTENT_TYPE_UID : Replace this with the unique identifier (UID) of the content type in which you want to create the entry.
entry : Replace this with the JSON object containing the entry’s field values.

Return Value:
The `ContentstackResponse` object contains the response of the API call, including the created entry data if the operation is successful.

Usage Example:

JSONObject entryJson = new JSONObject();
entryJson.put("title", "New Blog Post");
entryJson.put("content", "This is the content of the blog post.");
ContentstackResponse createEntryResponse = stack.ContentType("blog_post").Entry().Create(entryJson);
if (createEntryResponse.isSuccess()) {
// Entry created successfully, retrieve entry data
Entry createdEntry = createEntryResponse.entry();
// Process the created entry data
} else {
// Handle error
}

Updating an Entry

// Update An Entry
ContentstackResponse updateEntry = stack.ContentType("CONTENT_TYPE_UID").Entry().Update(entry);

Description:
This method allows you to update an existing entry within a specific content type in your Contentstack CMS.

Parameters:
CONTENT_TYPE_UID : Replace this with the unique identifier (UID) of the content type to which the entry belongs.
entry: Replace this with the JSON object containing the updated field values for the entry.

Return Value:
The `ContentstackResponse` object contains the response of the API call, including the updated entry data if the operation is successful.

Usage Example:


JSONObject entryJson = new JSONObject();
entryJson.put("title", "Updated Blog Post Title");
entryJson.put("content", "This is the updated content of the blog post.");
ContentstackResponse updateEntryResponse = stack.ContentType("blog_post").Entry().Update(entryJson);
if (updateEntryResponse.isSuccess()) {
// Entry updated successfully, retrieve updated entry data
Entry updatedEntry = updateEntryResponse.entry();
// Process the updated entry data
} else {
// Handle error
}

Deleting an Entry

// Delete An Entry
ContentstackResponse deleteEntry = stack.ContentType("CONTENT_TYPE_UID").Entry().Delete(entry);

Description:
This method allows you to delete an existing entry from a specific content type in your Contentstack CMS.

Parameters:
CONTENT_TYPE_UID: Replace this with the unique identifier (UID) of the content type to which the entry belongs.
entry: Replace this with the JSON object representing the entry you want to delete.

Return Value:
The `ContentstackResponse` object contains the response of the API call, indicating whether the delete operation was successful.

Usage Example:

// Assuming 'entry' is the JSON object representing the entry to be deleted
ContentstackResponse deleteEntryResponse = stack.ContentType("blog_post").Entry().Delete(entry);
if (deleteEntryResponse.isSuccess()) {
// Entry deleted successfully
} else {
// Handle error
}

Note: Replace CONTENT_TYPE_UID with the actual unique identifier of your content type.

For the `entry` parameter, make sure you pass the correct JSON object with field values when creating or updating an entry.

Remember to handle errors appropriately when using these methods to ensure smooth application behavior.

--

--