Was this article helpful?
Thanks for your feedback
This guide will help you get started with Contentstack .NET Management SDK (that uses Content Management APIs) to manage apps powered by Contentstack.
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));
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.
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));
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 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'");
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));
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");
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));
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();
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);
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 feedback