<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>CRM</title>
        <link>http://www.lightningtools.com/blog/category/19.aspx</link>
        <description>Dynamics CRM</description>
        <language>en-GB</language>
        <copyright>Lightning Tools</copyright>
        <managingEditor>support@lightningtools.com</managingEditor>
        <generator>Subtext Version 1.9.5.177</generator>
        <item>
            <title>CRM 2011 Online and SharePoint 2010 Integration through Business Connectivity Services</title>
            <link>http://lightningtools.com/blog/archive/2012/02/20/crm-2011-online-and-sharepoint-2010-integration-through-business-connectivity.aspx</link>
            <description>&lt;p&gt;Integrating SharePoint and Microsoft CRM makes sense. Your sales team use CRM as they go about their daily work, but quite often non-sales people still need to be able to access and work with the same data. Although CRM 2011 has become part of the Office 365 package recently, they still run as separate instances in the cloud. Getting the data from CRM 2011 online displayed in SharePoint Online through the Business Connectivity Services is possible but needs a bit of coding and Azure magic. We are going to go through the steps to show you the kind of things involved to get this to happen. The scenario is we want to be able to display 30 days worth of orders in SharePoint Online through the Business Connectivity Services.&lt;/p&gt;  &lt;p&gt;This post makes a number of assumptions:&lt;/p&gt;  &lt;p&gt;1, You have an Azure account setup and the tools installed with Visual Studio 2010.&lt;/p&gt;  &lt;p&gt;2, You know how to deploy a project to Azure&lt;/p&gt;  &lt;p&gt;3, You have a CRM 2011 Online account and a SharePoint Online account (may be both are through Office 365)&lt;/p&gt;  &lt;p&gt;4, You have downloaded the &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=24004" target="_blank"&gt;CRM 2011 SDK&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Here’s an overview of what we are going to do:&lt;/p&gt;  &lt;p&gt;1, Build a WCF Service that can pull data from CRM Online and present it in a BCS friendly manner&lt;/p&gt;  &lt;p&gt;2, Run this WCF Service on Azure&lt;/p&gt;  &lt;p&gt;3, Use SharePoint Designer 2010 to create an External Content Type that will display CRM data in SharePoint Online.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Generate Early Bound Types for CRM&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Crm has a really flexible data model which allows you to add new custom fields to entities such as customers or sales. This also presents the problem that the web services are not strongly typed. There are some web service methods you can call to discover the data structure that is used within CRM, but Microsoft also ships a tool with the CRM SDK that can generate your CRM data model in code so you can use Linq to CRM to get access to your data. &lt;/p&gt;  &lt;p&gt;Open a command prompt and move to the bin folder within the CRM SDK. crmsvcutil.exe is the tool we use to generate our code. At the command prompt type:&lt;/p&gt;  &lt;p&gt;CrmSvcUtil.exe /url:https://[YourCrmSiteName].crm.dynamics.com/XRMServices/2011/Organization.svc /out:GeneratedCode.cs /username:"[your Live Id]" /password:"[your password]"&lt;/p&gt;  &lt;p&gt;These are the parameters you need to use when using CRM 2011 online. If you are using CRM on premise or have any issues running this check out this page for some help:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/gg327844.aspx"&gt;http://msdn.microsoft.com/en-us/library/gg327844.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Running the command above will generate a file called GeneratedCode.cs that we will add to our project shortly.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Create a WCF Azure Service&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;1, Open up Visual Studio 2010 and choose to Create a new Cloud –&amp;gt; Windows Azure Project called something like CrmBcsWcfLayer (see how many acronyms I can fit into a project name! &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/wlEmoticon-smile_2.png" /&gt;)&lt;/p&gt;  &lt;p&gt;2, Select the WCF Service Web Role from the Azure project types available and once moved across to the right pane hover your mouse over it to rename it to something sensible&lt;/p&gt;  &lt;p&gt;3, First thing we can do is add our GeneratedCode.cs file from earlier into our WCF project. This file is generated in the same directory where you ran the crmsvcutil tool.&lt;/p&gt;  &lt;p&gt;4, Now we need to add some assembles from the CRM SDK. In the bin folder of the SDK select:&lt;/p&gt;  &lt;p&gt;microsoft.xrm.sdk    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Also add a reference to:&lt;/p&gt;  &lt;p&gt;System.Security&lt;/p&gt;  &lt;p&gt;5, To help us authenticate and run code against CRM Online we want to make use of a helper class from the CRM SDK. Add an existing item to your project and find \{YOUR CRM SDK}\sdk\samplecode\cs\helpercode\deviceidmanager.cs&lt;/p&gt;  &lt;p&gt;6, Open up IService.cs and delete the CompositeType class. The SalesOrder CRM object has many fields, but we only want to return a small subset. We will therefore define our own class that contains this subset of fields&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cd994524-98bc-44db-ad29-9c033a7ac0ae" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;;font-family:Consolas;font-size:11.25"&gt;&lt;span style="color: #000000;"&gt;[DataContract]
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; BcsSalesOrder
{
    [DataMember]
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; Guid SalesOrderId { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;set&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }

    [DataMember]
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;decimal&lt;/span&gt;&lt;span style="color: #000000;"&gt; TotalAmount { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;set&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }

    [DataMember]
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; CustomerName { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;set&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }

    [DataMember]
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; DateTime OrderDate { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;set&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;7, Still in IService.cs, delete the two old operation contracts from there. Add in the following interface&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:45ebff89-05d0-4d0a-a867-b4c0cefe7a7c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;;font-family:Consolas;font-size:11.25"&gt;&lt;span style="color: #000000;"&gt;[OperationContract]
IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;BcsSalesOrder&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetRecentOrders();
        &lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;8, Right click on Service1.svc and chose View Code. Clear out the GetData and GetDataUsingDataContract method.&lt;/p&gt;

&lt;p&gt;9, We’re going to add some code to help us Authenticate against CRM. This code is taken from samples in the CRM ask so I can’t take any credit for it! Within the Service1 class paste in:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d54ea898-3bcc-4408-8487-bc7118afcf66" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;;font-family:Consolas;font-size:11.25"&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Obtain the AuthenticationCredentials based on AuthenticationProviderType.
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name="endpointType"&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;An AuthenticationProviderType of the CRM environment.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;Get filled credentials.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; AuthenticationCredentials GetCredentials(AuthenticationProviderType endpointType)
{

    AuthenticationCredentials authCredentials &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; AuthenticationCredentials();
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;switch&lt;/span&gt;&lt;span style="color: #000000;"&gt; (endpointType)
    {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;case&lt;/span&gt;&lt;span style="color: #000000;"&gt; AuthenticationProviderType.ActiveDirectory:
            authCredentials.ClientCredentials.Windows.ClientCredential &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Net.NetworkCredential(_userName,
                    _password,
                    _domain);
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;break&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;case&lt;/span&gt;&lt;span style="color: #000000;"&gt; AuthenticationProviderType.LiveId:
            authCredentials.ClientCredentials.UserName.UserName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; _userName;
            authCredentials.ClientCredentials.UserName.Password &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; _password;
            authCredentials.SupportingCredentials &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; AuthenticationCredentials();
            authCredentials.SupportingCredentials.ClientCredentials &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
                Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;break&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;default&lt;/span&gt;&lt;span style="color: #000000;"&gt;: &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; For Federated and OnlineFederated environments.                    &lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            authCredentials.ClientCredentials.UserName.UserName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; _userName;
            authCredentials.ClientCredentials.UserName.Password &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; _password;
            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; For OnlineFederated single-sign on, you could just use current UserPrincipalName instead of passing user name and password.
            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; authCredentials.UserPrincipalName = UserPrincipal.Current.UserPrincipalName;  &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Windows/Kerberos&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;break&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
    }

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; authCredentials;
}

&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Discovers the organizations that the calling user belongs to.
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name="service"&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;A Discovery service proxy instance.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;Array containing detailed information on each organization that 
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; the user belongs to.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; OrganizationDetailCollection DiscoverOrganizations(
    IDiscoveryService service)
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (service &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;throw&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ArgumentNullException(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;service&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    RetrieveOrganizationsRequest orgRequest &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; RetrieveOrganizationsRequest();
    RetrieveOrganizationsResponse orgResponse &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
        (RetrieveOrganizationsResponse)service.Execute(orgRequest);

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; orgResponse.Details;
}

&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Finds a specific organization detail in the array of organization details
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; returned from the Discovery service.
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name="orgFriendlyName"&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;The friendly name of the organization to find.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name="orgDetails"&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;Array of organization detail object returned from the discovery service.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;Organization details or null if the organization was not found.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;seealso cref="DiscoveryOrganizations"/&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; OrganizationDetail FindOrganization(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; orgFriendlyName,
    OrganizationDetail[] orgDetails)
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (String.IsNullOrWhiteSpace(orgFriendlyName))
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;throw&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ArgumentNullException(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;orgFriendlyName&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (orgDetails &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;throw&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ArgumentNullException(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;orgDetails&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    OrganizationDetail orgDetail &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;;

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt; (OrganizationDetail detail &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; orgDetails)
    {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (String.Compare(detail.FriendlyName, orgFriendlyName,
            StringComparison.InvariantCultureIgnoreCase) &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
        {
            orgDetail &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; detail;
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;break&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        }
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; orgDetail;
}

&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Generic method to obtain discovery/organization service proxy instance.
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;typeparam name="TService"&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Set IDiscoveryService or IOrganizationService type to request respective service proxy instance.
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;typeparam name="TProxy"&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Set the return type to either DiscoveryServiceProxy or OrganizationServiceProxy type based on TService type.
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name="serviceManagement"&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;An instance of IServiceManagement&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name="authCredentials"&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;The user's Microsoft Dynamics CRM logon credentials.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; TProxy GetProxy&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;TService, TProxy&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(
    IServiceManagement&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;TService&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; serviceManagement,
    AuthenticationCredentials authCredentials)
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; TService : &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; TProxy : ServiceProxy&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;TService&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
{
    Type classType &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(TProxy);

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (serviceManagement.AuthenticationType &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
        AuthenticationProviderType.ActiveDirectory)
    {
        AuthenticationCredentials tokenCredentials &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
            serviceManagement.Authenticate(authCredentials);
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Obtain discovery/organization service proxy for Federated, LiveId and OnlineFederated environments. 
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Instantiate a new class of type using the 2 parameter constructor of type IServiceManagement and SecurityTokenResponse.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; (TProxy)classType
            .GetConstructor(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Type[] { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(IServiceManagement&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;TService&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;), &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(SecurityTokenResponse) })
            .Invoke(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
    }

    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Obtain discovery/organization service proxy for ActiveDirectory environment.
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Instantiate a new class of type using the 2 parameter constructor of type IServiceManagement and ClientCredentials.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; (TProxy)classType
        .GetConstructor(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Type[] { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(IServiceManagement&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;TService&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;), &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(ClientCredentials) })
        .Invoke(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;[] { serviceManagement, authCredentials.ClientCredentials });
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;You’ll need to resolve some references once you paste this code in.&lt;/p&gt;

&lt;p&gt;10, In Service1.svc we need to add a few class variables that we’ll set…&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9ad3e3ee-e0b3-47ae-9177-35e496f5fd18" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;;font-family:Consolas;font-size:11.25"&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; String _discoveryServiceAddress &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;https://dev.crm.dynamics.com/XRMServices/2011/Discovery.svc&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; String _organizationUniqueName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;""&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Provide your user name and password.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; String _userName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Live Id&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; String _password &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Password&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;

&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Provide domain name for the On-Premises org.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; String _domain &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;mydomain&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Set the values for your Organization name, and the Live Id and password you want to connect as.&lt;/p&gt;

&lt;p&gt;11, Add in the method that was defined in our interface:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b95e4775-dd76-4033-9a72-b43f6af5bc18" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;;font-family:Consolas;font-size:11.25"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;BcsSalesOrder&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetRecentOrders()
{

}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;12, Add in the following code to this method:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1a2a26a8-55f2-4b79-b6a3-32e0c6285603" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;;font-family:Consolas;font-size:11.25"&gt;&lt;span style="color: #000000;"&gt;List&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;BcsSalesOrder&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; orders &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; List&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;BcsSalesOrder&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;();

IServiceManagement&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IDiscoveryService&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; serviceManagement &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
            ServiceConfigurationFactory.CreateManagement&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IDiscoveryService&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Uri(_discoveryServiceAddress));
AuthenticationProviderType endpointType &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; serviceManagement.AuthenticationType;

&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Set the credentials.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;AuthenticationCredentials authCredentials &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetCredentials(endpointType);


String organizationUri &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; String.Empty;
&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Get the discovery service proxy.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; (DiscoveryServiceProxy discoveryProxy &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    GetProxy&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IDiscoveryService, DiscoveryServiceProxy&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(serviceManagement, authCredentials))
{
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Obtain organization information from the Discovery service. &lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (discoveryProxy &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
    {
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Obtain information about the organizations that the system user belongs to.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        OrganizationDetailCollection orgs &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; DiscoverOrganizations(discoveryProxy);
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Obtains the Web address (Uri) of the target organization.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        organizationUri &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; FindOrganization(_organizationUniqueName,
            orgs.ToArray()).Endpoints[EndpointType.OrganizationService];

    }
}


&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #000000;"&gt;!&lt;/span&gt;&lt;span style="color: #000000;"&gt;String.IsNullOrWhiteSpace(organizationUri))
{
    IServiceManagement&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IOrganizationService&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; orgServiceManagement &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
        ServiceConfigurationFactory.CreateManagement&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IOrganizationService&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Uri(organizationUri));

    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Set the credentials.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    AuthenticationCredentials credentials &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetCredentials(endpointType);

    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Get the organization service proxy.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; (OrganizationServiceProxy organizationProxy &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;
        GetProxy&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IOrganizationService, OrganizationServiceProxy&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(orgServiceManagement, credentials))
    {
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; This statement is required to enable early-bound type support.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        organizationProxy.EnableProxyTypes();

        var service &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (IOrganizationService)organizationProxy;
        OrganizationServiceContext orgContext &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; OrganizationServiceContext(service);

        IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;SalesOrder&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; crmOrders &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; from s &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; orgContext.CreateQuery&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;SalesOrder&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;()
                                            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; s.DateFulfilled.Value &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; DateTime.Now.AddMonths(&lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
                                            select s;

        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var salesOrder &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; crmOrders)
        {
                        
            var bcsOrder &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; BcsSalesOrder();
            bcsOrder.SalesOrderId &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (Guid)salesOrder.SalesOrderId;
            bcsOrder.CustomerName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; salesOrder.CustomerId.Name;
            bcsOrder.OrderDate &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Convert.ToDateTime(salesOrder.DateFulfilled);
            bcsOrder.TotalAmount &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; salesOrder.TotalAmount.Value;

            orders.Add(bcsOrder);
        }

    }
}

&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; orders;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;13, We can now test our WCF service to make sure it can return some data from CRM Online. Press F5 in Visual Studio and once the browser opens copy out the url from the address bar. Then open a Visual Studio command prompt from the start menu and type wcftestclient.&lt;/p&gt;

&lt;p&gt;14, Once the WCF Test Client opens up go File –&amp;gt; Add a Service, and paste in your url&lt;/p&gt;

&lt;p&gt;15, Click on the GetThisMonthsOrders from the tree view in the right Window and then click the Invoke button. This will try to execute your WCF method and if successful you should see data returned in the bottom window:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/SNAGHTML41c7e1.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="SNAGHTML41c7e1" border="0" alt="SNAGHTML41c7e1" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/SNAGHTML41c7e1_thumb.png" width="244" height="156" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you get an error returned, you should be able to step through the code in Visual Studio and fix it. If you do not get any data returned, make sure you have some orders in CRM that have been fulfilled in the last month.&lt;/p&gt;

&lt;p&gt;16, Now we can deploy this to Azure. Right click on the Azure project in Visual Studio and click Package&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_thumb.png" width="244" height="79" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will notice when you do this you get a warning about Microsoft.IdentityModel.dll being required. This is because microsoft.xrm.sdk has a dependancy on it. Follow the instructions in the Output console to add it as a reference to your project and also set it to ‘copy local’&lt;/p&gt;

&lt;p&gt;17, Once the Service Package file and Cloud Service Configuration File have been produced you can deploy this to Azure.&lt;/p&gt;

&lt;p&gt;18, Once your project has been deployed it is always a good thing to test it with the WcfTestClient again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting the data into SharePoint Online&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1, If you are at this stage then great! We can now get our data displaying in SharePoint Online. Open up SharePoint Designer and connect to your SharePoint Online site&lt;/p&gt;

&lt;p&gt;2, Click on the External Content Types button in the Site Objects listing&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_4.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_thumb_1.png" width="211" height="124" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3, Click the button in the ribbon to create a new External Content Type&lt;/p&gt;

&lt;p&gt;4, Give your External Content Type a nice name such as Orders, and then click on the link to use the Operation Designer:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_6.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_thumb_2.png" width="560" height="40" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5, Click the button to ‘Add a connection’ and choose the type to be WCF Service&lt;/p&gt;

&lt;p&gt;6, When viewing your service1.svc file running on Azure, you should see a link to the wsdl file. It’s basically the same as service1.svc but with ?wsdl on the end. Copy the whole url out and paste it into the Service Metadata Url field in SPD&lt;/p&gt;

&lt;p&gt;7, Put the normal service1.svc full url into Service Endpoint URL, and you can then click OK:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/SNAGHTML941779.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="SNAGHTML941779" border="0" alt="SNAGHTML941779" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/SNAGHTML941779_thumb.png" width="171" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;8, With the Service Endpoint now displayed in the SPD connection manager, expand the tree so you can see the method we created and right click on it. Choose ‘New Read List Operation’ and step through the wizard. You do not actually need to change anything in the steps to get our simple example working except on the third screen select SalesOrderId as the Identifier for this ECT&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_8.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_thumb_3.png" width="244" height="116" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;9, Once the Operation Designer wizard has finished, click the Save button in the top left of SPD and your External Content Type will be created!&lt;/p&gt;

&lt;p&gt;10, The final step! Woot! Log on to SharePoint Online and add a Business Data List web part to the page. Configure it to use your Orders External Content Type:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_10.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_thumb_4.png" width="244" height="204" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;11, Click OK in the web part properties, and you should see Orders from your CRM system from the last 30 days (that the CRM users credentials you used in your WCF service have permission to see)&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_12.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lightningtools.com/blog/images/lightningtools_com/blog/Windows-Live-Writer/CRM-2011-Online-and-SharePoint-2010-Busi_E936/image_thumb_5.png" width="644" height="162" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You should be asking yourself right now – but this WCF web service is publically available and so anyone could call it and see my CRM data – and you are completely correct. As a ‘how-to’ article we are leaving things here, but if you were to be following this and using it as a basis for moving something into production you definitely need to consider how you will secure the WCF end point so only SharePoint Online and the BCS can call it. Steve Fox has a good blog post and a link to a lab to go through the different options around this, this should definitely be your next step:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/b/steve_fox/archive/2012/02/07/sharepoint-online-and-windows-azure-developing-secure-bcs-connections-using-wcf-services.aspx" target="_blank"&gt;SharePoint Online and Windows Azure: Developing Secure BCS Connections using WCF Services&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hopefully this post has given you a good introduction of the steps needed to get data from CRM 2011 Online displayed in SharePoint Online through the Business Connectivity Services. There are a few steps required to get this working, but the good news is we are working on some tools and services to make this a whole lot easier. If you’d like more information on these tools and be an early beta tester please drop me an email : &lt;a href="mailto:nick@lightningtools.com"&gt;nick@lightningtools.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;nick/&amp;gt;&lt;/p&gt;&lt;img src="http://lightningtools.com/blog/aggbug/313.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Lightning Tools</dc:creator>
            <guid>http://lightningtools.com/blog/archive/2012/02/20/crm-2011-online-and-sharepoint-2010-integration-through-business-connectivity.aspx</guid>
            <pubDate>Mon, 20 Feb 2012 21:14:20 GMT</pubDate>
            <wfw:comment>http://lightningtools.com/blog/comments/313.aspx</wfw:comment>
            <comments>http://lightningtools.com/blog/archive/2012/02/20/crm-2011-online-and-sharepoint-2010-integration-through-business-connectivity.aspx#feedback</comments>
            <wfw:commentRss>http://lightningtools.com/blog/comments/commentRss/313.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
