Customização Microsoft CRM – Programando Email Anexos À Atividades
por Boris Makushkin, Grupo Alba Spectrum
Alba Spectrum Group
São Paulo, USA +1-866-528-0577, +1-630-961-5918,
help@albaspectrum.com skype: albaspectrum
Microsoft CRM SDK C# Development
Microsoft CRM está agora em cena e está aumentando sua fatia no mercado, graças aos músculos Microsoft Business Solutions e sua estratégia de marketing. Ele está fortemente integrado com outros produtos Microsoft Business Solutions tais como Microsoft Great Plain, Solomon, Navision. Sendo relativamente barato em comparação aos concorrentes, como Siebel, Oracle – Microsoft CRM lhe abre as portas para operações aumatizadas em todo o mundo. Neste pequeno artigo gostaríamos de dar a você, desenvolvedor de software, algumas dicas the customização em Microsoft CRM.
O tópico de hoje é Atividade de tipo de programação de email – geralmente estas customizações são lidadas quando o conector Microsoft Exchange é melhorado. Como criar email attachment – este e o principal topico de discussão. Usaremos C#.Net.
Em Exchange handler/ event sink você cria Atividade de um tipo de email MS CRM e uma das tarefas é transferir o(s) anexo(s) na Atividade. É possível percebê-lo através do direto acesso a Microsoft CRM DB. Veremos o código C##:
CDO.Message iMessage = new CDO.MessageClass();
CDO.IBodyPart iPrt;
iMessage.DataSource.Open(bstrURLItem, null, ADODB.ConnectModeEnum.adModeRead,
ADODB.RecordCreateOptionsEnum.adFailIfNotExists, ADODB.RecordOpenOptionsEnum.adOpenSource, "", "");
for(int i = 1; i <= aNum; i++) {
string fName = iMessage.Attachments[i].FileName;
string eName = fName.Substring(fName.Length-3, 3).ToUpper();
log.Debug("Attachment: " + fName);
iPrt = iMessage.Attachments[i];
string attName = Path.GetTempPath() + fName;
iPrt.SaveToFile(attName);
attachments.Add(Guid.NewGuid(), attName);
iPrt = null;
}
if (attachments != null) {
ICollection keys = attachments.Keys;
int attCounter = 0;
foreach (Guid o in keys) {
string attName = (string)(attachments[o]);
crmConnector.AddAttachmentToActivity(emailId, attName, (new FileInfo(attName)).Length, attCounter);
File.Delete(attName);
attCounter++;
}
}
public Guid AddAttachmentToActivity(Guid emailId, string filename, long filesize, int attachmentNumber) {
try {
log.Debug("Prepare for Mail Activity Attachment Creating");
// BizUser proxy object
Microsoft.Crm.Platform.Proxy.BizUser bizUser = new Microsoft.Crm.Platform.Proxy.BizUser();
ICredentials credentials = new NetworkCredential(sysUserId, sysPassword, sysDomain);
bizUser.Url = crmDir + "BizUser.srf";
bizUser.Credentials = credentials;
Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();
// CRMActivityAttachment proxy object
Microsoft.Crm.Platform.Proxy.CRMActivityAttachment activityAttachment = new Microsoft.Crm.Platform.Proxy.CRMActivityAttachment();
activityAttachment.Credentials = credentials;
activityAttachment.Url = crmDir + "CRMActivityAttachment.srf";
// Set up the XML string for the activity attachment
string strXml = "<activitymimeattachment>";
strXml += "<subject>Activity 1</subject>";
strXml += "<attachmentnumber>" + attachmentNumber + "</attachmentnumber>";
strXml += "<activityid>" + emailId.ToString("B") + "</activityid>";
strXml += "</activitymimeattachment>";
// Create the activity attachment
Guid attachmentId = new Guid(activityAttachment.Create(userAuth, strXml));
log.Debug("Create Attachemnt ID: " + attachmentId.ToString("B"));
UploadFileToDB(attachmentId, filename, filesize);
return attachmentId;
}
catch (System.Web.Services.Protocols.SoapException e) {
log.Debug("ErrorMessage: " + e.Message + " " + e.Detail.OuterXml + " Source: " + e.Source);
}
catch (Exception e) {
log.Debug(e.Message + "\r\n" + e.StackTrace);
}
return new Guid();
}
public void UploadFileToDB(Guid attachmentId, string filename, long filesize) {
string contentType = "application/octet-stream";
try {
Hashtable mimes = LoadMimeDB(Environment.SystemDirectory + "/Albaspectrum/ContentType.txt");
if (mimes != null) {
string tmpContentType = GetMimeType(mimes, filename);
if (tmpContentType != null && !tmpContentType.Equals(""))
contentType = tmpContentType;
}
byte[] memoryData = new byte[filesize];
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
reader.Read(memoryData, 0, (int)filesize);
reader.Close();
fs.Close();
OleDbCommand command = conn.CreateCommand();
command.CommandText = "UPDATE ActivityMimeAttachment SET FileSize = (?), MimeType = (?), FileName = (?), Body = (?) WHERE ActivityMimeAttachmentId = (?)";
command.Prepare();
command.Parameters.Add(new OleDbParameter("FileSize", filesize));
command.Parameters.Add(new OleDbParameter("MimeType", contentType));
command.Parameters.Add(new OleDbParameter("FileName", new FileInfo(filename).Name));
command.Parameters.Add(new OleDbParameter("Body", Convert.ToBase64String(memoryData, 0, (int)filesize)));
command.Parameters.Add(new OleDbParameter("ActivityMimeAttachmentId", attachmentId));
log.Debug("Prepare to upload attachemnt " + attachmentId.ToString("B") + " in ActivityMimeAttachment");
command.ExecuteNonQuery();
memoryData = null;
}
catch (Exception e) {
log.Debug(e.Message + "\r\n" + e.StackTrace);
}
}
asc application/pgp-encrypted Armored Encrypted file (PGP)
asd application/astound Autosave file (Word for Windows)
asm PC ASM File
asn application/astound
etc.
Boris Makushkin, Grupo Alba Spectrum Technology ( http://www.albaspectrum.com http://www.ronix-systems.de ) - SAP Business One, Oracle: Oracle E-Business Suite, Oracle Financials, Microsoft Business Solutions: Microsoft Great Plains (Microsoft Dynamics GP), Microsoft CRM (Microsoft Dynamics CRM), Microsoft Navision (Microsoft Dynamics NAV), Microsoft Axapta (Microsoft Dynamics AX). Atendemos clientes em São Paulo, Rio de Janeiro, Salvador, Porto Alegre, Curitiba, Belo Horizonte, Recife, Manaus e demais cidades do Brasil e o resto do mundo.
Alba Spectrum Group
help@albaspectrum.com skype: albaspectrum