Tuesday 30 April 2019

Delete File/ Folder from Azure storage account in D365 using X++ code

This code will help you to delete list of files in a folder using CloudBlobContainer reference

public void deleteFilesFromAzure(CloudBlobContainer _blobContainer)
{
CloudBlobDirectory blobDirectoryToDelete;
System.Collections.IEnumerable listToDelete;
System.Collections.IEnumerator listEnumeratorToDelete;
CloudBlockBlob blobToDelete;

blobDirectoryToDelete = _blobContainer.GetDirectoryReference("File/ folder name");

listToDelete = new System.Collections.Generic.List<str>();

listToDelete =  blobDirectoryToDelete.ListBlobs(false, 0, null, null);

listEnumeratorToDelete = listToDelete.getEnumerator();

while  (listEnumeratorToDelete.moveNext())
{
blobToDelete = listEnumeratorToDelete.get_Current();
blobToDelete.DeleteIfExists(DeleteSnapshotsOption::None, null, null, null);
}
}

Thanks,
DAXBuddy

Convert Base64(PDF file) to Memory stream in D365

Now converting Base64 to Memory stream is a single piece of code. 

Here you go...

System.IO.MemoryStream memoryStream = Binary::constructFromContainer(BinData::loadFromBase64("Base64 Value")).getMemoryStream() as System.IO.MemoryStream;

Thanks,
DAXBuddy

Get all files (Blobs) in a list from Azure storage container in D365 using X++ code

Now a days Microsoft have moved everything to Azure. But in D365 fetching blobs as a list from a storage container is still tricky. Here in this blog code you'll be able to get all the blobs from Azure container in a single list.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

class Azure
{
    public void processAzureBlobFiles()
    {      
         var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("Storage account name", "Access key");

CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer blobContainer = blobClient.GetContainerReference("Container Name");

System.Collections.IEnumerable list = new System.Collections.Generic.List<str>();
list = blobContainer.ListBlobs(null, false, 0, null, null);
System.Collections.IEnumerator listEnumerator = list.getEnumerator();

while  (listEnumerator.moveNext())
{
CloudBlockBlob blob = listEnumerator.get_Current();

if (blob.Exists(null, null))
{
        str file = blob.StorageUri.PrimaryUri.AbsoluteUri;
str fileName = blob.Name;
}
}
    }
}

"Storage account name" Can be taken from Azure -> Storage accounts -> Settings -> Access keys -> Storage account name

"Access key" Can be taken from Azure -> Storage accounts -> Settings -> Access keys -> Key1 or Key2


"Container Name" Can be taken from Azure -> Storage accounts -> Blobs -> Container name

Thanks,
DAXBuddy

Convert HTML text to PDF stream in D365

In D365, out of the box HTML to PDF converter is not available using X++ code. So we need to use NuGet package "HTMLRenderer.PDFSharp" which is open source. 

  • Create a new console application VS project
  • Right click on the project and select "Manage NuGet package"

  • New NuGet window opens, Search for "HTMLRenderer.PDFSharp" and install it.
  • After installing you can see the installed references in the project
  •  To convert HTML to PDF we required only 3 references
    • HTMLRenderer
    • HTMLRenderer.PDFSharp
    • PDFSharp
  • Copy the .dll's file from the reference path for all the 3 references which is available in the properties
  • Create a new D365 project and add the 3 references .dll's to the D365 project reference as image shown below unser D365 project

Now here is the ready made code for you to convert HTML to PDF and PDF will be as a stream:
     public static System.IO.Stream converttopdffile(str _html)
    {
        PdfSharp.Pdf.PdfDocument pdf =                                                               TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator::GeneratePdf(_html, PdfSharp.PageSize::A4,       10, null, null);
        
        System.IO.Stream stream = new System.IO.MemoryStream();
        pdf.Save(stream, false);
        
        System.IO.StreamWriter writer = new System.IO.StreamWriter(stream);
        
        return stream;
    }

Here your HTML is now converted :)

Thanks,
DAXBuddy