Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Upload File to CRM File-Field with C#

Posted on by 5

Hi, I created a file field at powerapps and can upload a file in the CRM formular. Now I need an upload by C# and I found the classes:

InitializeFileBlocksUploadRequest and CommitFileBlocksUploadRequest

Fine except of the "BlockList" at CommitFileBlocksUploadRequest: How to set it correctly within byte-Array or base64 String?

public long UploadFileAsByteArray(ref IOrganizationService service, ref ITracingService tracingService, ref byte[] bytes, string fileName, EntityReference entityReference, string fieldNameOfFile) { string[] blockList = new string[1]; blockList[0] = InfSystem.GetBase64OfByteArray(bytes); InitializeFileBlocksUploadRequest initRequest = new InitializeFileBlocksUploadRequest { Target = entityReference, FileAttributeName = fieldNameOfFile, FileName = fileName, }; InitializeAnnotationBlocksUploadResponse initResponse = (InitializeAnnotationBlocksUploadResponse)service.Execute(initRequest); CommitFileBlocksUploadRequest commitRequest = new CommitFileBlocksUploadRequest { BlockList = blockList, MimeType = InfSystem.GetMimetypeByFilename(fileName), FileContinuationToken = initResponse.FileContinuationToken }; CommitFileBlocksUploadResponse comitResponse = (CommitFileBlocksUploadResponse)service.Execute(commitRequest); long fileSizeInBytes = comitResponse.FileSizeInBytes; return fileSizeInBytes; }

  • RobertHaupt Profile Picture
    RobertHaupt 5 on at
    RE: Upload File to CRM File-Field with C#

    Hi Esteban,

    yes, BlockList is a string array but how can fill it with the bytes of an bytes array of the file?

    And how to enter code in a ticket to be shown in a goog readable way?

    Best Robert

  • RE: Upload File to CRM File-Field with C#

    Hello, to declare a BlockList you can do it in the following ways:

    For type string []:
        public string [] BlockList {get; set; }

    You can check it at the following link: https://docs.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.commitfileblocksuploadrequest.blocklist?view=dynamics-general-ce-9#Microsoft_Crm_Sdk_Messages_CommitistFilestlocklock_Messages_CommitistFilestlock_lockRequest

    For the string type:
        public string BlockId {get; set; }

    You can check it in the following link: https://docs.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.uploadblockrequest.blockid?view=dynamics-general-ce-9#Microsoft_Crm_Sdk_Messages_UploadBlockRequest_BlockIsages_UploadBlockRequest_BlockIsages_UploadBlockRequest_BlockIsages


    The complete information of the CommitFileBlocksUploadRequest Class can be found at the following link: https://docs.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.commitfileblocksuploadrequest?view=dynamics-general-ce-9

    Thanks!

    Community Support Team - Esteban
    If this Post helps, then please consider Accept as solution to help the other members find it more quickly.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans