cs-icon.svg

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 four regions (North America, Europe, Azure North America, and Azure Europe) 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&lt;ContentstackClientOptions&gt;(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 as follows:

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&lt;ContentstackClientOptions&gt;(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

To create an entry, you need to prepare a custom model class that represents the entry's request body. To do so, create a separate EntryModel.cs file and add your custom EntryModel class, implementing IEntry interface, as follows:

using Contentstack.Management.Core.Abstractions;
using Newtonsoft.Json;
namespace TestModels
{
	public class EntryModel : IEntry
    {
		public EntryModel()
		{
		}
        [JsonProperty(propertyName: "title")]
        public string Title { get; set; }
        [JsonProperty(propertyName: "url")]
        public string URL { get; set; }
        [JsonProperty(propertyName: "uid")]
        public string Uid { get; set; }
    }
}

Next, you need to set the data to the model. Here’s how you can do that:

EntryModel entryModel = new EntryModel()
{
    Title = "Your Entry Title",
    URL = "path/yoururl.com/example",
};

The code below illustrates how to create an entry:

using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
EntryModel model = new EntryModel();
ContentstackResponse contentstackResponse = client.Stack("<API_KEY>").ContentType("<CONTENT_TYPE_UID>").Entry().Create(model);

Upload Assets

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

using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
AssetModel model = new AssetModel("ASSET_NAME", "FILE_PATH", "FILE_CONTENT_TYPE");
ContentstackResponse contentstackResponse = client.Stack("<API_KEY>").Asset().Create(model);
Was this article helpful?
^