Microsoft CRM Customization ? Programming Closed Email Activity

Microsoft CRM is CRM answer from Microsoft and attempt to get market share from Siebel, Oracle and others traditional Client Relationship Management System vendors. Microsoft CRM uses all the spectrum of Microsoft recent technologies: .Net, MS Exchange, MS Outlook, MS SQL Server, Replication, Indexing, Active Directory, Windows 2000/2003 security model, C#, VB.Net, HTML, XML Web Service, XLTP, Javascript to name a few.

Today's topic is Activity of email type programming - you usually deal with these customizations when you improve Microsoft Exchange CRM connector. How do you create closed activity - this is the main discussion topic. We'll use C#.Net coding

One of the roles of our Exchange Event Handler/Sink is creation MS CRM Closed Activity in handling incoming and outgoing email messages. The interaction with Microsoft CRM uses two approached ? using MS CRM SDK (handling inbound and outbound XML messages) and via direct access to MS CRM Database. Let's first look at the Closed Activity creation algorithm:

1. First we need to understand the entity we need to create activity for: Account, Lead or Contact. The selection should use specific criteria ? in our case this is email address:

if ((crmAccount = crmConnector.GetAccount(mailboxFrom)) != null) {

}

else if ((crmContact = crmConnector.GetContact(mailboxFrom)) != null) {

}

else if ((crmLead = crmConnector.GetLead(mailboxFrom)) != null) {

}

2. Then we have to get GUID of MS CRM user, who owns this entity, C# code like this:

crmUser = crmConnector.GetUser(crmAccount.GetOwnerId());

3. Next step is closed Activity creation:

emailId = crmConnector.CreateEmailActivity(

crmUser.GetId(),

Microsoft.Crm.Platform.Types.ObjectTy pe.otAccount, crmAccount.GetId(),

Microsoft.Crm.Platform.Types.ObjectType.otSystemUser, crmUser.GetId(),

crmAccount.GetEmailAddress(), crmUser.GetEmailAddress(), sSubject, sBody);

4. The method to create closed activity:

public Guid CreateEmailActivity(Guid userId, int fromObjectType, Guid fromObjectId, int toObjectType, Guid toObjectId, string mailFrom, string mailTo, string subject, string body) {

try {

log.Debug("Prepare for Mail Activity 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();

// CRMEmail proxy object

Microsoft.Crm.Platform.Proxy.CRMEmail email = new Microsoft.Crm.Platform.Proxy.CRMEmail();

email.Credentials = credentials;

email.Url = crmDir + "CRMEmail.srf";

// Set up the XML string for the activity

string strActivityXml = "";

strActivityXml += "";

strActivityXml += "") + "]]>";

strActivityXml += "";

strActivityXml += userId.ToString("B") + "";

strActivityXml += "";

// Set up the XML string for the activity parties

string strPartiesXml = "";

strPartiesXml += "";

strPartiesXml += "" + mailTo + "";

if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otSystemUser) {

strPartiesXml += "" + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() + "";

}

else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otAccount) {

strPartiesXml += "" + Microsoft.Crm.Platform.Types.ObjectType.otAccount.ToString() + "";

}

else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otContact) {

strPartiesXml += "" + Microsoft.Crm.Platform.Types.ObjectType.otContact.ToString() + "";

}

else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otLead) {

strPartiesXml += "" + Microsoft.Crm.Platform.Types.ObjectType.otLead.ToString() + "";

}

strPartiesXml += ""+ toObjectId.ToString("B") + "";

strPartiesXml += "";

strPartiesXml += Microsoft.Crm.Platform.Types.ACTIVITY_PARTY_TYPE.ACTIVITY_PARTY_TO_RECIPIENT.ToString();

strPa rtiesXml += "";

strPartiesXml += "";

strPartiesXml += "";

strPartiesXml += "" + mailFrom + "";

if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otSystemUser) {

strPartiesXml += "" + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() + "";

}

else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otAccount) {

strPartiesXml += "" + Microsoft.Crm.Platform.Types.ObjectType.otAccount.ToString() + "";

}

else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otContact) {

strPartiesXml += "" + Microsoft.Crm.Platform.Types.ObjectType.otContact.ToString() + "";

}

else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otLead) {

strPartiesXml += "" + Microsoft.Crm.Platform.Types.ObjectType.otLead.ToString() + "";

}

strPartiesXml += ""+ fromObjectId.ToString("B") + "";

strPartiesXml += "";

strPartiesXml += Microsoft.Crm.Platform.Types.ACTIVITY_PARTY_TYPE.ACTIVITY_PARTY_SENDER.ToString();

strPartiesX ml += "";

strPartiesXml += "";

strPartiesXml += "";

log.Debug(strPartiesXml);

// Create the e-mail object

Guid emailId = new Guid(email.Create(userAuth, strActivityXml, strPartiesXml));

return emailId;

}

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 + " " + e.StackTrace);

}

return new Guid();

}

5. To make the activity just created be shown correctly you need to setup it's flags according to MS CRM standards:

public void UpdateActivityCodes(Guid emailId) {

try {

OleDbCommand command = conn.CreateCommand();

command.CommandText = "UPDATE ActivityBase SET DirectionCode = (?), StateCode = (?), PriorityCode = (?) WHERE ActivityId = (?)";

command.Prepare();

command.Parameters.Add(new OleDbParameter("DirectionCode", Microsoft.Crm.Platform.Types.EVENT_DIRECTION.ED_INCOMING));

command.Parameters.Add(new OleDbParameter("StateCode", Microsoft.Crm.Platform.Types.ACTIVITY_STATE.ACTS_CLOSED));

command.Parameters.Add(new OleDbParameter("PriorityCode", Microsoft.Crm.Platform.Types.PRIORITY_CODE.PC_MEDIUM));

command.Parameters.Add(new OleDbParameter("ActivityId", emailId));

log.Debug("Prepare to update activity code " + emailId.ToString("B") + " in ActivityBase");

command.ExecuteNonQuery();

}

catch(Exception e) {

log.Debug(e.Message + " " + e.StackTrace);

}

}

public void UpdateActivityQueueCodes(Guid emailId, Guid queueId) {

try {

OleDbCommand command = conn.CreateCommand();

command.CommandText = "UPDATE QueueItemBase SET Priority = (?), State = (?), QueueId = (?) WHERE ObjectId = (?)";

command.Prepare();

command.Parameters.Add(new OleDbParameter("Priority", Microsoft.Crm.Platform.Types.PRIORITY_CODE.PC_MEDIUM));

command.Parameters.Add(new OleDbParameter("State", Microsoft.Crm.Platform.Types.ACTIVITY_STATE.ACTS_CLOSED));

command.Parameters.Add(new OleDbParameter("QueueId", queueId));

command.Parameters.Add(new OleDbParameter("ObjectId", emailId));

log.Debug("Prepare to update activity queue code " + emailId.ToString("B") + " in QueueItemBase");

command.ExecuteNonQuery();

}

catch(Exception e) {

log.Debug(e.Message + " " + e.StackTrace);

}

}

Happy customizing, implementing and modifying! If you want us to do the job - give us a call 1-866-528-0577! help@albaspectrum.com

About The Author

Boris Makushkin is Lead Software Developer in Alba Spectrum Technologies ? USA nationwide Microsoft CRM, Microsoft Great Plains customization company, based in Chicago, Boston, San Francisco, San Diego, Los Angeles, Houston, Dallas, Atlanta, Miami, Montreal, Toronto, Vancouver, Madrid, Moscow, Europe and internationally (help@albaspectrum.com), he is Microsoft CRM SDK, C#, VB.Net, SQL, Oracle, Unix developer. Boris can be reached: 1-866-528-0577 or help@albaspectrum.com.

help@albaspectrum.com

recurring maid service Highland Park ...
In The News:

Amazon's Alexa for Shopping now lets customers verify suspicious emails, texts and phone calls against the company's own communication records.
Lindsey Brooke Isaacs spent 13 days in jail after a Flock camera flagged her Dodge Durango near a fatal Florida crash that killed three people.
AI-powered smart grocery carts are linked to 32% higher spending, according to new research, as Instacart Caper Carts expand across U.S. stores.
A VPN encrypts your connection on hotel, airport and café Wi-Fi abroad, but streaming catalogs and live sports access still depend on provider rules.
Gmail's Verified Sender Program launches September 8, 2026, giving U.S. political committees a more reliable path to your personal email inbox.
Elroy Air's Chaparral can carry over 500 pounds and fly 450 miles without a runway, and the FAA just tested it under a new federal program.
Bedrock Robotics says its autonomous excavators are doing paid earthwork on active job sites in Texas and Nevada with no operator in the cab.
A dedicated banking computer isolates your financial accounts from malware and phishing, but passwords, SIM swapping and outdated software remain risks.
The OlloNi SS1 AI robot pet uses a 4K camera and facial recognition to learn your family, but its removable Red Heart memory module raises questions.
Nine identity theft warning signs range from unfamiliar credit inquiries to SIM swap attacks that can quietly drain your bank account funds.
Robot.com's R-dog uses articulated legs and wheels to climb stairs and deliver packages, while its built-in screens display paid ads along the route.
MyChart phishing emails mimic real patient portal alerts with fake lab results and malware tricks that target Windows users across the country.
The Fox News AI Newsletter covers the latest artificial intelligence technology advancements, including the challenges and opportunities AI presents now and for the future.
Google's Motion Assist places animated dots on Pixel screens to combat motion sickness in vehicles, but the Android 17 rollout remains limited.
Moving to a 55+ community can quickly expose your data to scammers and data brokers before you even unpack, putting your whole family at risk.
Amazon Prime Air drone delivery plans to reach nearly 500 U.S. cities by the end of 2026, but your yard and ZIP code may determine eligibility.
The FBI reports nearly $21 billion in cybercrime losses for 2025, up 26%, as AI scams make fake voices and identities more convincing than ever.
Uber teen accounts now feature live video streaming from the driver's camera, but encrypted recordings stay on Uber's servers for up to 14 days.
Router firewalls and software firewalls serve different roles in protecting your home Wi-Fi from unwanted traffic and suspicious connections.
A Chrome extension with 70,000 users and a 4.7-star rating began injecting fake update warnings after a threat actor acquired and weaponized it.
Amazon's Alexa+ rolls out free to Fire TV devices across the U.S., but hands-free microphones and stored voice data raise key privacy questions.
Meta's new teen accounts settlement sets a default two-hour daily limit on Instagram and Facebook, plus night mode and school mode restrictions.
The CareCloud data breach exposed Social Security numbers, banking information and medical records belonging to more than 3.75 million people.
Waymo reveals its custom 5-nanometer chip and dual computing systems designed to take over instantly if the robotaxi's primary computer fails.
Identity theft now includes account takeover, phone hijacking and payment card exposure. Here's what to look for in identity theft protection services.

What to Do if All Screensavers Fun is Grayed Out?

Finally, you have some time to personalize your desktop with... Read More

Spyware, What is it?

Spyware, what it is and what it does. Basically, spyware... Read More

Twelve Things You Should Know to Save on Computer Software

Do you want to get quality software at a reasonable... Read More

Linux for Home Users

Hey Guys! Don't raise your eyebrows or fear by hearing... Read More

Does your Company have Documentum?

Are you lost in the mess of documents that get... Read More

Databases ? How We Love to Hate Them!

You've finally created databases that you can actually use to... Read More

Free Software for Newbies and Web Developers

Here is some free software tools to help you build... Read More

Review on QuarkXpress 6.0

After almost two decades of existence, Quark has become the... Read More

Things You Can Do With Windows XP!

Did you ever give a thought to the number of... Read More

Microsoft Great Plains Upgrade ? Version 8.0 Overview for IT Director/Controller

If you have Microsoft Great Plains as main accounting and... Read More

SSH (SCP) Send Files from Windows to Your Linux Box

Not all of us have the luxury of working both... Read More

Microsoft Great Plains Jewelry ? Implementation & Customization Overview

Each Industry and market niche has business specific and unique... Read More

Great Plains Dynamics/eEnterprise Upgrade ? Things to Consider and FAQ

If you have Great Plains Dynamics/eEnterprise (version 6.0 or earlier)... Read More

Microsoft Great Plains, Navision, Axapta ? Selection Considerations

During the years of our consulting practice, which comes back... Read More

Best Practices In Choosing Network Monitoring Software

Blue Cross and Blue Shield of Hawaii (HMSA) found itself... Read More

Microsoft Great Plains Subcontracting ? Overview for Microsoft Business Solutions Partner

Microsoft Business Solutions Great Plains is very popular ERP/MRP applications... Read More

How to Tell You Have Spyware, Ad-ware or Viruses

Usually, the easiest way to tell you have spyware is... Read More

How to Evaluate Staffing Software

If you are in the market for new staffing software,... Read More

Microsoft Axapta, Navision or Great Plains: ERP Selection for Large Corporation

If you would like to pick something from Microsoft, or... Read More

Scrap Booking Online: Word Perfect or Corel Graphics Suite?

Scrapbooks are very popular these days. I think that almost... Read More

Microsoft Great Plains: Offshore Customization & Development ? Overview for Consultant

When you visit department stores and see that majority of... Read More

The Software 2005 Conference - A Review

The Software 2005 conference is now a wrap. This conference,... Read More

Why do Manufacturers Invest in Business Management Software?

With many manufacturing shops heading over seas in favor of... Read More

Alien Intruders!

You probably didn't casually invite, or extend a formal attendance... Read More

Microsoft Great Plains Implementation for Large Corporation ? Overview for VP IT

Microsoft Business Solutions Great Plains was historically designed to serve... Read More

move out cleaning service Wilmette ...