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
AppKeyandSecretfrom 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.QueryConfigure 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"
}
}
}
ImportantDo not commit
Secretto 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
| Issue | Resolution |
|---|---|
401 Unauthorized | Check AppKey and Secret in appsettings.json. |
| Empty results | Verify content is published and the Graph Full Synchronization job has run. |
IGraphContentClient not resolved | Confirm 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:
- Authentication requests in C# SDK — Single Key versus Basic authentication
- Filter Optimizely Graph results using C# SDK — filter by string, numeric, boolean, and date fields
- Search content in Optimizely Graph using C# SDK — full-text search with field boosting
- Paginate Optimizely Graph results using C# SDK —
SkipandLimitfor large result sets - Aggregate Optimizely Graph results in C# SDK — facets for category-based filtering
- Search tracking — track which results users click
Updated 6 days ago
