Create Markdown document in Azure Functions (Flex Consumption)
14 Aug 202614 minutes to read
Syncfusion® Markdown is a .NET Markdown library used to create, read, edit, and convert Markdown documents programmatically without external dependencies. Using this library, you can create a Markdown document in Azure Functions deployed on the Flex Consumption plan.
Steps to create a Markdown document in Azure Functions (Flex Consumption)
Step 1: Create a new Azure Functions project.

Step 2: Create a project name and select the location.

Step 3: Select the function worker as .NET 8.0 (Long Term Support) (isolated worker) and target Flex Consumption hosting, which is suitable for the isolated worker.

Step 4: Install the Syncfusion.Markdown NuGet package as a reference to your project from NuGet.org.

Starting with v34.x.x, if you reference Syncfusion® assemblies from the trial setup or from the NuGet feed, you must add a reference to the Syncfusion.Licensing assembly and include a valid license key in your application.
Install the Syncfusion.Licensing NuGet package and register the license key during application startup.
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR_LICENSE_KEY");For more information about generating and registering a license key, refer to the Syncfusion® licensing documentation.
Step 5: Include the following namespaces in the Function1.cs file.
using Syncfusion.Office.Markdown;Step 6: Add the following code snippet in Run method of Function1 class to perform Create Markdown document in Azure Functions and return the resultant Markdown document to client end.
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req)
{
try
{
// Creates a new instance of MarkdownDocument.
MarkdownDocument markdownDocument = new MarkdownDocument();
// Adds a heading to the Markdown document.
MdParagraph mdHeadingParagraph = markdownDocument.AddParagraph();
// Applies the Heading 1 style to the paragraph.
mdHeadingParagraph.ApplyParagraphStyle("Heading 1");
MdTextRange mdHeadingTextRange = mdHeadingParagraph.AddTextRange();
mdHeadingTextRange.Text = "Adventure Works Cycles";
// Adds a paragraph to the Markdown document.
MdParagraph mdParagraph = markdownDocument.AddParagraph();
MdTextRange mdTextRange = mdParagraph.AddTextRange();
mdTextRange.Text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.";
// Adds the first list item.
MdParagraph item1 = markdownDocument.AddParagraph();
item1.ListFormat = new MdListFormat();
item1.ListFormat.IsNumbered = false;
item1.ListFormat.ListLevel = 0;
item1.ListFormat.ListValue = "- ";
item1.AddTextRange().Text = "First item";
// Adds the second list item.
MdParagraph item2 = markdownDocument.AddParagraph();
item2.ListFormat = new MdListFormat();
item2.ListFormat.IsNumbered = false;
item2.ListFormat.ListLevel = 0;
item2.ListFormat.ListValue = "- ";
item2.AddTextRange().Text = "Second item";
// Adds the third list item.
MdParagraph item3 = markdownDocument.AddParagraph();
item3.ListFormat = new MdListFormat();
item3.ListFormat.IsNumbered = false;
item3.ListFormat.ListLevel = 0;
item3.ListFormat.ListValue = "- ";
item3.AddTextRange().Text = "Third item";
// Adds a table to the Markdown document.
MdTable table = markdownDocument.AddTable();
table.ColumnAlignments.Add(MdColumnAlignment.Left);
table.ColumnAlignments.Add(MdColumnAlignment.Left);
// Adds the header row.
MdTableRow headerRow = table.AddTableRow();
MdTableCell header1 = headerRow.AddTableCell();
header1.Items.Add(new MdTextRange { Text = "Profile picture" });
MdTableCell header2 = headerRow.AddTableCell();
header2.Items.Add(new MdTextRange { Text = "Description" });
// Adds a data row.
MdTableRow dataRow = table.AddTableRow();
MdTableCell cell1 = dataRow.AddTableCell();
MdPicture picture = new MdPicture();
picture.Url = "Data\\photo.jpg";
picture.AltText = "Profile picture";
cell1.Items.Add(picture);
MdTableCell cell2 = dataRow.AddTableCell();
cell2.Items.Add(new MdTextRange { Text = "AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company." });
// Create memory stream
MemoryStream memoryStream = new MemoryStream();
//Saves the Markdown document file.
markdownDocument.Save(memoryStream);
memoryStream.Position = 0;
markdownDocument.Dispose();
var bytes = memoryStream.ToArray();
return new FileContentResult(bytes, "text/markdown")
{
FileDownloadName = "document.md"
};
}
catch (Exception ex)
{
// Log the error with details for troubleshooting
_logger.LogError(ex, "Error converting Markdown document to Image.");
// Prepare error message including exception details
var msg = $"Exception: {ex.Message}\n\n{ex}";
// Return a 500 Internal Server Error response with the message
return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" };
}
}Step 7: Right-click the project and select Publish. Then, create a new profile in the Publish window.

Step 8: Select the target as Azure and click the Next button.

Step 9: Select the specific target as Azure Function App and click the Next button.

Step 10: Click the Create new button.

Step 11: Click the Create button.

Step 12: After the App Service is created, click the Finish button.

Step 13: Click the Publish button.

Step 14: Publishing has succeeded.

Step 15: Now, go to Azure portal and select the App Services. After running the service, click Get function URL by copying it. Then, paste it in the below client sample (which will request the Azure Functions, to perform create a Markdown document using the template Markdown document). You will get the output Markdown document as follows.

Steps to post the request to Azure Functions
Step 1: Create a console application to request the Azure Functions API.
Step 2: Add the following code snippet into Main method to post the request to Azure Functions with template Markdown document and get the resultant Markdown document.
static async Task Main()
{
try
{
Console.Write("Please enter your Azure Function URL: ");
string url = Console.ReadLine();
if (string.IsNullOrWhiteSpace(url)) return;
// Create a new HttpClient instance for sending HTTP requests
using var http = new HttpClient();
using var content = new StringContent(string.Empty);
using var res = await http.PostAsync(url, content);
// Read the response body as a byte array
var resBytes = await res.Content.ReadAsByteArrayAsync();
// Extract the media type from the response headers
string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty;
// Decide the output file path the response is an markdown or txt
string outputPath = mediaType.Contains("word", StringComparison.OrdinalIgnoreCase)
|| mediaType.Contains("officedocument", StringComparison.OrdinalIgnoreCase)
|| mediaType.Equals("text/markdown", StringComparison.OrdinalIgnoreCase)
? Path.GetFullPath(Path.GetFullPath(@"Output/Output.md"))
: Path.GetFullPath(Path.GetFullPath(@"Output/function-error.txt"));
// Write the response bytes to the output markdown file
await File.WriteAllBytesAsync(outputPath, resBytes);
Console.WriteLine($"Saved: {outputPath}");
}
catch (Exception ex)
{
throw;
}
}From GitHub, you can download the console application and the Azure Functions Flex Consumption project.
The code sample references an image file (
photo.jpg). Download this asset from the GitHub sample Data folder and place it in the application’sDatafolder so theMdPicture.Urlvalue ("Data/photo.jpg") resolves correctly at runtime.
Looking for the full .NET Markdown Library overview, features, pricing, and documentation? Visit the .NET Markdown Library page.
An online sample link to create a Markdown document in ASP.NET Core.