Build a news app using Contentstack’s .NET SDK

This demo news app is built using Contentstack’s .NET SDK. It uses Contentstack to store and deliver the content of the news app.

Screenshots

Quickstart

Here’s a quick guide on how to create a news app for .NET using .NET SDK and Contentstack.

Prerequisites

Note: For this tutorial, we have assumed that you are familiar with Contentstack. If not, then please refer to the docs (Contentstack docs) for more details.

In this tutorial, we will first go through the steps involved in configuring Contentstack, and then look at the steps required to customize and use the presentation layer.

Step 1: Create a stack

Log in to your Contentstack account, and create a new stack. This stack will hold all the data, specific to your website. Learn more on how to create a stack.

Step 2: Add a publishing environment

To add an environment in Contentstack, navigate to ‘Settings' -> 'Environment', and click on the '+ New Environment’ tab. Provide a suitable name for your environment, say ‘staging’. Specify the base URL (e.g., ‘http://YourDomainName.com’), and select the language (e.g., English - United States). Then, click on 'Save'. Read more about Environments.

Step 3: Import content types

For this website, two basic content types are required: ‘Category’ and ‘News’. Read more about Content Types.

For quick integration, we have already created these content types. You can import these content types into your project stack in Contentstack.

Here’s a brief overview of the content types required for this project.

  • News: This content type lets you add the news content into your app.
  • Category: This content type lets you create the various categories of your news app.

Download All Content Types

Now that all the content types are ready, let’s add some content for your news app. 

Step 4: Adding content

Create and publish entries for the ‘News’ and ‘Category’ content types

Add a few dummy entries for news articles for the ‘News’ content type. Save and publish these entries. Learn how to create and publish entries.

With this step, you have created sample data for your website. Now, it’s time to use and configure the presentation layer. 

Step 5: SDK Installation and Setup

To use the .NET SDK, 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

Step 6: Initialize SDK

You will need to specify the API key, Delivery token, and Environment Name of your stack to initialize the SDK. (Learn how to find your Stack's API Key and Delivery Token. Read more about Environments.)

// Initialize the Contentstack Stack
ContentstackOptions contentstackOptions = new ContentstackOptions {
    ApiKey = "api_key",
    AccessToken = "delivery_token",
    Environment = "environment_name"
};
ContentstackClient contentstackClient = new ContentstackClient(contentstackOptions);

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

Step 7: Configure the application

To help you get started, we have created a sample application that is powered by Contentstack .NET SDK.

Note: Add your Contentstack API Key, Delivery Token, and Environment to the project during the SDK initialization step:

using System;
using System.Linq;
using Contentstack.Core;
using Contentstack.Core.Models;
using Contentstack.Core.Configuration;
namespace SampleConsoleApp {
  class Program {
    static void Main(string[] args) {
      ContentstackOptions contentstackOptions = new ContentstackOptions {
        ApiKey = "API_Key",
        AccessToken = "Delivery_Token",
        Environment = "Environment_Name"
      };
      ContentstackClient contentstackClient = new ContentstackClient(contentstackOptions);
      Query query = contentstackClient.ContentType("news").Query();
      query.IncludeReference("category");
      query.Find().ContinueWith((t) => {
        if (t.IsFaulted) {
          // Handle error
          var exception = t.Exception.Flatten().InnerException;
          Console.WriteLine("Error Message : ", exception.Message);
        } else {
          // Get all the entries
          String newsTitle = string.Empty;
          var listEntries = t.Result.Result;
          for (int i = 0; i < listEntries.Count(); i++) {
            newsTitle = listEntries[i].Title;
            Console.WriteLine("Title : " + newsTitle);
          }
        }
      });
      Console.ReadLine();
    }
  }
}

Result

Here's a screenshot that shows how your output will look like:

DotNet Example Code Result.png

Now that we have a working project, you can fire queries to see how it works.

Was this article helpful?
top-arrow