Giter Club home page Giter Club logo

contentful.net's Introduction

build status

contentful.net

https://www.contentful.com is a content management platform for web applications, mobile apps and connected devices. It allows you to create, edit & manage content in the cloud and publish it anywhere via powerful API. Contentful offers tools for managing editorial teams and enabling cooperation between organizations.

This is the .NET SDK for Contentful's Content Delivery and Content Management APIs.

Setup

We recommend you use the NuGet package manager to add Contentful to your .Net application using the following command in your NuGet package manager console.

Install-Package contentful.csharp -prerelease

Usage

The ContentfulClient handles all communication with the Contentful Content Delivery API.

To create a new client you need to pass an HttpClient, your delivery API key and any other configuration options:

var httpClient = new HttpClient();
var client = new ContentfulClient(httpClient, "<content_delivery_api_key>", "<space_id>");

or:

var httpClient = new HttpClient();
var options = new ContentfulOptions()
{
    DeliveryApiKey = "<content_delivery_api_key>",
    SpaceId = "<space_id>"
}
var client = new ContentfulClient(httpClient, options);

If you are running asp.net core and wish to take advantage of the options pattern you can do so by passing an IOptions<ContentfulOptions> to the constructor. This lets you keep your authorization token in your application settings, in environment variables or your own custom Microsoft.Extensions.Configuration.IConfigurationSource provider.

Querying for content

After creating a ContentfulClient, you can now query for a single entry:

var entry = await client.GetEntryAsync<Entry<dynamic>>("<entry_id>");

Console.WriteLine(entry.Fields.productName.ToString()); // => Contentful

Normally you serialize this response into your own class instead of the generic Entry<> type. You can do this by providing a suitable type to seralize into. Take the following class as an example:

public class Product {
    public string ProductName { get; set; }
    public string Price { get; set; }
    public string Description { get; set; }
}

Pass this class to the GetEntryAsync<> method to serialize the response correctly.

var product = await client.GetEntryAsync<Product>("<entry_id>");

Console.WriteLine(product.ProductName); // => Your product
Console.WriteLine(product.Price); // => 12.38
Console.WriteLine(product.Description); // => A fantastic product.

You can combine the two approaches if you're interested in the system properties of the entry but still want to use your own class.

var productEntry = await client.GetEntryAsync<Entry<product>>("<entry_id>");

Console.WriteLine(entry.Fields.Price); // => 12.38
Console.WriteLine(entry.SystemProperties.Id); // => 2CfTFQGwogugS6QcOuwO6q

Management API

To edit, update and delete content you use the ContentfulManagementClient class which uses the same familiar pattern as the regular client.

var httpClient = new HttpClient();
var managementClient = new ContentfulManagementClient(httpClient, "<content_management_api_key>", "<space_id>");

You can then use the client to, for example, create a content type.

var contentType = new ContentType();
contentType.SystemProperties = new SystemProperties() {
    Id = "new-content-type"
};
contentType.Name = "New contenttype";
contentType.Fields = new List<Field>()
{
    new Field()
    {
        Name = "Field1",
        Id = "field1",
        Type = "Text"
    },
    new Field()
    {
        Name = "Field2",
        Id = "field2",
        Type = "Integer"
    }
};


await managementClient.CreateOrUpdateContentTypeAsync(contentType);

Further information

You can read the full documentation and explore the api at https://contentful.github.io/contentful.net-docs/

contentful.net's People

Contributors

akzent avatar mariobodemann avatar matt-hammond avatar roblinde avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.