Home > C# > Dataverse File Download error | Feature ‘target-typed object creation’ is not available in C# 7.3

Dataverse File Download error | Feature ‘target-typed object creation’ is not available in C# 7.3

I was trying to download the Dataverse File using the code snippet provided in official documentation.

Following is the snippet and I got the Feature ‘target-typed object creation’ is not available in C# 7.3 compilation error.

Reason:

  • Target-typed new expressions has been introduced in C# 9.0 using which we do not require type specification for constructors when the type is known.
  • Since I was not having C# 9.0 I was getting the compilation error.

Fix:

  • Fix is simple, we need to replace
InitializeFileBlocksDownloadRequest initializeFileBlocksDownloadRequest = new()
            {
                Target = entityReference,
                FileAttributeName = fileAttributeName
            };sds

with

            InitializeFileBlocksDownloadRequest initializeFileBlocksDownloadRequest = new InitializeFileBlocksDownloadRequest
            {
                Target = entityReference,
                FileAttributeName = fileAttributeName
            };

and replace

            List<byte> fileBytes = new();

with

            List<byte> fileBytes = new List<byte>();
Modified code snippet:
    /// <summary>
    /// Downloads a file
    /// </summary>
    /// <param name="service">The service</param>
    /// <param name="entityReference">A reference to the record with the file column</param>
    /// <param name="fileAttributeName">The name of the file column</param>
    /// <returns></returns>
    private static byte[] DownloadFile(
                IOrganizationService service,
                EntityReference entityReference,
                string fileAttributeName)
    {
        InitializeFileBlocksDownloadRequest initializeFileBlocksDownloadRequest = new InitializeFileBlocksDownloadRequest()
        {
            Target = entityReference,
            FileAttributeName = fileAttributeName
        };

        var initializeFileBlocksDownloadResponse =
              (InitializeFileBlocksDownloadResponse)service.Execute(initializeFileBlocksDownloadRequest);

        string fileContinuationToken = initializeFileBlocksDownloadResponse.FileContinuationToken;
        long fileSizeInBytes = initializeFileBlocksDownloadResponse.FileSizeInBytes;

        List<byte> fileBytes = new List<byte>();

        long offset = 0;
        long blockSizeDownload = 4 * 1024 * 1024; // 4 MB

        // File size may be smaller than defined block size
        if (fileSizeInBytes < blockSizeDownload)
        {
            blockSizeDownload = fileSizeInBytes;
        }

        while (fileSizeInBytes > 0)
        {
            // Prepare the request
            DownloadBlockRequest downLoadBlockRequest = new()
            {
                BlockLength = blockSizeDownload,
                FileContinuationToken = fileContinuationToken,
                Offset = offset
            };

            // Send the request
            var downloadBlockResponse =
                     (DownloadBlockResponse)service.Execute(downLoadBlockRequest);

            // Add the block returned to the list
            fileBytes.AddRange(downloadBlockResponse.Data);

            // Subtract the amount downloaded,
            // which may make fileSizeInBytes < 0 and indicate
            // no further blocks to download
            fileSizeInBytes -= (int)blockSizeDownload;
            // Increment the offset to start at the beginning of the next block.
            offset += blockSizeDownload;
        }

        return fileBytes.ToArray();
    }

🙂

Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: