Skip to content

Instantly share code, notes, and snippets.

@OllyHodgson
Last active November 27, 2018 15:26
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 OllyHodgson/af813b8133302d09668172b63407a594 to your computer and use it in GitHub Desktop.
Save OllyHodgson/af813b8133302d09668172b63407a594 to your computer and use it in GitHub Desktop.
Load data from the DNN Evoq Liquid Content API using the DNN Razor Host module. Razor gives you a bit more control and flexibility than the built-in visualizers.
@using System
@using System.Net
@using System.Text
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@{
// The API URL and your API Key
string url = "https://dnnapi.com/content/api/ContentItems?contentTypeId=7f0d48be-a6bf-4f3c-9fdf-ba895727e818";
string apikey = "**** INSERT YOUR API KEY HERE ****";
// Declare the result variables
string lcjson = String.Empty;
string err = String.Empty;
JObject lcObject = null;
JArray items = null;
IEnumerable<Newtonsoft.Json.Linq.JToken> jobs = null;
// First, we'll set up the http request
var myWebRequest = WebRequest.Create(url);
var myHttpWebRequest = (HttpWebRequest)myWebRequest;
myHttpWebRequest.Method = "GET";
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Headers.Add("Authorization", "Bearer " + apikey);
myHttpWebRequest.Accept = "application/json";
// Get the JSON response from the API
try
{
var myWebResponse = myHttpWebRequest.GetResponse();
var responseStream = myWebResponse.GetResponseStream();
if (responseStream != null) {
var myStreamReader = new StreamReader(responseStream, Encoding.Default);
lcjson = myStreamReader.ReadToEnd();
}
responseStream.Close();
myWebResponse.Close();
}
catch(Exception ex)
{
err = ex.ToString();
}
// Parse JSON into an object
if (lcjson != String.Empty)
{
lcObject = JObject.Parse(lcjson);
items = (JArray)lcObject["documents"];
jobs =
from j in items
select j["details"];
}
}
<div class="container">
<div class="row">
<!-- If we have an error, tell the user what went wrong -->
@if (err != String.Empty)
{
<div class="col mb-4 bump bump-primary">@err</div>
}
<!-- List of jobs -->
<div class="col-12 mb-4">
<h2>@jobs.Count() vacancies in the system</h2>
</div>
@foreach (var x in jobs)
{
<div class="col mb-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">
@x["jobTitle"]
<em>at</em>
@x["firmName"]
</h5>
<p class="card-text">
@x["jobIntroduction"]
</p>
</div>
</div>
</div>
}
<!-- Here's the JSON data from the Liquid Content API -->
<div class="col mb-4">
<div class="bump bump-primary">
@ObjectInfo.Print(items)
</div>
</div>
<!-- Don't forget we have full access to the DNN API here -->
<div class="col mb-4">
<div class="bump bump-primary">
@ObjectInfo.Print(Dnn)
</div>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment