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 previewThis 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 KeyandSecretfrom 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"
}
}
}
ImportantDo not commit
Secretto 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
| Issue | Resolution |
|---|---|
401 Unauthorized | Check AppKey and Secret in appsettings.json |
| Empty results | Verify content is published and the Graph sync job has run |
IGraphContentClient not resolved | Confirm the package is installed and DI is configured |
What's next
Now that you have a working query, explore what the SDK can do:
- Authentication requests in C# SDK — understand Single Key vs. Basic Auth
- Filter Optimizely Graph results using C# SDK — filter by strings, numbers, dates, and boolean fields
- Search content in Optimizely Graph using C# SDK — full-text search with field boosting
- Paginate Optimizely Graph results using C# SDK — skip/limit for large result sets
- Aggregate Optimizely Graph results in C# SDK — facets for category-based filtering
- Search tracking — track which results users click
Updated 13 days ago
