web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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);

  • Paul23W Profile Picture
    5 on at

    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

  • Paul23W Profile Picture
    5 on at

     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?

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 179 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 131

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans