Wednesday, August 25, 2021

Get all the file names in List from blob container in Dynamics 365 finance and operations

 public static list getFileNamesFromBlob(CloudBlobContainer _blobContainer, str _filepath)

    {

        CloudBlobDirectory  cloudBlobDirectory;

        container           con;


        cloudBlobDirectory  = _blobContainer.GetDirectoryReference(_filepath);


        System.Collections.IEnumerable  lstEnumarable   = cloudBlobDirectory.ListBlobs(false, 0, null, null);

        System.Collections.IEnumerator  lstEnumarator   = lstEnumarable.GetEnumerator();

        List                            filenames       = new List(Types::String);

        

        while(lstEnumarator.MoveNext())

        {

            IListBlobItem   item = lstEnumarator.Current;

            

            if(item is CloudBlockBlob)

            {

                CloudBlockBlob  blob = item;

                blob.FetchAttributes(null, null, null);

                con = str2con(blob.name, '/');

                filenames.addStart(conPeek(con,conlen(con)));

            }

        }


        return filenames;

    }

Converting string to stream in Dynamics 365 finance and operations

 

/// <summary>

    /// Converts string to stream

    /// </summary>

    /// <param name="_record">

    /// String record to be converted to stream

    /// </param>

    /// <param name="_encoding">

    /// Encoding format

    /// </param>

    public static MemoryStream convertStringToStream(str _record, System.Text.Encoding _encoding)

    {

        System.Byte[]  byteArray =   new System.Byte[0]();


        if(_encoding == System.Text.Encoding::Unicode)

        {

            byteArray = System.Text.Encoding::Unicode.GetBytes(_record);

        }

        else

        {

            byteArray = System.Text.Encoding::ASCII.GetBytes(_record);

        }


        return  new MemoryStream( byteArray );

    }