Microsoft Dynamics CRM 2015 Certifications list

MB2-704: Microsoft Dynamics CRM Application

MB2-706: Microsoft Dynamics CRM Online Deployment

MB2-707: Microsoft Dynamics CRM Customization and Configuration

MB2-708: Microsoft Dynamics CRM Installation

MB2-709: Microsoft Dynamics Marketing

How to Integrate Dynamics CRM and Share Point Online

Will demonstrate integration between CRM and Share Point in Online 30 days Trial Version.

Please follow the below steps,

Step 1: If you have existing CRM Trial 2013 or 2015 Online, then open Office 365 Login and provide your credentials.

If not, create 30 days CRM Trial version. Click Here to create.

Step 2: Now will extend CRM Trial with Office 365 Enterprise E3 Trial, for Sharepoint with same Office account.

Click on PURCHASE SERVICES

59.2.Purchase Services

Step 3: You can see List of Enterprise plans, check for Office 365 Enterprise E3 and click on Trial.

Click on Learn more to see the benefits that offered with Office 365 Enterprise E3.

59.3.List of Licenses

 

Step 4: Click on try now for 30 day subscription.

59.4 Confirm your order

Step 5: Once Subscription is placed successfully, you can see the below order receipt. Click on continue.

 59.5.Order Receipt

Step 6: Will take some time to reflect all the benefits offered with Office 365 E3 Subscription.

Once it is activated, you can see the below in Office 365 Login.

Click on SharePoint.

59.6.Office365_SharePoint_Link

Step 7: You can see SharePoint admin center page, with list of URLs. Copy the first link as highlighted in the below screen.

Ex: https://provideyourorgname.sharepoint.com

59.7.SharePoint Admin Center

Step 8: We are done with SharePoint. Goto Office 365 admin center and click on CRM under Admin Tab.

59.8.CRM_Admin Center
Step 9: Click on OPEN.

59.9.CRM Online Administration Center

Step 10: Goto Microsoft Dynamics CRM –> Settings –> Document Management.

59.10.CRM_DoumentManagement

Step 11: Click on Enable Server-Based SharePoint Integration.

59.11.Enable Server-Based SharePoint Integration

Step 12: Click on Next

59.12.Enable Server-Based SharePoint Integration

Step 13: Select Online and Click on Next.

59.13.Enable Server-Based SharePoint Integration_Define Deployment

Step 14:  Provide SharePoint URL and Click Next.

59.14.Enable Server-Based SharePoint Integration_Prepare Sites

Step 15: Once the SharePoint URL is valid. You can see the Validation Status as Complete. Click on Enable.

59.15.Enable Server-Based SharePoint Integration_Validate Sites

Step 16: We have Succesfully Completed Server-Based SharePoint Integration.

Select Open Document Mangement Settings Wizard, and click on Finish.

59.16.Document Mangement Settings Wizard

Step 17: Select the required Entities that you want to store Documents, and provide SharePoint URL. Click Next.

59.17.Document Mangement Settings

Step 18: If the site URL is valid then you can see the below status. Select Based on entity to create folder structure based on each entity in SharePoint.

Click on Next.

59.18.Document Mangement Settings_Based On Entity

Step 19: Click on OK to create Document Libraries in SharePoint.

59.19.Document Mangement Creation

Step 20: Document Libraries created in SharePoint. Click on Finish to complete Integration process between SharePoint and CRM Online.

59.20.Document Mangement Settings

Step 21: Goto Account entity and open any existing account record to test SharePoint connectivity.

Goto Associated Entity Documents.

59.21.CRM Document Associated View in Account

Step 22: One you click on Documents, you can see the below message for the first time, stating that it will create a folder under Account à ArunPotti (account record) in SharePoint.

Click to Confirm.

59.22.Create Folder in Share Point

Step 23: Once Folder created successfully in SharePoint, you can see the below SharePoint Documents Grid.

Click on UPLOAD to upload any document.

59.23.1.Account - Share Point Document Associated View

Click on OPEN SHAREPOINT, to see the documents associated to Account (Arun Potti account record) in SharePoint.

Uploaded a Sample Record that we can see here.

59.23.2.Documents in Share Point

Step 24: Click on Site Contents to see the folders structure for each and every entity.

59.24.Share Point Site Contents

Hope you have successfully Integrated Microsoft Dynamics CRM and SharePoint online.

Thank you for reading this article :):):)

Please provide your valuable comments.

How to get Organization & Sql Server Database name in MS CRM using SQL Server

It is not mandatory to have the same CRM Organization Name (On-Premises) with same Database Name in Sql Server.

So to check which Oragnization pointing to the database can be known by using the below query

SELECT DatabaseName [DATABASE NAME],
FriendlyName [ORGANIZATION NAME],
SqlServerName [SQL SERVER NAME],
SrsUrl [SSRS URL],
[ORGANIZATION STATUS] =
CASE
WHEN STATE=1 THEN 'ENABLED'
WHEN STATE=0 THEN 'DISABLED'
END
FROM
[MSCRM_CONFIG].[DBO].[ORGANIZATION]

Please share your valuable feedback on this article.

Hide Quick Tour in CRM 2015 Online

When you open Online CRM for first time in a browser, we will see the below CRM Navigation/ Quick Tour Popup. If you click on Don’t show me this again also, this popup may appear again in a browser, if your browser cookies deleted.

Navigation Tour

If you want to hide Navigation/ Quick Tour permanently, there is a setting available.

Goto Settings –> Administration –> System Settings

Under General Tab, Find Set whether users see navigation tour option.

Select No for the option Display navigation tour to users when they sign in and click on OK.

System Settings

Close browser and open CRM to check this.

Please provide your valuable comments on this article.

How to import CRM Orgnization Using CRM SQL Server backup file

Will import CRM Organization using Existing SQL Server CRM Database backup file. Follow the below steps,

Step 1: Open SQL Server Management Studio. I am using SQL Server 2012 Enterprise Edition.

Step 2: Provide Server name, Authentication, User name and Password. Click on Connect.

Import CRM Organization - Open SQL

Step 3: Take Backup of the Required Organization before do this Operation. Click here to know how to take backup of MSCRM Organization database backup.

Step 4: Right Click on Databases and click on Restore Database.

Import CRM Organization - Click on Restore Database

Step 5: Follow the below points,

  1. Select Device.
  2. Click on (Browse) button.
  3. Click on Add.
  4. Browse bak file that you would like to create Org in MSCRM.
  5. Click OK and Ok.

Import CRM Organization - Locate BAK Data file

Step 6: Provide Destination Database Name that you would like to give for New CRM Organization and click ok.

Import CRM Organization - Provide Destination DB Name

Step 7: Once the database is restored, you can see the below Message and Click on OK.

Import CRM Organization - Restored Successfully

Step 8: Open CRM Deployment Manager and Click on Import Organization….

Import CRM Organization - Open Deployment Manager

Step 9: You can see the below SQL Server and Organization database name automatically. Click Next.

Import CRM Organization - Select Server and Organization Name

Step 10: Provide Display Name for the New CRM Organization. Automatically Unique Database Name will populate and Click on Next.

Import CRM Organization - Provide CRM Organization Name

Step 11: Report Server URL will auto populate or Provide required URL. Click on Next.

Import CRM Organization - Provide CRM Report Server URL

Step 12: Select Automatically Map Users, and click on Next.

Import CRM Organization - Map Users Automatically

Step 13: You will see the list of Active Directory Users. Click on Next.

Import CRM Organization - Edit User Mappings

Step 14: Click on Next.

Import CRM Organization - System Checks

Step 15: Click on Import.

Import CRM Organization - Ready to Import

Step 16: Update Organization is in Progress.

Import CRM Organization - Import Organization Wizard

Step 17: Organization imported Succesfully. Click on Finish.

Import CRM Organization - Organization created Succesfully

Step 18: Goto CRM Deployment Manager and browse for the Organization that you created.

Please provide your valuable comments on this article.

Multi Option Set / Picklist in CRM 2011 / 13 / 15 using Javascript

50th Article

Yes it is my 50th Article in WordPress. Hope you all like my articles.

Please provide your valuable comments on my articles to improve further.

Happy New Year to all my blog viewers.

Task: Create a Multi Option Set in contact, for the OptionSet below

OptionSet Values

Solution: Follow the below steps to select multi Option Set / Picklist values in CRM,

Step 1: Create below fields in Contact form,

50 - Field Schema Names

50 - Option Set Field

50 - Option Set Field Values

Step 2: Place the 2 fields on the required place on contact form, and uncheck Visible by default for “Sport Option Set Values” field.

50 - Form Properties - Option Set Field Values

Step 3: Create Javascript Webresource “new_multiPickList”. Open Text Editor, Copy & Paste the below Code

// Method to convert an optionset to multi select OptionSet
function ConvertToMultiSelect(var_sc_optionset, var_sc_optionsetvalue) {
var optionSetObj = Xrm.Page.getAttribute(var_sc_optionset);
if (optionSetObj != null) {
var options = optionSetObj.getOptions();
if (options != null) {
document.getElementById(var_sc_optionset).style.display = “none”;

// Create a DIV container

var addDiv = document.createElement(“div”);
addDiv.id = var_sc_optionsetvalue + “_m”;
addDiv.style.width = “100%”;
addDiv.style.height = “80px”;
addDiv.style.background = “#ffffff”;
addDiv.style.color = “white”;
addDiv.style.overflow = “auto”;
addDiv.style.border = “1px #6699cc solid”;

document.getElementById(var_sc_optionset).parentNode.appendChild(addDiv);

// Declaration of variables will be used in the loop depending upon the browser

var initialValue = 0,
maxValue = 0,
nAgt = navigator.userAgent;

if (nAgt.indexOf(“Firefox”) != -1) { // If the broswer is “Firefox”
initialValue = 1;
maxValue = options.length;
}
else if (nAgt.indexOf(“Chrome”) != -1 || nAgt.indexOf(“IE”) != -1) { // If the browser is Chrome or IE
initialValue = 0;
maxValue = options.length – 1;
}
else if (nAgt.indexOf(“Safari”) != -1) { // If the browser is “Safari”
initialValue = 1;
maxValue = options.length;
}

// Initialize checkbox controls

for (var i = initialValue; i < maxValue; i++) {
var pOption = options[i];
if (!IsChecked(pOption.value, var_sc_optionsetvalue)) {
var addInput = document.createElement(“input”);
addInput.type = “checkbox”;
addInput.style.border = “none”;
addInput.style.width = “25px”;
addInput.style.align = “left”;
addInput.style.color = “#000000”;
addInput.onclick = function() {
OnSave(var_sc_optionset, var_sc_optionsetvalue);
createTable(var_sc_optionsetvalue);
}
} else {
var addInput = document.createElement(“input”);
addInput.type = “checkbox”;
addInput.checked = true;
addInput.setAttribute(“checked”, true);
addInput.checked = “checked”;
addInput.defaultChecked = true;
addInput.style.border = “none”;
addInput.style.width = “25px”;
addInput.style.align = “left”;
addInput.style.color = “#000000”;
addInput.onclick = function() {
OnSave(var_sc_optionset, var_sc_optionsetvalue);
createTable(var_sc_optionsetvalue);
}
}

//Create Label

var addLabel = document.createElement(“label”);
addLabel.style.color = “#000000”;
addLabel.innerHTML = pOption.text;
var addBr = document.createElement(“br”); // it’s a ‘br’ flag

document.getElementById(var_sc_optionset).nextSibling.appendChild(addInput);
document.getElementById(var_sc_optionset).nextSibling.appendChild(addLabel);
document.getElementById(var_sc_optionset).nextSibling.appendChild(addBr);
}
}
}
}

// Check if it is selected function IsChecked(pText, optionSetValue) {

var selectedValue = Xrm.Page.getAttribute(optionSetValue).getValue();
if (selectedValue != “” && selectedValue != null) {
var OSVT = selectedValue.split(“,”);
for (var i = 0; i < OSVT.length; i++) {
if (OSVT[i] == pText)
return true;
}
}
return false;
}

// var_sc_optionsetvalue >> Provide logical-name for field which will

//                         store the multi selected values for Option Set

// optionSet>> Provide logical-name of Option Set field

function OnSave(optionSet, var_sc_optionsetvalue) {
var OS = document.getElementById(optionSet),
options = Xrm.Page.getAttribute(optionSet).getOptions(),
getInput = OS.nextSibling.getElementsByTagName(“input”),
result = “”,
result1 = “”;

var nAgt = navigator.userAgent;

for (var i = 0; i < getInput.length; i++) {
if (getInput[i].checked) {
result += getInput[i].nextSibling.innerHTML + “,”;
if (nAgt.indexOf(“Firefox”) != -1) { //If the broswer is “Firefox”
result1 += options[i + 1].value + “,”;
}
else if (nAgt.indexOf(“Chrome”) != -1 || nAgt.indexOf(“IE”) != -1) { //If the browser is Chrome or IE
result1 += options[i].value + “,”;
}
else if (nAgt.indexOf(“Safari”) != -1) { //If the browser is “Safari”
result1 += options[i + 1].value + “,”;
}
}
}

//save value
Xrm.Page.getAttribute(var_sc_optionsetvalue).setValue(result1);
}

// var_sc_optionsetvalue >> Provide logical-name for field which will
//                         store the multi selected values for Option Set

function createTable(var_sc_optionsetvalue) {    // Get OptionSet value

var OptionValue = Xrm.Page.getAttribute(var_sc_optionsetvalue),
c_OptionValue = Xrm.Page.getControl(var_sc_optionsetvalue),
d_OptionValue = var_sc_optionsetvalue + “_d”;

if (OptionValue.getValue() != null) {

var OptionValueHtml = “<div style=\”overflow-y:auto;width:100%;display: none; min-height: 5em; max-height: 1000px;\”>”,
OptionValueHtml += “<table style=’width:100%;height: 100%;’>”,
OptionValueV = OptionValue.getValue(),
OptionValueT = OptionValueV.split(“,”),
cols = 0;

for (var row = 0; row < OptionValueT.length – 1; row++) {
OptionValueHtml += “<tr style=’height:20px;’>”;
for (var i = cols; i < cols + 3; i++) {
OptionValueHtml += “<td style=’width:33%;’>”;
if (OptionValueT[i] != null || OptionValueT[i] != undefined) {
OptionValueHtml += OptionValueT[i];
}
OptionValueHtml += “</td>”;
}

cols = cols + 3;
OptionValueHtml += “</tr>”;
if (cols >= OptionValueT.length) {
break;
}
}

OptionValueHtml += “</table>”;
OptionValueHtml += “</div>”;
document.getElementById(d_OptionValue).innerHTML = OptionValueHtml;
}
}

Step 4: Open Contact Form -> Form Properties.

Add the webresource “new_multiPickList” on Contact Form.

Select Library “new_multiPickList” and provide the function name “ConvertToMultiSelect” on “OnLoad” Event.

Provide the parameters “new_favouritesport“, “new_sportoptionsetsvalues“.

50 - OnLoad

Step 5: Select Library “new_multiPickList” and provide the function name “OnSave” on OnSave Event.

Provide the parameters “new_favouritesport“, “new_sportoptionsetsvalues“.

50 - OnSave

Step 6: Save & Publish Contact Form. Open any CRM record to see the below Output,

Download JavaScript Code

Sources:

http://slalomdotcom.wordpress.com/2011/05/23/multi-select-option-sets-in-dynamics-crm/

http://mscrmmindfire.wordpress.com/2014/01/30/crm-2013-multi-pick-list-2/

Create Email using CRM SDK and C#

Follow the below steps to create a simple Email record in Console application,

Step 1: Open Visual Studio and Create Console Application Project.

Step 2: First we have to establish connection to CRM. Please follow my earlier post Connect to CRM Online or On-premise using C# SDK.

Step 3: Generate Early bound class using Crmsvcutil.exe. Please follow my earlier post Generate Early bound Class using CrmSvcUtil.Exe in CRM.

Step 4: Add EarlyBound.cs to the project.

Step 5: Include the namespace using EarlyBound.cs.

Step 6: Below method will create Email record,

Create Email - Parameter Description

public static void SendEmail(Guid From, Guid To, string ToEntitySchemaName, string Subject, string Description)
{
try
{
// Create 'From' activity party for the email

ActivityParty fromParty = new ActivityParty
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, From)
};

// Create 'To' activity party for the email

ActivityParty toParty = new ActivityParty
{
PartyId = new EntityReference(ToEntitySchemaName, To)
};

// Create an e-mail message

Email email = new Email
{
To = new ActivityParty[] { toParty },
From = new ActivityParty[] { fromParty },
Subject = Subject,
Description = Description,
DirectionCode = true
};

Guid _emailId = _service.Create(email);

SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = _emailId,
TrackingToken = "",
IssueSend = true
};

SendEmailResponse sendEmailresp = (SendEmailResponse)_service.Execute(sendEmailreq);

if (sendEmailresp != null)
{
Console.WriteLine("Email record created successfully");
Console.ReadKey();
}
}
catch(Exception ex)
{
Console.Write("Error " + ex.Message);
Console.ReadKey();
}
}

Step 7: Final Code will be like below,

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using EarlyBound;

