C# Code for Sending Email to Unresolved Recipients

This code will send email to unresolved recipient.
private void SendEmailToUnresolvedRecent(IOrganizationService prmCrmService, string 
             prmToRecipientEmailAddress, Guid prmSenderUserId, string prmSubject, string prmMessageBody)
        {

            // Email record id
            Guid wod_EmailId = Guid.Empty;

            // Creating Email 'to' recipient activity party entity object
            Entity wod_EmailToReciepent = new Entity("activityparty");

            // Creating Email 'from' recipient activity party entity object
            Entity wod_EmailFromReciepent = new Entity("activityparty");

            // Assigning receiver email address to activity party addressused attribute
            //wod_EmailToReciepent["participationtypemask"] = new OptionSetValue(0);
            wod_EmailToReciepent["addressused"] = prmToRecipientEmailAddress;

            // Setting from user account
            wod_EmailFromReciepent["partyid"] = new EntityReference("systemuser", prmSenderUserId);

            // Creating Email entity object
            Entity wod_EmailEntity = new Entity("email");

            // Setting email entity 'to' attribute value
            wod_EmailEntity["to"] = new Entity[] { wod_EmailToReciepent };

            // Setting email entity 'from' attribute value
            wod_EmailEntity["from"] = new Entity[] { wod_EmailFromReciepent };

            // Setting email subject and description
            wod_EmailEntity["subject"] = prmSubject;

            wod_EmailEntity["description"] = prmMessageBody;

            // Creating email record
            wod_EmailId = prmCrmService.Create(wod_EmailEntity);

            // Creating SendEmailRequest object for sending email
            SendEmailRequest wod_SendEmailRequest = new SendEmailRequest();

            // Creating Email tracking token request object
            GetTrackingTokenEmailRequest wod_GetTrackingTokenEmailRequest = new GetTrackingTokenEmailRequest();

            // Creating Email tracking token response object to get tracking token value
            GetTrackingTokenEmailResponse wod_GetTrackingTokenEmailResponse = null;

            // Setting email record if for sending email
            wod_SendEmailRequest.EmailId = wod_EmailId;

            wod_SendEmailRequest.IssueSend = true;

            // Getting tracking token value
            wod_GetTrackingTokenEmailResponse = (GetTrackingTokenEmailResponse)
                                                 prmCrmService.Execute (wod_GetTrackingTokenEmailRequest);

            // Setting tracking token value
            wod_SendEmailRequest.TrackingToken = wod_GetTrackingTokenEmailResponse.TrackingToken;

            // Sending email
            prmCrmService.Execute(wod_SendEmailRequest);

        }

Create a button on the CRM 4.0

I'll show you the example of Appeal (Case-incident) how to create a button on the form and hang on click function.

ms-crm-create-button-alert

button on the CRM-form

Add to the essence of the new Case attribute named new_button, submit it to a form, the properties of the field new_button remove the check mark Display label on the form, save and publish.

Open the OnLoad event of form and paste the following script:

 
/* Jscript */



/ / The button
crmForm.all.new_button.DataValue = «Button»;
crmForm.all.new_button.style.textAlign = "center";
crmForm.all.new_button.vAlign = "Middle";
/ / styles
crmForm.all.new_button.style.cursor = "Hand";
crmForm.all.new_button.style . backgroundColor = "# CADFFC";
crmForm.all.new_button.style.color = "# 000000";
crmForm.all.new_button.style.borderColor = "# 330066";
crmForm.all.new_button.style.fontWeight = "bold ";
crmForm.all.new_button.contentEditable = false;
/ / change color when the mouse



changeC1 function () {
crmForm.all.new_button.style.color = "000099";
}
changeC2 function () {
crmForm.all.new_button.style.color = "000000";
}
changeC3 function () {
crmForm.all.new_button.style.backgroundColor = "# 6699FF";
}
changeC4 function () {
crmForm.all.new_button.style.backgroundColor = "CADFFC";
}
/ / when you click on the button call the TestTheButton
crmForm.all.new_button.attachEvent ("onclick", TestTheButton);
function TestTheButton ()
{Alert (":)");
}

Follow a lead from creation through closure

A lead record in Microsoft Dynamics CRM Online represents a potential customer who must be qualified or disqualified as a sales opportunity. You can use leads to manage potential sales from the point at which you become aware of a customer's interest through the successful sale.

The following diagram illustrates the ways that you can create a lead and convert it to several different record types.

Lead diagram

There are several ways that you can create a lead in Microsoft Dynamics CRM Online:

After you create the lead, you can convert it into any of the following three record types:

  • Account
  • Contact
  • Opportunity

When you convert the lead to an opportunity, you can also choose to link the new opportunity to the new accounts or contacts you may have created, or to an existing account or contact in your Microsoft Dynamics CRM Online database.

Follow an opportunity from creation through closure

An opportunity is a potential sale or possible revenue from an account or contact.

The following diagram illustrates the ways that you can create an opportunity and close it when the potential customer decides whether to move forward with the sale.

Opportunity diagram

There are several ways that you can create an opportunity in Microsoft Dynamics CRM:

When you know whether or not the customer is going to move forward with the sale, you can close the opportunity with a status of either Won or Lost.

Walkthrough: Using the Discovery Service with Active Directory Authentication

[Applies to: Microsoft Dynamics CRM 4.0]

This walkthrough demonstrates how to use the Discovery Web service to find the correct CrmService Web service endpoint for your organization. This is for an on-premise installation of Microsoft Dynamics CRM. For more information on the Discovery Web service, see Web Services: CrmDiscoveryService.

During this walkthrough you will learn how to do the following:

  • Use Microsoft Visual Studio 2005 to create a console application that uses the Microsoft Dynamics CRM Web services.
  • Use Active Directory authentication for interacting with the Microsoft Dynamics CRM Web services.

This walkthrough utilizes Microsoft Visual C# sample code only. However, a Microsoft Visual Basic .NET version of the code can be found at SDK\Walkthroughs\Authentication\VB\ActiveDirectory.

Prerequisites

In order to complete this walkthrough, you will need the following:

  • Access to a Microsoft Dynamics CRM 4.0 server.
  • A Microsoft Dynamics CRM system account.
  • Visual Studio 2005.

Creating a Visual Studio 2005 Solution

You will use a Visual Studio 2005 solution to build your project code.

To create a Visual Studio 2005 solution

1. In Microsoft Visual Studio 2005, on the File menu, point to New, and then click Project to open the New Project dialog box.

2. In the Project types pane, select Visual C#.

3. In the Templates pane, click Console Application.

4. Type a name for your project and then click OK.

While this walkthrough only shows the Visual C# code for the project, a VB.NET version of the code can be found in the SDK\Walkthroughs\Authentication\VB\ActiveDirectory folder.

Adding Web References

You need to add Web references to the required Microsoft Dynamics CRM Web services. By adding these Web references, you are making the Web service proxy namespaces accessible to your project.

To add the CrmDiscoveryService Web service reference

1. In the Solution Explorer window, right-click your project name and choose Add Web Reference.

2. In the Add Web Reference wizard, type the URL for the CrmDiscoveryService Web service in the URL box, using the name and port number for your Microsoft Dynamics CRM server, and then click Go. For example:

3. http://<servername:port>/mscrmservices/2007/AD/CrmDiscoveryService.asmx

4. When the CrmDiscoveryService Web service is found, change the text in the Web reference name box to CrmSdk.Discovery and then click Add Reference.

To add the CrmService Web service reference

1. In the Solution Explorer window, right-click your project name and choose Add Web Reference.

2. In the Add Web Reference wizard, type the URL for the CrmService Web service in the URL box, using the name and port number for your Microsoft Dynamics CRM server, and then click Go. For example:

3. http://<servername:port>/mscrmservices/2007/CrmServiceWsdl.aspx

4. When the CrmService Web service is found, change the text in the Web reference name box to CrmSdk and then click Add Reference.

Note that you can name the Web references any name that you like, but for this example they have been named CrmSdk and CrmSdk.Discovery.

Accessing Microsoft Dynamics CRM Web Services

To access the Microsoft Dynamics CRM Web services and work with business entities, your code typically includes these kinds of program statements:

  • Using statements, which provide access to the required namespaces.
  • Code that instantiates the CrmDiscoveryService and CrmService Web service proxies.
  • Instantiation of Microsoft Dynamics types.
  • Invocation of Web service methods.

To include the required .NET namespaces

Add the following lines of code above the namespace statement in your project:

[C#]

 using System.Web.Services.Protocols;
using System.Xml;

To include the required Microsoft Dynamics CRM Web service namespaces

Add the following lines of code after the namespace statement and before the class statement:

// Import the Microsoft Dynamics CRM namespaces.
using CrmSdk;
using CrmSdk.Discovery;
// This class is found in Microsoft.Crm.Sdk.dll. You can add a reference
// to the DLL and remove this class definition if you like.
public sealed class AuthenticationType
{
public const int AD = 0;
public const int Passport = 1;
public const int Spla = 2;
}

This code also adds an AuthenticationType class that is used later in the sample code.

To add configuration information to your solution

Add the following code after the class statement and before the Main method. This code sets up the necessary variables such as organization name. Be sure to fill in the correct values for the server name, TCP port, and organization of your Microsoft Dynamics CRM installation.

[C#]



// The following configuration data is site specific.
// TODO: Set the name and TCP port of the server hosting Microsoft Dynamics CRM.
static private string _hostname = "localhost";
static private string _port = "80";

// TODO: Set the target organization.
static private string _organization = "AdventureWorksCycle";

#endregion Configuration data

// Expired authentication ticket error code. The error codes can be found in the
// SDK documentation at Server Programming Guide\Programming Reference\Error Codes.
static private string ExpiredAuthTicket = "8004A101";





To add error handling to your solution

Add the following code within the Main method. This code will provide some console output you can use to verify that your program is working.

[C#]




try
{
Run();
Console.WriteLine("Authentication was successfull.");
}
catch (System.Exception ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine(ex.Message);

// Display the details of the inner exception.
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);

SoapException se = ex.InnerException as SoapException;
if (se != null)
Console.WriteLine(se.Detail.InnerText);
}
}
finally
{
Console.WriteLine("Press to exit.");
Console.ReadLine();
}




Your program will not build because the Run method does not exist. You will create this next.

To add the Run method

This method contains all the code needed to use the Discovery service to obtain the correct URL of the CrmService Web service for your organization. The code then sends a WhoAmI request to the service to verify that the user has been successfully authenticated.

Add the following code after the Main method.

[C#]



public static bool Run()
{
try
{
// STEP 1: Instantiate and configure the CrmDiscoveryService Web service.
CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.UseDefaultCredentials = true;
discoveryService.Url = String.Format(
"http://{0}:{1}/MSCRMServices/2007/{2}/CrmDiscoveryService.asmx",
_hostname, _port, "AD");

// STEP 2: Retrieve the organization name and endpoint Url from the
// CrmDiscoveryService Web service.
RetrieveOrganizationsRequest orgRequest =
new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse orgResponse =
(RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

OrganizationDetail orgInfo = null;

foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
{
if (orgDetail.OrganizationName.Equals(_organization))
{
orgInfo = orgDetail;
break;
}
}

if (orgInfo == null)
throw new Exception("The organization name is invalid.");

// STEP 3: Create and configure an instance of the CrmService Web service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.AD;
token.OrganizationName = orgInfo.OrganizationName;

CrmService crmService = new CrmService();
crmService.Url = orgInfo.CrmServiceUrl;
crmService.CrmAuthenticationTokenValue = token;
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;

// STEP 4: Invoke CrmService Web service methods.
WhoAmIRequest whoRequest = new WhoAmIRequest();
WhoAmIResponse whoResponse = (WhoAmIResponse)crmService.Execute(whoRequest);

return true;
}

// Handle any Web service exceptions that might be thrown.
catch (SoapException ex)
{
throw new Exception("An error occurred while attempting to authenticate.", ex);
}
}





To add the GetErrorCode method

Add the following code, after the Run method, for the GetErrorCode method that extracts a numeric error code from a SoapException stored in anXmlNode or returns an empty string if no error exists.

[C#]



private static string GetErrorCode(XmlNode errorInfo)
{
XmlNode code = errorInfo.SelectSingleNode("//code");

if (code != null)
return code.InnerText;
else
return "";
}




Now build and run your solution by pressing F5. If you get any compile errors, you can find the complete code sample in the SDK\Walkthroughs\Authentication\CS|VB\ActiveDirectory folder.