Sean Thorne
04/25/2024, 8:24 AMcs
public class DiscordChannelDataList : IDataListSource
{
    private readonly IContentmentContentContext _contentContext;
    private readonly IDiscordService _discordService;
    public DiscordChannelDataList(IContentmentContentContext contentContext, IDiscordService discordService)
    {
        _contentContext = contentContext;
        _discordService = discordService;
    }
    public string Name => "Discord Server Channels";
    public string Description => "A list of channels from a Discord server";
    public string Icon => "icon-badge";
    public Dictionary<string, object> DefaultValues { get; } = new();
    public IEnumerable<ConfigurationField> Fields { get; } = new List<ConfigurationField>();
    public string Group => "Discord";
    public OverlaySize OverlaySize => OverlaySize.Large;
    public IEnumerable<DataListItem?> GetItems(Dictionary<string, object> config)
    {
        try
        {
            var currentNode = _contentContext.GetCurrentContent(out bool isParent);
            if (currentNode is null)
                return Enumerable.Empty<DataListItem>();
            
            var server = currentNode.Ancestor<Server>();
            if (server?.ServerId == null)
                return Enumerable.Empty<DataListItem>();
            var serverId = ulong.Parse(server.ServerId);
            var channels = Task.Run(async () => await _discordService.GetTextChannelsAsync(serverId));
            var rolesResult = channels.Result.ToList();
            if (rolesResult.Count == 0)
                return Enumerable.Empty<DataListItem>();
            return rolesResult.Select(x => new DataListItem
            {
                Value = x.Id.ToString(),
                Name = x.Name,
            });
        }
        catch
        {
            // intentionally empty
        }
        
        return Enumerable.Empty<DataListItem>();
       
    }
}
I have very simliar code in another project and it works fine. 
I want to get the current node id, to access the root node property.
My tree is:
DiscordServer
               -> node
DiscordServer
               -> node
I am on "node" and want "discord server" property.
I've tried the above code, and also some weirdness:
cs
var gotContext = _umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext);
            if (!gotContext || umbracoContext?.Content == null)
                return Enumerable.Empty<DataListItem>();
            var gotId = int.TryParse(_requestAccessor.GetQueryStringValue("id"), out var id);
            if (!gotId)
                return Enumerable.Empty<DataListItem>();
            var publishedContent = umbracoContext.Content.GetById(id);
            if (publishedContent == null)
                return Enumerable.Empty<DataListItem>();Sean Thorne
04/25/2024, 8:27 AMJemayn
04/25/2024, 9:19 AMSean Thorne
04/25/2024, 9:39 AMSean Thorne
04/25/2024, 10:04 AMcs
app.Use(async (context, next) =>
{
    context.Request.EnableBuffering();
    string bodyContent;
    using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8, false, leaveOpen: true))
    {
        bodyContent = await reader.ReadToEndAsync();
        // Reset the request body stream position so it can be read again further in the pipeline
        context.Request.Body.Position = 0;
    }
    // Store the request body content to be accessed later in the pipeline
    context.Items["RequestBody"] = bodyContent;
    await next.Invoke();
});
Now I have access to the body (as it's already read by the time it's received)
cs
            if (_httpContextAccessor.HttpContext == null)
                return Enumerable.Empty<DataListItem>();
            
            var requestBody = _httpContextAccessor.HttpContext.Items["RequestBody"]?.ToString();
            if (string.IsNullOrEmpty(requestBody))
                return Enumerable.Empty<DataListItem>();
            
            var request = System.Text.Json.JsonSerializer.Deserialize<GetByEmptyKeysRequest>(requestBody);
            if (request is null)
                return Enumerable.Empty<DataListItem>();
            // get parent id
            var parentId = request.ParentId;
I can now do my get at root of this node just fineSean Thorne
04/25/2024, 10:07 AMSean Thorne
04/25/2024, 10:07 AMJemayn
04/25/2024, 10:08 AMSean Thorne
04/25/2024, 10:09 AMJemayn
04/25/2024, 10:09 AMSean Thorne
04/25/2024, 10:10 AMJemayn
04/25/2024, 10:11 AMA hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by