namespace LinqToCRM
{
class Program
{
static IOrganizationService _service;
static void Main(string[] args)
{
ConnectToMSCRM("arunpotti@XYZOrg.onmicrosoft.com", "password", "https://XYZOrg.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");

Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;

if (userid == Guid.Empty) return;

Contact c = new Contact() //Create Contact Record
{
FirstName = "Arvind Kumar",
LastName = "Shah",
MobilePhone = "9876543210",
EMailAddress1 = "arvindshah@crm2015trial.com"
};

Guid contactGuid = _service.Create(c);

if (contactGuid != Guid.Empty)
{
SendEmail(userid, contactGuid, "contact", "Test Subject", "Test Description");
}
}

public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
{
try
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;
Uri serviceUri = new Uri(SoapOrgServiceUri);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
}
catch (Exception ex)
{
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();
}
}

public static void SendEmail(Guid From, Guid To, string ToEntitySchemaName, string Subject, string Description)
{
try
{
// Create 'From' activity party for the email

ActivityParty fromParty = new ActivityParty
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, From)
};

// Create 'To' activity party for the email

ActivityParty toParty = new ActivityParty
{
PartyId = new EntityReference(ToEntitySchemaName, To)
};

// Create an e-mail message

Email email = new Email
{
To = new ActivityParty[] { toParty },
From = new ActivityParty[] { fromParty },
Subject = Subject,
Description = Description,
DirectionCode = true
};

Guid _emailId = _service.Create(email);

SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = _emailId,
TrackingToken = "",
IssueSend = true
};

SendEmailResponse sendEmailresp = (SendEmailResponse)_service.Execute(sendEmailreq);

if (sendEmailresp != null)
{
Console.WriteLine("Email record created successfully");
Console.ReadKey();
}
}

catch(Exception ex)
{
Console.Write("Error " + ex.Message);
Console.ReadKey();
}
}
}
}

Step 8: Execute the project for the below output, Open CRM and check for the email record

Create Email - Output

Please provide your valuable comments on this article.

Delete Contact record using Early Bound and LINQ in CRM

Task: Delete Contact record where Full Name = “Arun Potti”

Solution: Follow the below steps,

Step 1: Open Visual Studio and Create Console Application Project.

Step 2: First we have to establish connection to CRM. Please follow my earlier post Connect to CRM Online or On-premise using C# SDK.

Step 3: Generate Early bound class using Crmsvcutil.exe. Please follow my earlier post Generate Early bound Class using CrmSvcUtil.Exe in CRM.

Step 4: Add EarlyBound.cs to the project.

Step 5: Include the namespace using EarlyBound.cs.

Step 6: Below method will delete Contact record,

deleteContactRecord("Arun Potti");
public static void deleteContactRecord(string fullname)
       {
           using (var context = new ServiceContext(_service))
           {
               Contact conRecord = context.ContactSet.FirstOrDefault(c => c.FullName == fullName);
               if (conRecord != null)
               {
                   context.DeleteObject(conRecord);
                   if (context.SaveChanges().Count > 0)
                  {
                       Console.WriteLine("Record deleted Successfully");
                       Console.ReadKey();
                   }
               }
           }
       }

Step 7: Final Code will be like below,

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using EarlyBound;
namespace LinqToCRM
{
class Program
{
static IOrganizationService _service;
static void Main(string[] args)
{
ConnectToMSCRM("arunpotti@MyOrg.onmicrosoft.com", "Password", "https://MyOrg.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");
Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
if (userid == Guid.Empty) return;
deleteContactRecord("Arun Potti");
}
public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
{
try
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;
Uri serviceUri = new Uri(SoapOrgServiceUri);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
}
catch (Exception ex)
{
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();
}
}
public static void deleteContactRecord(string fullName)
{
using (var context = new ServiceContext(_service))
{
Contact conRecord = context.ContactSet.FirstOrDefault(c => c.FullName == fullName);
if (conRecord != null)
{
context.DeleteObject(conRecord);
if (context.SaveChanges().Count > 0)
{
Console.WriteLine("Record deleted Successfully");
Console.ReadKey();
}
}
}
}
}
}

Step 8: Execute the project for the below output,

LinqToCrm Delete Output

Please provide your valuable comments on this article.

Create record using Early Bound and LINQ in CRM

Task: Create Contact record

Solution: Follow the below steps,

Step 1: Open Visual Studio and Create Console Application Project.

Step 2: First we have to establish connection to CRM. Please follow my earlier post Connect to CRM Online or On-premise using C# SDK.

Step 3: Generate Early bound class using Crmsvcutil.exe. Please follow my earlier post Generate Early bound Class using CrmSvcUtil.Exe in CRM.

Step 4: Add EarlyBound.cs to the project.

Step 5: Include the namespace using EarlyBound.cs.

Step 6: Below method will create Contact record,

createContactRecord("Arun", "Potti", "9876543210", "arunpotti@xyzorg.com");

public static void createContactRecord(string firstName, string lastName, string mobileNumber, string email)
{
Contact c = new Contact()
{
FirstName = firstName,
LastName =lastName,
MobilePhone = mobileNumber,
EMailAddress1 = email
};
Guid contactGuid = _service.Create(c);
if (contactGuid != Guid.Empty)
Console.WriteLine("Contact record created");
}

Step 7: Final Code will be like below,

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using EarlyBound;
 
namespace LinqToCRM
{
class Program
{
static IOrganizationService _service;
static void Main(string[] args)
{
ConnectToMSCRM("arunpotti@MyOrg.onmicrosoft.com", "Password", "https://MyOrg.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");
Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;

if (userid == Guid.Empty) return;
createContactRecord("Arun", "Potti", "9876543210", "arunpotti@xyzorg.com");
}

public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
{
try
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;
Uri serviceUri = new Uri(SoapOrgServiceUri);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
}
catch (Exception ex)
{
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();
}
}

public static void createContactRecord(string firstName, string lastName, string mobileNumber, string email)
{
Contact c = new Contact()
{
FirstName = firstName,
LastName = lastName,
MobilePhone = mobileNumber,
EMailAddress1 = email
};

Guid contactGuid = _service.Create(c);
if (contactGuid != Guid.Empty)
{
Console.WriteLine("Contact record created");
Console.ReadKey();
}
}
}
}

Step 8: Execute the project for the below output,

LinqToCrm Create Output

Please provide your valuable comments on this article.

Retrieve records using Early Bound and LINQ in CRM

Task: Display all Contact records Full Name from Contact entity where Status = Active and Order by Full Name

Solution: Follow the below steps,

Step 1: Open Visual Studio and Create Console Application Project.

Step 2: First we have to establish connection to CRM. Please follow my earlier post Connect to CRM Online or On-premise using C# SDK.

Step 3: Generate Early bound class using Crmsvcutil.exe. Please follow my earlier post Generate Early bound Class using CrmSvcUtil.Exe in CRM.

Step 4: Add EarlyBound.cs to the project.

Step 5: Include the namespace using EarlyBound.cs.

Step 6: Below code will retrieve records from Contact entity,

public static void retrieveContactRecords()
{
using (var context = new ServiceContext(_service))
{
var getContactRecords = from conSet in context.ContactSet
where conSet.StateCode == 0   //Check for Status == Active
orderby conSet.FullName
select conSet;

if (getContactRecords != null)
{
StringBuilder fullNameString = new StringBuilder();
Console.WriteLine("Full Name\n--------------------------");
foreach (var item in getContactRecords)
{
if (item.FullName != null)
fullNameString.AppendLine(item.FullName);
}

Console.Write(fullNameString);
Console.ReadKey();
}
}
}

Step 7: Final Code will be like below,

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using EarlyBound;

namespace LinqToCRM
{
class Program
{
static IOrganizationService _service;
static void Main(string[] args)
{
ConnectToMSCRM("arunpotti@MyOrg.onmicrosoft.com", "Password", "https://MyOrg.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");
Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;

if (userid == Guid.Empty) return; // Check whether connection established or not

retrieveContactRecords();
}

public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
{
try
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;
Uri serviceUri = new Uri(SoapOrgServiceUri);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
}
catch (Exception ex)
{
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();
}
}

public static void retrieveContactRecords()
{
using (var context = new ServiceContext(_service))
{
var getContactRecords = from conSet in context.ContactSet
where conSet.StateCode == 0   //Check for Status == Active
orderby conSet.FullName
select conSet;

if (getContactRecords != null)
{
StringBuilder fullNameString = new StringBuilder();
Console.WriteLine("Full Name\n--------------------------");
foreach (var item in getContactRecords)
{
if (item.FullName != null)
fullNameString.AppendLine(item.FullName);
}

Console.Write(fullNameString);
Console.ReadKey();
}
}
}
}
}

Step 8: Execute the project for the below output,

Linq Retrieve Output

Please provide your valuable comments on this article.