Using LINQPad to Access Headless CMS

You can use LINQPad (https://www.linqpad.net/) to access headless Content Management Systems with C# code. LINQPad provides convenient visualizers for JSON and images. You can use LINQPad for scripting, and both visualized and raw access the JSON can be useful when implementing Entry Model classes (see https://deliverystack.wordpress.com/2020/07/20/net-core-headless-cms-entry-models-and-entry-model-classes/), diagnosing issues, and otherwise.

The code in the following image uses the Contentstack .NET SDK (https://www.contentstack.com/docs/platforms/dot-net/api-reference/api/index.html, AKA https://www.nuget.org/packages/contentstack.csharp, also at https://github.com/contentstack/contentstack-dotnet) to retrieve an Entry (https://www.contentstack.com/docs/developers/apis/content-delivery-api/#get-a-single-entry, https://www.contentstack.com/docs/platforms/dot-net/api-reference/api/Contentstack.Core.Models.Entry.html, https://github.com/contentstack/contentstack-dotnet/blob/master/Contentstack.Core/Models/Entry.cs) from the CMS and render a server-side manipulated Asset (https://www.contentstack.com/docs/developers/apis/image-delivery-api/ ) referenced by that Entry.

LINQPad accessing the Contentstack Headless CMS.

To achieve this in LINQPad:

  1. Download the Contentstack.Csharp (https://www.nuget.org/packages/contentstack.csharp/), Newtonsoft.Json (https://www.nuget.org/packages/Newtonsoft.Json), andMicrosoft.Extensions.Options (https://www.nuget.org/packages/Microsoft.Extensions.Options) NuGet packages.
  2. Use a compression tool such as 7-Zip (https://www.7-zip.org/) to extract the /contentstack.csharp.2.3.0.nupkg/lib/netstandard2.0/Contentstack.Core.dll, /newtonsoft.json.12.0.3.nupkg/lib/netstandard2.0/Newtonsoft.Json.dll, and /microsoft.extensions.options.3.1.6.nupkg/lib/netstandard2.0/Microsoft.Extensions.Options.dll assemblies from the NuGet packages to a temporary directory such as C:\temp.
  3. In LinqPad, press F4 or select the Query menu and then select References and Properties to open the Query Properties dialog.
  4. On the Additional References tab, click the Add / Browse button and add the two libraries.
  5. On the Namespace Imports tab, add Contentstack.Core, Contentstack.Core.Models, and Newtonsoft.Json.Linq namespaces.

Additional References tab:

LNIQPad referencing assemblies.

Imports tab:

LINQPad importing namespaces.

In LINQPad, you do not need a namespace, class, or Main() method, only the lines of code to execute. Look for output on the Results tab.

Here is some sample code to access an existing Entry and an Asset in an existing Environment in an existing Stack with an existing Content Delivery Token in the Contentstack SaaS headless CMS:

ContentstackClient stack = new ContentstackClient(
    "blt02f7b45378b008ee",
    "cs5b69faf35efdebd91d08bcf4",
    "production");
Entry entry = stack.ContentType("product").Entry(
    "blta250054cfa4f5aab").Fetch<Entry>().GetAwaiter().GetResult();
JObject entryJson = entry.ToJson();

foreach (var image in entryJson.SelectTokens("$.images").Children())
{
    string url = image["url"] + "?w=25p";
    Console.WriteLine(url);
    Console.WriteLine(Util.Image(url));
}

Console.WriteLine(entryJson);

Of course, you would normally use Entry Models (https://deliverystack.wordpress.com/2020/07/20/net-core-headless-cms-entry-models-and-entry-model-classes/) to model Entries and to access Asset metadata including URLs rather than accessing the processing the raw JSON directly.

One thought on “Using LINQPad to Access Headless CMS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: