Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Contentful Database Synchronization Controller
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Web.Http;
// Note: for this to work, you need to insert this line in the Application_Start event of your global.asax
// GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/"));
// This tells the model binder that the Contentful webhook request carries a JSON payload in the body
namespace Website.Controllers
public class WebhookController : ApiController
private const string TOPIC_HEADER_NAME = "X-Contentful-Topic";
private const string PUBLISHED_TOPIC_HEADER_VALUE = "ContentManagement.Entry.publish";
private const string INSERTED_RESPONSE = "Inserted/Refreshed";
private const string DELETED_RESPONSE = "Deleted";
private string connectionString = ConfigurationManager.ConnectionStrings["blog"].ToString();
public string Index([FromBody] JObject data)
using (var conn = new SqlConnection(connectionString))
// Always execute a delete
// We don't care if the record actually exists or not
var deleteCommand = new SqlCommand("DELETE FROM posts WHERE id = @id", conn);
deleteCommand.Parameters.AddWithValue("id", (string)data.SelectToken(""));
// The "X-Contentful-Topic" tells us what Contentful was doing.
// We weren't publishing, so we're done
// We're publishing, so insert the entry
// This is either a brand new record or it will replace a record we just deleted
var insertCommand = new SqlCommand("INSERT INTO posts (id, title, body, published) VALUES (@id, @title, @body, @published)", conn);
insertCommand.Parameters.AddWithValue("id", (string)data.SelectToken(""));
insertCommand.Parameters.AddWithValue("title", (string)data.SelectToken("fields.title.en-US"));
insertCommand.Parameters.AddWithValue("body", (string)data.SelectToken("fields.body.en-US"));
insertCommand.Parameters.AddWithValue("published", (DateTime)data.SelectToken(""));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment