Skip to content

Instantly share code, notes, and snippets.

@campersau
Created July 14, 2021 11:06
Show Gist options
  • Save campersau/b3198f881b84e23c908adeafea7494fe to your computer and use it in GitHub Desktop.
Save campersau/b3198f881b84e23c908adeafea7494fe to your computer and use it in GitHub Desktop.
open bi server - Custom HtmlItem
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<!-- References to open bi server -->
<ItemGroup>
<Reference Include="BiExcellence.OpenBi.Server.License.Abstractions">
<HintPath>C:\OPENBI\BiExcellence.OpenBi.Server.License.Abstractions.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack">
<HintPath>C:\OPENBI\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="ibssolution.bioxRepository">
<HintPath>C:\OPENBI\ibssolution.bioxRepository.exe</HintPath>
</Reference>
<Reference Include="ibssolution.bioxSession">
<HintPath>C:\OPENBI\ibssolution.bioxSession.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Http.Abstractions">
<HintPath>C:\OPENBI\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Http.Features">
<HintPath>C:\OPENBI\Microsoft.AspNetCore.Http.Features.dll</HintPath>
</Reference>
</ItemGroup>
<!-- Copy dll to C:\OPENBI\httpserver\plugins after debug build -->
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Debug' ">
<Exec Command="copy /Y &quot;$(TargetPath)&quot; &quot;C:\OPENBI\httpserver\Plugins\$(TargetFileName)&quot;" />
</Target>
</Project>
{
"profiles": {
"ConsoleApp2": {
"commandName": "CustomHttpHandlerExample",
"executablePath": "C:\\OPENBI\\ibssolution.bioxRepository.exe",
"workingDirectory": "C:\\OPENBI\\"
}
}
}
using BiExcellence.OpenBi.Server.License.Abstractions;
using HtmlAgilityPack;
using Ibssolution.biox.Repositoryserver;
using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Threading.Tasks;
namespace CustomHtmlItemExample
{
public class CustomHtmlItem : HtmlItem
{
private readonly ILicense _license;
private readonly string _templateId;
// Get ILicense from DI
public CustomHtmlItem(HtmlNode node, ILicense license)
: this(node)
{
_license = license;
}
// Called once when server starts with null node
public CustomHtmlItem(HtmlNode node)
: base(node)
{
// Read passed in attributes
Attributes.TryGetValue("data-template", out _templateId);
}
// Called for GET requests when used in HTML
protected override async Task<string> GetHtmlFromTagChildAsync(CmsDescription cms, CancellationToken cancellationToken)
{
// Get item template
var template = await GetItemTemplateAsync(_templateId, cms);
// Check if user is authentificated
if (cms.HttpContext.User.Identity.IsAuthenticated)
{
template = template.Replace("%USERNAME%", cms.OpenBiRequest.User.Username);
template = template.Replace("%USERNAME%", cms.HttpContext.User.Identity.Name); // same as above
}
template = template.Replace("%LICENSE_NAME%", _license.Name);
// append the InnerHtml from the HTML
template += HtmlNode.InnerHtml;
return template;
}
// Called for form POST requests
protected override async Task ProcessActionChildAsync(CmsDescription cms, string action, CancellationToken cancellationToken)
{
// Check the action
if (action == "customaction")
{
// Get the form from the request
var form = await cms.HttpContext.Request.ReadFormAsync();
// Change response Content-Type
cms.HttpContext.Response.ContentType = "text/html";
// Check if user is authentificated
if (cms.HttpContext.User.Identity.IsAuthenticated)
{
await cms.HttpContext.Response.WriteAsync($"<h1>Username: {cms.OpenBiRequest.User.Username}</h1>");
await cms.HttpContext.Response.WriteAsync($"<h1>Username: {cms.HttpContext.User.Identity.Name}</h1>"); // same as above
}
await cms.HttpContext.Response.WriteAsync($"License Name: {_license.Name}");
}
}
// Document possible attributes
protected override void AddParametersToCollection(HtmlItemParameterCollection collection)
{
collection.Add(new HtmlItemParameter("data-template", HtmlItemParameterType.Template, "Custom Template"));
}
// Document possible replacements
protected override HtmlItemReplacementParameterCollection getReplacementParameters()
{
var replacements = new HtmlItemReplacementParameterCollection();
replacements.Add(new HtmlItemReplacementParameter("%USERNAME%", "Username"));
replacements.Add(new HtmlItemReplacementParameter("%LICENSE_NAME%", "License Name"));
return replacements;
}
// HTML tag name which will instantiate a new instance of this class when found the HTML
public override string GetTagName()
{
return "custom:htmlitem";
}
// Document HTML item
public override string GetDescription()
{
return "My Custom HTML Item";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment