Get Started with .NET Management SDK

This guide will help you get started with Contentstack .NET Management SDK (that uses Content Management APIs) to manage apps powered by Contentstack. 

Prerequisite

  • .NET version 3.1 or later

SDK Installation and Setup

Contentstack offers three regions (North America, Europe, and Azure North America) as data centers to store customers' account details and data.

Open the terminal and install the Contentstack module via the “Package Manager” command.

PM> Install-Package contentstack.management.core

To install via “.NET CLI”, use the following command:

dotnet add package contentstack.management.core

To import the SDK, use the following command:

using Contentstack.Management.Core
ContentstackClient client = new ContentstackClient();

You can also use the following command to import the SDK:

ContentstackClientOptions options = new ContentstackClientOptions();
ContentstackClient client = new ContentstackClient(new
OptionsWrapper<ContentstackClientOptions>(options));

Authentication

To use this SDK, you need to authenticate users. You can do this by using an authtoken, credentials, or a management token (stack-level token). Let's discuss them in detail.

Authtoken

An authtoken is a read-write token used to make authorized CMA requests, and it is a user-specific token.

using Microsoft.Extensions.Options;
ContentstackClientOptions options = new ContentstackClientOptions() {
    Authtoken: 'AUTHTOKEN'
};
ContentstackClient client = new ContentstackClient(new OptionsWrapper<ContentstackClientOptions>(options));

Login

To log in to Contentstack, provide your credentials as follows:

using System.Net;


NetworkCredential credentials = new NetworkCredential("EMAIL", "PASSWORD");
ContentstackClient client = new ContentstackClient();

try
{
    ContentstackResponse contentstackResponse = client.Login(credentials);
} catch (Exception e)
{

}

Management Token

Management tokens are stack-level tokens with no users attached to them.

using Contentstack.Management.Core

ContentstackClient client = new ContentstackClient();
client.Stack("API_KEY", "'MANAGEMENT_TOKEN'");

Initialize your SDK

To use the .NET Management SDK, you need to first initialize it.

using Contentstack.Management.Core
ContentstackClientOptions options = new ContentstackClientOptions() {
    Authtoken: 'AUTHTOKEN'
};
ContentstackClient client = new ContentstackClient(new OptionsWrapper<ContentstackClientOptions>(options));

For Setting the branch:

If you want to initialize the SDK in a particular branch, use the following code:

using Contentstack.Management.Core

ContentstackClient client = new ContentstackClient();
client.Stack("API_KEY", "MANAGEMENT_TOKEN", "BRANCH");

Proxy Configuration

Contentstack allows you to define HTTP proxy for your requests with the .NET Management SDK. A proxied request allows you to anonymously access public URLs even from within a corporate firewall through a proxy server.

Here is the basic syntax of the proxy settings that you can pass within fetchOptions of the .NET Management SDK:

using System.Net;
var contentstackConfig = new ContentstackClientOptions();
contentstackConfig.ProxyHost = "http://127.0.0.1";
contentstackConfig.ProxyPort = 9000;
contentstackConfig.ProxyCredentials = new NetworkCredential(userName: "username", password: "password");
ContentstackClient client = new ContentstackClient(new OptionsWrapper<ContentstackClientOptions>(options));

Fetch Stack Details

To fetch your stack details through the SDK, use the following code:

using Contentstack.Management.Core

ContentstackClient client = new ContentstackClient();
Stack stack = client.Stack("'API_KEY'");
ContentstackResponse contentstackResponse = stack.Fetch();
var response = contentstackResponse.OpenJObjectResponse();

Create an Entry

You can use the following code to create an entry in a specific content type of a stack through the SDK:

EntryModel entry = new EntryModel() {
   Title: 'Sample Entry',
   Url: '/sampleEntry'
}
ContentstackClient client = new ContentstackClient();
Stack stack = client.Stack("'API_KEY'");
ContentstackResponse contentstackResponse = stack.ContentType("CONTENT_TYPE_UID").Entry().Create(entry);

Upload Assets

Use the following code snippet to upload assets to your stack through the SDK:

ContentstackClient client = new ContentstackClient();
Stack stack = client.Stack("'API_KEY'");

var path = Path.Combine(Environment.CurrentDirectory, "path/to/file");
AssetModel asset = new AssetModel("'Asset Title", path, "application/json");
ContentstackResponse response = stack.Asset().Create(asset);

Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page

^