Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

How to upload files to the Microsoft Dynamics CRM server using C# Crm SDK?

(0) ShareShare
ReportReport
Posted on by 5

Good day,

I have a task in my job working place to create a C# automatic tool that would be useful in uploading a large amount of files from the local disk to the Microsoft Dynamics 365 (on-premises) server. Once the tool uploads these files, it will be required for it to use a .CSV file, which has all the information on which files need to be attached as attachment files to certain Entity records. Currently I am a beginner and learner on how to create automatic tools for the CRM server. I have intermediate experience with C# but I am not very familiar with creating automatic tools for the CRM server. I want learn and understand the best ways in creating a automatic tool for the CRM server. I read online in the Microsoft documentation pages, stackoverflow, github and watched some tutorials which mentioned to use the Microsoft dynamics SDK for C#. I installed the SDK code samples on the local disk and the SDK assembly packages using Visual Studio Nuget tool in my Visual Studio project. However, all of these sources have information that is a bit confusing, sometimes misleading and gives me pressure in figure outing how to start writing code for this CRM tool. Currently I managed to create to login process part, which uses CrmClientService class and successfully logs into the server with the hardcoded connection string(Which later on will be located in a specific tool config file). However, I am currently stuck in understanding how to upload files to existing entity records. I've spent many days in learning and I am struggling a bit.

Could some of you give me some useful tips, recommendations or some information sources on how to start writing a simple mini tool, who will upload one file to a certain CRM server entity table record?

Once I understand how to accomplish this mini task, then it will be a nice start for me in understanding on how to upload large quantity of files that are located on the disk and required to be uploaded to the CRM server entity records as attachments. I only need to understand how to upload one simple file and I want do it in C# code.

Cheers,

Paulius

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,073 Moderator on at
    RE: How to upload files to the Microsoft Dynamics CRM server using C# Crm SDK?

    yes you can use an alternate key (but the alternate key must be defined first)

    the syntax would be

    attachment["objectid"] = new EntityReference("account", "alternatekey", "alternatekeyvalue");

    here a documentation link on how to create alternate keys https://learn.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/define-alternate-keys-reference-records?view=op-9-1

  • Paul23W Profile Picture
    Paul23W 5 on at
    RE: How to upload files to the Microsoft Dynamics CRM server using C# Crm SDK?

     Guido Preite 

    Is it possible to create an attachment objectId EntityReference using not a Guid(in your example would be accountID at line 6) but with a unique alternate key Entity text field like for example Account name?

  • Paul23W Profile Picture
    Paul23W 5 on at
    RE: How to upload files to the Microsoft Dynamics CRM server using C# Crm SDK?

    Thank you for the reply. I check the code sample you provided. Later I will reply on how things went by testing the code sample that you provided

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,073 Moderator on at
    RE: How to upload files to the Microsoft Dynamics CRM server using C# Crm SDK?

    To upload files in Dynamics you usually use the annotation entity (there is also a file column in the recent versions but I don't think it's your scenario).

    Assuming you want to attach a file to an account with the ID stored inside accountId, this code should work:

    string content = "Example";
    byte[] fileContent = Encoding.ASCII.GetBytes(content);
    string encodedData = System.Convert.ToBase64String(fileContent);
    
    Entity attachment = new Entity("annotation");
    attachment["objectid"] = new EntityReference("account", accountId);
    attachment["objecttypecode"] = "account";
    attachment["subject"] = "Uploaded Attachment - Subject";
    attachment["notetext"] = "Uploaded Attachment - Description";
    attachment["documentbody"] = encodedData;
    attachment["mimetype"] = @"text/plain"; // MIME of the file
    attachment["filename"] = "example.txt"; // filename of the file
    service.Create(attachment);

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans