- In this walkthrough we will learn Step-By-Step How to integrate CRM2011 with SSIS through a Web Service in Dynamics CRM 2011 and Sql Server Integration Services.
We'll call the CRM 2011 WCF Web Service and perform CRUD operations using Indexing on the generic Entity class. We'll consume the CRM WCF Organization (SOAP) service from a class which we will use from inside an SSIS project, but the same logic can also be used by a console application, or a Windows Service, or a Windows Form application.
We'll not use the early binding Microsoft CRM SDK 2011; instead we'll reach directly the CRM 2011 WCF SOAP endpoint using Late Binding to perform the required CRUD operations. - Sql Server Integration Services as implemented through Visual Studio 2008, support only the Framework 3.5 libraries at most. Therefore, we cannot use the CRM SDK and its framework 4.0 assemblies while coding from SSIS 2008. Therefore , we'll reach Crm2011 from SSIS via its Organization Web Service.
- First, we'll create a .dll which will send requests to the CRM2011 Organization Web Service . Then, we'll create an SSIS project to use that assembly and fetch the CRM data, and also create new entities from SSIS.
- So let's create a new Class Project in Visual Studio 2010 or 2008, selecting the target framework to be version 3.5:

- Next, add the CRM 2011 Web Service to the project:

- Remember that we'll be using the Organization CRM Web Service:

- After adding the Web Service, rename the Class to CrmInit:

- Create an static method returning an IOrganizationService object. This function will get the host, organization and user logon data:

- Now add the following security standard code for reaching the CRM Web Service :

SymmetricSecurityBindingElement security = new SymmetricSecurityBindingElement();
security.ProtectionTokenParameters = new SspiSecurityTokenParameters();
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
httpTransport.MaxReceivedMessageSize = Int32.MaxValue ;
CustomBinding binding = new CustomBinding();
binding.Elements.Add(security);
TextMessageEncodingBindingElement encoding =
new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressing10, Encoding.UTF8);
binding.Elements.Add(encoding);
binding.Elements.Add(httpTransport); - Now append the WCF Endpoint code:

EndpointAddress endpoint =
new EndpointAddress(new Uri(string.Format("{0}/{1}/XRMServices/2011/Organization.svc", host, organization)),
EndpointIdentity.CreateUpnIdentity(string.Format("{0}@{1}", user, "")), new AddressHeader[] { });
- And finally add the code to craete the Client:

OrganizationServiceClient client = new OrganizationServiceClient(binding, endpoint);
client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(user, password, ""); - Check that you have all the "usings" you need:

- Also, check the references:

- Now, in order to test our class, create a new console Project , which will call our CRM assembly:

- Add the following references:

- First, add the Serialization assembly:

- Second, add our assembly:

- Now, code a call to the IOrganizationService:

- Add the corresponding using:

- ... and type the relevant logon data:

- Next, select some columns you want to retrieve, and get the entity object:

- Check in the QuickWatch window that you get an account with the required attributes:


- Now we want to use the dll, so get to the BIN folder, and get it:

- .. and paste it to some relevant folder where you'll keep it:

Also, paste it to the SSIS folder (if you decide to put the assembly inside the GAC, you could skip this step):
C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn - Now it's time to create the SSIS project. Open Visual Studio 2008, and create the project:

- Search in the toolbox for an Script Task:

- Drop the task and rename it:

- Change the project properties:

- .. in order to run the debbugger in 32 bits mode:


- Now open the editor of the task:

- Go to the Properties window:

- ... and change the target Framework to 3.5:

- Now go to the references tab:

- ... and add a reference to the Serialization assembly:

- ... and to our CRM assembly:

- Add the "using" directive to make visible the assembly's classes:

- Now go to the CRMProxy project, and copy the code we tested inside our Console project:

- Return to the SSIS project and paste the code inside the Main function:

- .. and add a breakpoint:

- Build the Script Task :

- Close the VisualStudio Tool For Applications (VSTA) project, and save the Script Task:

- Debug the SSIS project until the breakpoint is reached:

- When the debugger reaches the breakpoint, check for the account data returned from the Web Service:

- We got the CRM data. Let's now retrieve the Account name:

- Run again the debugger, and check for the returned Account name:

- It seems OK. We fetch CRM2011 data from the SSIS. Now let's create a new Entity record from the SSIS. Add the following code to create a new Competitor record:

- Run the SSIS, and open your CRM2011 to see the new added Competitor:

This late Entity creation using the "Attributes" ( [""] ) feature, is the result of extending the Entity class to allow for string indexing using the field names. We extended the Entity class in this walkthrough.
That's all...Enjoy Dynamics CRMכתב: כרמל שוורצמן
A Step-By-Step Walk-Through CRM 2011 designed for Begginer and Professional CRM Developers
Microsoft Dynamics CRM 2011
Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts
Tuesday, November 12, 2013
How to Integrate Dynamics CRM with SSIS
by Carmel Schvartzman
Thursday, October 3, 2013
Step-By-Step How to fix the Entity Framework error "no valid primary key could be inferred"
by Carmel Schvartzman
- In this walkthrough we will learn How to fix the Entity Framework error "does not have a primary key defined and no valid primary key could be inferred".

- In the case you design an Entity Type in the Entity Framework's conceptual entity model, based not on a sql table, but on a sql view, or in the case your table does not have a primary key defined, the entity framework will try to infer which column can be the unique KEY of the entity.
You cannot state a primary key for an SQL SERVER view, so if you create an Entity Framework entity based on a view, you may have an issue here.
Usually the Entity Framework will succeed inferring the primary key: if the SQL view is the result of a JOIN and you bring some primary key between the selected columns of the view, it will take that field as the unique KEY of the entity.
The error "does not have a primary key defined and no valid primary key could be inferred" tells us that the Entity Framework couldn't infer the primary key.
- In our example, we see that the entity CRM_USER_ACTIVITIES, based on the view of the same name, does not have a primary key:

- We received the error message because there is not field at the view that the Entity Framework can recognize as an original Primary Key of some table.In our example, NEW_IDENTIFIER is not a key, and ACTIVITYID, although a primary key of the table LETTERBASE, is camouflaged here inside the "GUID" column:

- To solve the problem, let's wrap the entire select inside another select which will load the Primary Key of the LETTERBASE table we spoke of before, ACTIVITYID, using an INNER JOIN, so that the Entity Framework will recognize the key: therefore state BEFORE the query the code

- And at the end of the query , let's close the JOIN defining the ON clause using the primary key:

- Now save the view using ALTER and rerun the Entity Framework rebuilding the Visual Studio project:

- Just press at Finish after selecting the CRM_USER_ACTIVITIES from the "Views" tree item:

- Another approach to solve the problem, is to add a new column to the SQL SERVER view, called ID. This column will be unique, because we'll insert in it a DENSE_RANK over the field that we know is unique:

That's all...Enjoy Dynamics CRMכתב: כרמל שוורצמן
Subscribe to:
Posts (Atom)