Skip to content

Instantly share code, notes, and snippets.

@deanebarker
Created May 3, 2022 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanebarker/c6565f591bdcdaa27d9ffe4f0cc10156 to your computer and use it in GitHub Desktop.
Save deanebarker/c6565f591bdcdaa27d9ffe4f0cc10156 to your computer and use it in GitHub Desktop.
The simplest webhook system for Optimizely Content Cloud
public void Configuration(IAppBuilder app)
{
// This is the URL template to call
// The placeholders are optional -- they will be replaced with data specific to the content that is published
var webhookUrl = "https://example.com?id={id}&type={type}&version={version}";
// Here is an example URL that would be called for this example:
// https://example.com?id=17&type=ArticlePage&version=691
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.PublishedContent += (s, e) =>
{
// Get the type name
var typeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var typeName = typeRepository.Load(e.Content.ContentTypeID);
// Form the specific URL for *this* webhook call
var url = webhookUrl
.Replace("{id}", e.ContentLink.ID.ToString())
.Replace("{type}", typeName.Name)
.Replace("{version}", e.ContentLink.WorkID.ToString());
// Call it in a new thread
// Note: errors will be swallowed; implement logging as you see fit
Task.Run(() =>
{
var webClient = new WebClient();
webClient.DownloadStringAsync(new Uri(url));
});
};
// More startup code here...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment