by Gustaf Westerlund | Jan 30, 2007
In Microsoft CRM there are several methods to create great looking documents with mailmerge and the crm emails can be created with dynamic data. However, it is quite complicated to create automatic mails with data from one main entity and several sub entities. The most common example being an order confirmation, with data from both the order head and the order detail lines.
To make it a bit flexible, I created a function that could be called from the workflow engine.
Here is the method declaration that I will use bellow:
public string SendMailWithReport(System.Guid OrderId, string subject, string body, string reportpath, string callerXml)
The parameters are as follows:
OrderId – A Guid containing the orderid to be used as a parameter to the report.
Subject – a string that will contain the mail subject.
Body – a string that will contain the mail body
Reportpath – to make it a bit more flexible, the report path is not hard coded but can be inputed as a parameter,
callerXml – standard handling for getting the caller data to enable impersonation.
The first thing we want to do is to get the pdf from the report server.
We’ll store the binary pdf in the byte-array called result. When this is done, we’ll encode this into a string called encoded data. The rest is stuff that is needed to make this happen.
string encodedData = “”;
//Create Report PDF
ReportingService rs = new ReportingService();
rs.Credentials = new System.Net.NetworkCredential(username, passwd, domain);
Byte[] result;
string encoding;
string mimetype;
ParameterValue[] parametersUsed;
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = “CRM_OrderId”;
parameters[0].Value = “{” + OrderId.ToString() + “}”;
Warning[] warnings;
string[] streamids;
result = rs.Render(reportpath,
“PDF”,
null,
null,
parameters,
null,
null,
out encoding,
out mimetype,
out parametersUsed,
out warnings,
out streamids);
encodedData = System.Convert.ToBase64String(result);
The most complex part of this which took me the most time, was the “Render” method of the reporting server web service. I use it this way because it works, don’t ask me what all the parameters really are, I don’t know.
The next part is to create the CRM mail. This is quite straight forward, if you are used to the crm web service.
The “from” and “to” can be set to anything that can be used as a “to” or “from” in the CRM GUI.
CrmService service = new CrmService();
service.CallerIdValue = new CallerId();
service.CallerIdValue.CallerGuid = GetCaller(callerXml);
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
//Get current user
WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse user
= (WhoAmIResponse) service.Execute(userRequest);
//Load salesorder and account objects.
salesorder so = (salesorder)service.Retrieve(EntityName.salesorder.ToString(), OrderId, new AllColumns());
account acc = (account)service.Retrieve(EntityName.account.ToString(), so.customerid.Value, new AllColumns());
email em = new email();
activityparty from = new activityparty();
from.partyid = new Lookup();
from.partyid.type = EntityName.systemuser.ToString();
from.partyid.Value = user.UserId;
em.from = new activityparty[] {from};
activityparty toparty = new activityparty();
toparty.partyid = new Lookup();
toparty.partyid.type = EntityName.account.ToString();
toparty.partyid.Value = acc.accountid.Value;
em.to = new activityparty[] {toparty};
em.subject = subject;
em.sender = “test@test.com”;
em.regardingobjectid = new Lookup();
em.regardingobjectid.type = EntityName.salesorder.ToString();
em.regardingobjectid.Value = so.salesorderid.Value;
em.description = body;
em.ownerid = new Owner();
em.ownerid.type = EntityName.systemuser.ToString();
em.ownerid.Value = user.UserId;
Guid createdEmailGuid = service.Create(em);
Now, we have created the email. You can see it in CRM if you like.
The last part is now to create the attachment on the email as the pdf that we downloaded from the report server in the first part of this walk-through.
activitymimeattachment ama = new activitymimeattachment();
ama.activityid = new Lookup();
ama.activityid.type = EntityName.email.ToString();
ama.activityid.Value = createdEmailGuid;
ama.body = ” “;
ama.mimetype = “application/pdf”;
ama.attachmentnumber = new CrmNumber();
ama.attachmentnumber.Value = 1;
ama.filename = “A filename”;
Guid createdAttachment = service.Create(ama);
//Upload file
// Create the Request Object
UploadFromBase64DataActivityMimeAttachmentRequest upload = new UploadFromBase64DataActivityMimeAttachmentRequest();
// Set the Request Object’s Properties
upload.ActivityMimeAttachmentId = createdAttachment;
upload.FileName = “attachmentfilename.pdf”;
upload.MimeType = “application/pdf”;
upload.Base64Data = encodedData;
// Execute the Request
UploadFromBase64DataActivityMimeAttachmentResponse uploaded = (UploadFromBase64DataActivityMimeAttachmentResponse) service.Execute(upload);
The last part, if you want to, is to send the mail:
SendEmailRequest req = new SendEmailRequest();
req.EmailId = createdEmailGuid;
req.TrackingToken = “”;
req.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)service.Execute(req);
I have made some simplifications, like removing try-catch clauses, which you really should use, but the code has apart from that been cut-n-pasted from a working application and should work.
Gustaf Westerlund
CRM and SharePoint Consultant
Humandata AB
www.humandata.se
by Gustaf Westerlund | Jan 25, 2007
I recieved this question by email from one of the readers of this blog and thought that I might aswell let all of you in on the answer. (I’ve take the liberty to remove his name and to correct a few typos).
—
Hi Gustaf,
Let me introduce myself, I am XXX new to MS CRM. I got your mail id through your blog.I am mailing you as we have a urgent query with respect to mscrm. We have a company domain, which is being used by the entire organisation, now if i am going to install MS CRM 3.0 Server setup do we need to have a different domain/domain controllers for the CRM. Are their any issues which must be taken care of before going ahead, as we will be dealing with the live domain. The objective of this is to integrate with sharepoint technologies. I will be thankful for your earlier response.
Regards, XXX
—- My answer:
Hi,
When you install MS CRM 3 it will add a few groups to the AD, but apart from that will not touch the AD. MS CRM:s user handling and security information is stored in the MS CRM database hence no need for further schema extensions of the AD.
I understand your concern when installing on a live domain but there should not be any problems. If you still feel a bit worried, you can always install MS CRM in a virtual environment and see the changes for you self.
The live CRM server should be installed in the company domain.
Kind regards,
Gustaf
——
If your question is in regard to something I’ve posted on the blog, please add it as a comment, otherwise, you can always mail me. Please note that I will gladly answer technical questions like the one above but it is not feasable for me to design you solution. If you want my help in that aspect, please contact Humandata (www.humandata.se) and we can get you a good deal. I hope you appreciate that even I need to have a house to live in and food for my family 🙂
Gustaf Westerlund
CRM and SharePoint Consultant
Humandata AB
www.humandata.se
by Gustaf Westerlund | Jan 25, 2007
Well, as some of you might have noticed, not much has happened on this blog for the last 3 weeks which has been due to the fact that I’ve been on my honeymoon in Thailand and frankly didn’t start CRM och SharePoint even once :).
I’ve tried to find out a bit more about the upcoming integration engine CRM-Nav from Microsoft but have not been very successful. The main question is what kind of infrastructure it will be based on and I think the following three are the hottest tips:
1. Celenia/Tectura – The integration formerly known as Tectura, (see my previous postings), now called the Celenia CRM-Nav integration, has previously been put forward by Microsoft as THE integration engine to use.
2. BizTalk based integration – BizTalk is the talk of the town concerning integration, and it would not be very strange if the integration would be BizTalk based.
3. New/Custom – They might just have sat down and started from scratch and written an integraiton engine specifically for CRM-Nav.
Well, where’s my money? To start off, Microsoft have been very very quite about this integration engine, would seem a bit strange if the engine would be according to 2 or 3 above, when there really would be no reason for that.
However, if they are up to an aquisition of Celenia or just the program, then they would probably like to be just very very quite about it until all the formalities had been settled.
Another reason for not going along with 2, the BizTalk version, is that it would require an installation of BizTalk at each customer which might cause more problems and costs than what is reasonable.
One reason for not choosing 3 is that it most probably will be quite costly.
Something to take into consideration in the Celenia business is that the integration engine was previously owned by Tectura (although it had been developed by Celenia) but for reasons unknown to me, was reverted to Celenia. Most probably after considerable preasure from Microsoft.
Bottom line, my money is on the Celenia integration (1). Which versions of Nav it will support is still unknown and if it will still only be a technical integration is also still unknown, when I learn more, I will let you know.
Gustaf Westerlund
CRM and SharePoint Consultant
Humandata AB
www.humandata.se
by Gustaf Westerlund | Dec 18, 2006
This is a posting directed at all swedish speaking readers!
If you havn’t already been there, check out the swedish forum called IT-Proffs, it has (as far as I know) the only Swedish CRM forum. You will find me and several other talented and skilled CRM-professionals there like Jonas Deibe (Microsoft Sweden).
Apart from the global CRM-forum/newsgroup hosted by Microsoft, this is a bit smaller which makes it easier to handle, and doesn’t flood you inbox.
Please note that you can set an automatic notification when a new messsage is added to the CRM-Forum. I hope to see you there soon! www.itproffs.se
Gustaf Westerlund
CRM and SharePoint Consultant
Humandata AB
www.humandata.se
by Gustaf Westerlund | Dec 15, 2006
During the latest project i was involved in, we wanted to show SharePoint v3 pages in Outlook using sitemap customizations in CRM. This worked great at first (we didn’t really test it), but after a while, when we started using it a bit more we sometimes got the following error message “MSO.DLL is not compatible with Outlook”, ending with an OK-button. Pressing it, made Outlook exit in error. Not very nice.
I don’t know exactly why this happened but I think it happens due to the loading of some active-X components that are not supported by the web browser in Outlook, which for some reason doesn’t seem to be IE. If anyone knows anything more about this, please let me know.
Oh, I forgot, we were using Outlook 2003 since V3C isn’t publicly released yet. Perhaps you won’t get this error if using Outlook 2007 since it is probably more compatible with SharePoint v3.
Gustaf Westerlund
CRM and SharePoint Consultant
Humandata AB
www.humandata.se
Recent Comments