1. Save
Excel file in your Cdrive
2. Generate early bound types
Run the CrmSvcUtil.exe tool, with the “Microsoft.Xrm.Client.CodeGeneration” extension, to generate your entity classes and service contexts. The following is an example command to create a file called Xrm.cs that points at an instance of Microsoft Dynamics CRM. Note that the Microsoft.Xrm.Client.CodeGeneration.dll file must be in the same directory as the CrmSvcUtil.exe file, or in the system global assembly cache, when you run this command. The first command shown is for an on-premises organization. The second command is for a CRM Online organization. Both commands should be executed as a single command line with no line breaks.
CrmSvcUtil.exe
/codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization,
Microsoft.Xrm.Client.CodeGeneration"
/out:Xrm.cs
/url:https://<mydomain>.api.crm.dynamics.com/XRMServices/2011/Organization.svc
/username:<myusername>@<mydomain>.onmicrosoft.com
/password:<mypassword> /namespace:Xrm /serviceContextName:XrmServiceContext
3. Create
a Console Application project in Visual studio 2012.
4. Add the following references from the
SDK\bin
folder. You can skip this step
and the next by simply installing the Microsoft.CrmSdk.Extensions
NuGet package.- AntiXSSLibrary.dll
- Microsoft.Crm.Sdk.Proxy.dll
- Microsoft.Xrm.Client.dll
- Microsoft.Xrm.Portal.dll
- Microsoft.Xrm.Portal.Files.dll
- Microsoft.Xrm.Sdk.dll
5.Add the following references
from .NET.
- System.IdentityModel.dll
- Microsoft.ServiceBus.dll
- System.Data.Services.dll
- System.Data.Services.Client.dll
- System.Runtime.Serialization.dll
6.Make sure the console project specifies .NET Framework 4.5.2 as the
target framework and not .NET Framework 4.5.2 Client Profile. Check the
properties of the project by selecting the ConsoleApplication1 project in Solution
Explorer and clicking Alt-Enter.
Right Click
ConsoleApplication1 -> Properties
7. Right-click the project in
Visual Studio, click Add, and then click Existing Item.
- Select the “Xrm.cs” file that you created when you generated the early bound types.
- Right-click your project again, click Add, and then click New Item.
- Select Application Configuration File from the options and then click Add.
- Edit the App.config configuration file with your specific connection string.XML
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
</configSections>
<connectionStrings> <add name="Xrm" connectionString="Server=http://crmserver/contoso; Domain=CONTOSO; Username=Administrator;Password=pass@word1"/></connectionStrings>
<microsoft.xrm.client>
<contexts default="Xrm">
<add name="Xrm" type="Xrm.XrmServiceContext, Xrm" connectionStringName="Xrm"/>
</contexts>
</microsoft.xrm.client>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>
- Add a using statement to the namespace that you used in Step 1 when you created the project (for this example “Xrm”).using System;using System.Linq;using System.Threading.Tasks;using System.ServiceModel;using System.ServiceModel.Description;using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions;using System.Xml;using System.Xml.Linq;using System.Xml.XPath;using System.Xml;using System.IO;using System.Linq;//earlybound typesusing Xrm;// These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly// found in the SDK\bin folder.using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Client;using Microsoft.Xrm.Sdk.Query;using Microsoft.Xrm.Sdk.Discovery;// These namespaces are found in the Microsoft.Sharepoint.Client.dll assembly// found in the SDK\bin folder.using Microsoft.Win32;using Microsoft.SharePoint.Client;// This namespace is found in Microsoft.Crm.Sdk.Proxy.dll and Microsoft.Crm.Sdk.Workflow.dll assembly// found in the SDK\bin folder.using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Metadata;using Microsoft.Xrm.Sdk.Workflow;using Microsoft.Crm.Sdk.Messages;// These namespaces are found in the System.Data assembly// found in the .NETusing System.Data;using System.Data.Services;using System.Diagnostics;using System.Globalization;using System.Reflection;using System.Net;// add the following OleDb statement to connect with Excelusing System.Data.OleDb;
- Declare the Main Method as below:-{var xrm = new XrmServiceContext("Xrm");//Set up connection to Excel fileOleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\temp\\input\\Account; Extended Properties=Excel 8.0");con.Open();try{}catch(){}}
- Add the following code in Try Blocktry{DataSet ds = new DataSet();OleDbDataAdapter cmd = new OleDbDataAdapter("SELECT * FROM [DATA$]", con);cmd.Fill(ds);con.Close();foreach (DataRow dr in ds.Tables[0].Rows){object[] cells = dr.ItemArray;var account = new Xrm.Account{Name = cells[0].ToString(),Address1_City = cells[1].ToString(),Telephone1 = cells[2].ToString()};xrm.AddObject(account);xrm.SaveChanges();}Console.WriteLine("Account record created successfullly in CRM");Console.WriteLine("Press any key to exit1.");Console.ReadKey();}
- Add the following Code in Catch Blockcatch(Exception ex){Console.WriteLine("Press any key to exit2." + ex.Message);Console.ReadKey();}
- Rebuild and Run the solution
- Testing: Now go to advance find search like below query and click on result
- Testing:
No comments:
Post a Comment