Programmatic way to download CRM attachments



We have some sample code on this topic being released in the next rev of the SDK samples (coming very soon!). In the meantime, since I have been asked about this a few times, here is a simple piece of code that shows how to download CRM attachments using the .NET WebClient class.  Needless to mention that Uploading attachments programmatically through web services is a new feature in CRM V3.0.


using System.Net;


//NOTE: AttachmentType represents the type of the attachment: 1001 for ActivityMimeAttachment, 5 for Annotation and 1070 for SalesLiteratureItem.


//This is the ID of the attachement that you are trying to download


string myAttachementId = "{00000000-0000-0000-0000-00000000000}";


string url = "http://localhost/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId=" + myAttachementId;


WebClient myWebClient = new WebClient();


myWebClient.Credentials = CredentialCache.DefaultCredentials;


myWebClient.DownloadFile(url,"C:\myfile");


Comments (9)
  1. What we really want to do is email attachments programmatically. Is that easy to do?

  2. Arashs says:

    You can use the same code.  Email attachements are saved as ActiviyMimeAttachements in CRM (use code 1001 as listed above).  

  3. Hi,

    I have been trying to get some reference code on how to use the activitymimeattachment class to upload an attachment to a mail but have found anything. Any example code you can throw to the dogs? 🙂

    Gustaf

  4. Arashs says:

    We have a good sample in the SDK docs.  Download the latest SDK from the link on my recent blogs.  After install go here:
    <your install location?sdk samplesreferencecrmserviceapimiscellaneousuploadactivitymimeattachmentfrombase64data.cs

  5. asfarsadewa says:

    i got a 401 error when doing this from an application.

    i did not use the default credential though, i use a new networkcredential object to pass my user-passwd-domain to the server.

    any hint on this?

  6. Arashs says:

    Can you access any of the CRM web services or application from that machine?  If you want to impersonate, use the callerID header that is explained in the SDK documents (link in my blog).

     

    Thanks

    Arash

  7. Pedro Tanaka says:

    Hi ArashSichanie,

    You Help me a lot!

    but , If a annotation have 2 or more attachments?

    there is a to choose wich one download?

  8. pedro tanaka says:

    Hey , Thx , I missread it  ehhe , just choose the right guid

  9. akash Randeniya says:

    thank you dude thats work

    many thanks

Comments are closed.

Skip to main content