Thursday, January 29, 2009

CRM 4.0: Adding Attachment / Notes Programmatically to Entities

Hello, CRM Party people, to entertain your greed towards CRM snippets and to keep me from forgetting such a trivial thing, here we go … the adding attachment / note / annotation snippet for CRM 4.0. (In this sample I am binding it to the incident entity).

public static bool AddAttachment(string filename, Guid incidentguid, byte[] modal, out string errormessage)

{

try

{

string encodedData = Convert.ToBase64String(modal);

annotation ano = new annotation();

ano.filename = filename;

ano.isdocument = new Petrosea.CRM.SD.MetaUtility.CRMWebService.CrmBoolean();

ano.isdocument.Value = true;

ano.documentbody = encodedData;

ano.mimetype = "application/octet-stream";

ano.objectid = new Lookup();

ano.objectid.type = EntityName.incident.ToString();

ano.objectid.Value = incidentguid;

ano.objecttypecode = new EntityNameReference();

ano.objecttypecode.Value = EntityName.incident.ToString();

TargetCreateAnnotation target = new TargetCreateAnnotation();

target.Annotation = ano;

CreateRequest createrequest = new CreateRequest();

createrequest.Target = target;

CreateResponse created = (CreateResponse)crmService.Execute(createrequest);


errormessage = String.Empty;

return true;

}

catch (Exception ex)

{

errormessage = ex.Message;

return false;

}

}


1 comment: