Skip to content

Instantly share code, notes, and snippets.

@waldekmastykarz
Created October 12, 2023 09:07
Show Gist options
  • Save waldekmastykarz/5b5dc09759f97ee3611d2d2bb4a1db16 to your computer and use it in GitHub Desktop.
Save waldekmastykarz/5b5dc09759f97ee3611d2d2bb4a1db16 to your computer and use it in GitHub Desktop.
Build Microsoft Graph connector article - ContentService.cs
using Markdig;
using Microsoft.Graph.Models.ExternalConnectors;
using YamlDotNet.Serialization;
// [...] trimmed for brevity
static class ContentService
{
static IEnumerable<BlogPost> Extract()
{
// [...] trimmed for brevity
}
static IEnumerable<ExternalItem> Transform(IEnumerable<BlogPost> content)
{
var baseUrl = new Uri("https://blog.mastykarz.nl");
return content.Select(a =>
{
return new ExternalItem
{
Id = a.Slug,
Properties = new()
{
AdditionalData = new Dictionary<string, object> {
{ "title", a.Title ?? "" },
{ "excerpt", a.Excerpt ?? "" },
// [...] trimmed for brevity
}
},
Content = new()
{
Value = a.Content ?? "",
Type = ExternalItemContentType.Text
},
Acl = new()
{
new()
{
Type = AclType.Everyone,
Value = "everyone",
AccessType = AccessType.Grant
}
},
// [...] trimmed for brevity
};
});
}
static async Task Load(IEnumerable<ExternalItem> items)
{
foreach (var item in items)
{
await GraphService.Client.External
.Connections[ConnectionConfiguration.ExternalConnection.Id]
.Items[item.Id]
.PutAsync(item);
}
}
public static async Task LoadContent()
{
var content = Extract();
var transformed = Transform(content);
await Load(transformed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment