HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Quick start with Graph C# SDK

Install and configure the Optimizely Graph C# SDK and run your first strongly typed query against Optimizely Graph.

👍

Early access preview

This content is an early access preview and may change before general availability. Use the thumbs up or down at the end of this article to share feedback and help shape the final release.

Use the Graph C# SDK to query content from Optimizely Graph with a strongly typed, LINQ-style API. This guide shows how to install the SDK, configure authentication, and run your first query.

Prerequisites

Before you begin, ensure that you have the following:

  • .NET 10.0 or later.
  • An Optimizely Graph instance with an App Key and Secret from the Graph portal.
  • Content synchronized to Optimizely Graph.

If content is not synchronized yet, run the Graph Full Synchronization scheduled job in CMS.

Install the package

Install the core query package.

dotnet add package Optimizely.Graph.Cms.Query

Configure your credentials

Add your Graph endpoint and credentials to appsettings.json:

{
  "Optimizely": {
    "Graph": {
      "Endpoint": "https://cg.optimizely.com",
      "AppKey": "YOUR-APP-KEY-HERE",
      "Secret": "YOUR-SECRET-KEY-HERE"
    }
  }
}
❗️

Important

Do not commit Secret to source control. Store credentials in environment variables or a secure secrets manager in production environments.

Write your first query

Inject IGraphContentClient into your controller and call QueryContent<T>() to retrieve content.

public class MyController : Controller
{
    private readonly IGraphContentClient _graphClient;

    public MyController(IGraphContentClient graphClient)
    {
        _graphClient = graphClient;
    }

    public async Task<IActionResult> Index()
    {
        var result = await _graphClient
            .QueryContent<BlogPostPage>()
            .Where(x => x.Author == "John Developer")
            .Limit(10)
            .GetAsContentAsync();

        return View(result);
    }
}

The GetAsContentAsync() method returns an IGetAsContentResult<T>. This result implements IEnumerable<T>, so you can iterate over the returned content items directly.

Common issues on first run

IssueResolution
401 UnauthorizedCheck AppKey and Secret in appsettings.json
Empty resultsVerify content is published and the Graph sync job has run
IGraphContentClient not resolvedConfirm the package is installed and DI is configured

What's next

Now that you have a working query, explore what the SDK can do: