Get Started with .NET SDK

This guide will help you get started with Contentstack .NET SDK to build .NET apps powered by Contentstack.

Prerequisites

  • .NET version 2.0 or later

SDK Installation and Setup

Contentstack offers two regions (US and European) as data centers to store customers' account details and data. Both regions are independent of each other and therefore have a dedicated set of instructions to use SDKs offered by Contentstack.

To use SDKs for the European region, you will have to make certain changes in the configuration of the SDK, as detailed below, and the rest of the instructions remain the same.

Open the terminal and install the contentstack module via “Package Manager” command:

PM> Install-Package contentstack.csharp

And via “.Net CLI”

dotnet add package contentstack.csharp

To use the module in your application, you need to first add Namespace to your class

using Contentstack.Core; // ContentstackClient
using Contentstack.Core.Models; // Stack, Query, Entry, Asset, ContentType
using Contentstack.Core.Configuration; // ContentstackOptions

Initialize SDK

You will need to specify the stack’s API Key, delivery token, and name of the environment where you have published your content.

// Initialize the Contentstack
ContentstackClient stack = new ContentstackClient("stack_api_key", "delivery_token", "environment_name");

OR

var options = new ContentstackOptions() {
    ApiKey = "<stack_api_key>",
    AccessToken = "<delivery_token>",
    Environment = "<environment_name>"
}
ContentstackClient stack = new ContentstackClient(options);

For Setting the European Region:

Refer the below code if you want to use European region.

ContentstackOptions options = new ContentstackOptions() {
    ApiKey = "<stack_api_key>",
    AccessToken = "<delivery_token>",
    Environment = "<environment_name>",
    Region = ContentstackRegion.EU
}
ContentstackClient stack = new ContentstackClient(options);

Once you have initialized the SDK, you can start getting content in your app.

Basic Queries

Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.

Get a Single Entry

To retrieve a single entry from a content type, use the code snippet given below:

Entry entry = client.ContentType("blog").Entry("blta464e9fbd048668c");
entry.Fetch<Blog>().ContinueWith((t) => { 
    if (!t.IsFaulted) { 
        Console.WriteLine("entry:" + t.Result);  
    } 
});

Get Multiple Entries

To retrieve multiple entries of a particular content type, use the code snippet given below:

Query query = client.ContentType("blog").Query(); 
query.Where("title", "welcome"); 
query.IncludeSchema(); 
query.IncludeCount(); 
query.Find<Blog>().ContinueWith((t) => { 
    if (!t.IsFaulted) { 
         ContentstackCollection<Blog> result = t.Result; 
         Console.WriteLine("result" + result); 
    } 
});

These were examples of some of the basic queries of the SDK. For advanced queries, refer to the Contentstack .NET SDK API reference.

Note: Currently, the .NET SDK does not support multiple content types referencing in a single query. For more information on how to query entries and assets, refer the Queries section of our Content Delivery API documentation.

Paginating Responses

In a single instance, the Get Multiple Entries query will retrieve only the first 100 items of the specified content type. You can paginate and retrieve the rest of the items in batches using the skip and limit parameters in subsequent requests.

Query query = client.ContentType("blog").Query();
query.Skip(20);
query.Limit(20); 
query.Find<Blog>().ContinueWith((t) => { 
    if (!t.IsFaulted) { 
         ContentstackCollection<Blog> result = t.Result; 
         Console.WriteLine("result" + result); 
    } 
});

Limitations

  • We have a URL size limitation of 8KB on API Requests that hit our CDN services. Any Request URL that goes above this size limit will receive the 400 - Bad request error response. Please make sure you limit the size of your API Requests.
  • The .NET SDK does not support multiple content types referencing in a single query.
  • Currently, the .NET SDK does not support retrieving details of Global Fields.

More Resources

Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page

top-arrow