Welcome to MSDN Blogs Sign in | Join | Help

Arash Ghanaie-Sichanie Blog

Microsoft, Program Management, Live Services, Software + Service and more....
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");

Posted: Thursday, February 09, 2006 2:59 AM by ArashSichanie

Comments

Sterling Ledet said:

What we really want to do is email attachments programmatically. Is that easy to do?
# February 23, 2006 6:09 AM

ArashSichanie said:

You can use the same code.  Email attachements are saved as ActiviyMimeAttachements in CRM (use code 1001 as listed above).  
# March 14, 2006 5:56 PM

Gustaf Westerlund said:

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
# July 5, 2006 10:42 AM

ArashSichanie said:

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 samples\reference\crmserviceapi\miscellaneous\uploadactivitymimeattachmentfrombase64data.cs
# July 12, 2006 9:47 PM

asfarsadewa said:

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?
# July 24, 2006 11:55 PM

ArashSichanie said:

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

# July 26, 2006 3:54 AM

Pedro Tanaka said:

Hi ArashSichanie,

You Help me a lot!

but , If a annotation have 2 or more attachments?

there is a to choose wich one download?

# May 31, 2007 1:29 PM

pedro tanaka said:

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

# May 31, 2007 1:34 PM

akash Randeniya said:

thank you dude thats work

many thanks

# June 19, 2008 6:43 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker