Wednesday, October 21, 2009

Notes and Attachments


Almost every Entity in CRM has support for Notes and Attachments.
A Note could be almost any piece of text which does not fall under any of the CRM Activity types.(phone,fax,email etc.)
Attachment lets to attach any kind of documents (doc,xls,pdf,image files etc) to a particular record in CRM.


The Notes tab in CRM, allows you to create just a note, however if you need to create a note with an attachment, you will need to browse to Actions->Attach a Note on the menubar of the particular record.


All Notes go into the annotationbase table on the CRM DB.
Even attachments if any, associated with a note are stored in this table.
This architecture allows the attachements to the accessed even via the Outlook Client in Offline mode.


Imp: Only 1 file can be attached per Note.


Create Notes using the CRM SDK:
//Create a simple note
annotation note = new annotation();
note.notetext = "This is a note";
EntityNameReference entRef = new EntityNameReference();
entRef.Value = "account";
note.objecttypecode = entRef;
note.objectid = [GUID of the account record];
crmService.Create(note);


//Create note with an attachment
annotation note = new annotation();
note.notetext = "This is a note";
EntityNameReference entRef = new EntityNameReference();
entRef.Value = "account";
note.objecttypecode = entRef;
note.objectid = [GUID of the account record];
Guid noteID = crmService.Create(note);


UploadFromBase64DataAnnotationRequest uploadFile = new UploadFromBase64DataAnnotationRequest();
uploadFile.AnnotationId = noteID;
uploadFile.FileName = "Example.txt";
uploadFile.MimeType = "text/plain"; //Other possible values 'application/msword' for word doc
//Since this file would be stored in the CRM DB, the file must be converted to a base64 encoded string
uploadFile.Base64Data = GetBase64StringFromFile("Example.txt");
crmService.Execute(uploadFile);


private static string GetBase64StringFromFile(string filename) {
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
using (BinaryReader reader = new BinaryReader(fs)) {
byte[] fileContent = reader.ReadBytes((int) fs.Length);
return Convert.ToBase64String(fileContent);
  }
 }
}


//Retrieve Attachment from CRM
On the CRM domain one can use the below url to download an attachment directly from the CRM Server
http://[server:port]/[organization name]/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId=[Annotation ID]


The above url will prompt the user to either Open or Save the the particular attachment.


//The below SDK approach can also be used to download an attachment
Private byte[] GetAnnotationAttachment(Guid annotationId){
Guid attachid = annotationId;
//Pass the Annotation attachment OTC and the Annotation ID
http://[server:port]/[organization name]/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId=[Annotation ID]
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
return myWebClient.DownloadData(url);
}


byte[] exampleByteArray1 = GetAnnotationAttachment(new Guid("D7D5E13B-E3B4-DE11-A647-00012E0B81A2"));
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.AppendHeader("content-disposition", "attachment; filename=" + "Sample.xls");
Response.BinaryWrite(exampleByteArray1);

6 comments:

  1. hello sir...
    Please can u explain ..what would be the code if i want only create a Notes without any attachment....

    Thanks and regards..
    setu1205@gmail.com

    ReplyDelete
  2. hi, which no. should I use as attachment type for other types of file like pdf, text, excel etc. And the url given above used to download the file shows Access Denied error.

    ReplyDelete
  3. Hi,

    I tried using the download attachement file code but its also gives me the Access denied error. Does anyone have the solution to this error?

    ReplyDelete
  4. Solution is here
    http://cloudytech.blogspot.com/2013/06/access-denied-opening-attachment-in-crm.html

    ReplyDelete
  5. Hello, you used to write fantastic, but the last several posts have been kinda boring… I miss your great writings. Past few posts are just a little out of track! come on!
    artificial intelligence companies in usa

    ReplyDelete