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

Quick start with the Graph C# SDK

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

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

Prerequisites

Before you begin, confirm that you have the following:

  • .NET 10.0 or later.
  • An Optimizely Graph instance with an AppKey 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",
      "Secret": "YOUR_SECRET_KEY"
    }
  }
}
❗️

Important

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

Write your first query

Inject IGraphContentClient into your controller and call QueryContent<T>() to retrieve content. The following example queries BlogPostPage items by author:

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);
    }
}

GetAsContentAsync() returns an IGetAsContentResult<T>, which implements IEnumerable<T>. 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 Full Synchronization job has run.
IGraphContentClient not resolvedConfirm that the package is installed and dependency injection (DI) is configured.

Next steps

Now that you have a working query, explore the rest of the SDK: