Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using System.Web.Services;

using System.Net;

using Microsoft.Crm.Sdk;

using Microsoft.Crm.SdkTypeProxy;

using Microsoft.Crm.SdkTypeProxy.Metadata;

using PhilipRichardson.Org.Crm.Tools.CrmDisco;

 

/* SUPPORT: This code itself is unsupported by me. No exceptions.

 * HOWEVER: The techniques shown in this code are supported mechanisms for accessing Microsoft CRM

 *

 *

 * License: Creative Commons

 * Summary: http://creativecommons.org/licenses/by-nc-nd/3.0/us/

 * Full License: http://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode

 *

 * Attribution-Noncommercial-No Derivative Works 3.0 United States

 *

 * You are free to:

 *  to Share — to copy, distribute, display, and perform the work

 * 

 * Under the following conditions:

 *  Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

 *  Noncommercial. You may not use this work for commercial purposes.

 *  No Derivative Works. You may not alter, transform, or build upon this work.

 * 

 * For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.

 * Any of the above conditions can be waived if you get permission from the copyright holder.

 * Nothing in this license impairs or restricts the author's moral rights.

 *

 *

 *

 */

 

 

namespace PhilipRichardson.Org.Crm.Tools

{

    public class CrmConnect

    {

        CrmService service = new CrmService();

        MetadataService meta = new MetadataService();

        string webappurl;

        string friendlyname;

        Guid organizationid;

        string organizationname;

 

        /// <summary>

        /// Integrated Authentication

        /// </summary>

        /// <param name="server"></param>

        /// <param name="orgname"></param>

        public CrmConnect(string server, string orgname)

        {

            //Discovery

            CrmDiscoveryService disco = new CrmDiscoveryService();

            disco.Credentials = CredentialCache.DefaultCredentials;

            disco.Url = server + "/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";

            RetrieveOrganizationsRequest orgrequest = new RetrieveOrganizationsRequest();

            RetrieveOrganizationsResponse orgresponse = (RetrieveOrganizationsResponse)disco.Execute(orgrequest);

 

            OrganizationDetail myorg = new OrganizationDetail();

 

            foreach (OrganizationDetail org in orgresponse.OrganizationDetails)

            {

                if (org.OrganizationName.ToLower() == orgname)

                {

                    myorg = org;

                    break;

                }

            }

 

            //Token

            CrmAuthenticationToken token = new CrmAuthenticationToken();

            token.AuthenticationType = 0;

            token.OrganizationName = myorg.OrganizationName;

 

            //Services

            service = new CrmService();

            service.CrmAuthenticationTokenValue = token;

            service.Credentials = CredentialCache.DefaultCredentials;

            service.Url = myorg.CrmServiceUrl;

            meta = new MetadataService();

            meta.CrmAuthenticationTokenValue = token;

            meta.Credentials = CredentialCache.DefaultCredentials;

            meta.Url = myorg.CrmMetadataServiceUrl;

 

            //Other Properties

            webappurl = myorg.WebApplicationUrl;

            friendlyname = myorg.FriendlyName;

            organizationid = myorg.OrganizationId;

            organizationname = orgname;

        }

 

        /// <summary>

        /// Windows Authentication

        /// </summary>

        /// <param name="server"></param>

        /// <param name="orgname"></param>

        /// <param name="domain"></param>

        /// <param name="username"></param>

        /// <param name="password"></param>

        public CrmConnect(string server, string orgname, string domain, string username, string password)

        {

            //Network Credential

            NetworkCredential cred = new NetworkCredential();

            cred.Domain = domain;

            cred.UserName = username;

           

            //Discovery

            CrmDiscoveryService disco = new CrmDiscoveryService();

            disco.Credentials = cred;

            disco.Url = server + "/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";

            RetrieveOrganizationsRequest orgrequest = new RetrieveOrganizationsRequest();

            RetrieveOrganizationsResponse orgresponse = (RetrieveOrganizationsResponse)disco.Execute(orgrequest);

 

            OrganizationDetail myorg = new OrganizationDetail();

 

            foreach (OrganizationDetail org in orgresponse.OrganizationDetails)

            {

                if (org.OrganizationName.ToLower() == orgname)

                {

                    myorg = org;

                    break;

                }

            }

 

            //Token

            CrmAuthenticationToken token = new CrmAuthenticationToken();

            token.AuthenticationType = 0;

            token.OrganizationName = myorg.OrganizationName;

 

            //Services

            service = new CrmService();

            service.CrmAuthenticationTokenValue = token;

            service.Credentials = cred;

            service.Url = myorg.CrmServiceUrl;

            meta = new MetadataService();

            meta.CrmAuthenticationTokenValue = token;

            meta.Credentials = cred;

            meta.Url = myorg.CrmMetadataServiceUrl;

 

            //Other Properties

            webappurl = myorg.WebApplicationUrl;

            friendlyname = myorg.FriendlyName;

            organizationid = myorg.OrganizationId;

            organizationname = orgname;

        }

 

        /// <summary>

        /// Internet Facing Deployment (IFD)

        /// </summary>

        /// <param name="server"></param>

        /// <param name="orgname"></param>

        /// <param name="username"></param>

        /// <param name="password"></param>

        public CrmConnect(string server, string orgname, string username, string password)

        {

            //Discovery

            CrmDiscoveryService disco = new CrmDiscoveryService();

            disco.Url = server + "/MSCRMServices/2007/IFD/CrmDiscoveryService.asmx";

            RetrieveOrganizationsRequest orgrequest = new RetrieveOrganizationsRequest();

            orgrequest.UserId = username;

            orgrequest.Password = password;

            RetrieveOrganizationsResponse orgresponse = (RetrieveOrganizationsResponse)disco.Execute(orgrequest);

 

            OrganizationDetail myorg = new OrganizationDetail();

 

            foreach (OrganizationDetail org in orgresponse.OrganizationDetails)

            {

                if (org.OrganizationName.ToLower() == orgname)

                {

                    myorg = org;

                    break;

                }

            }

      

            //Ticket

            RetrieveCrmTicketRequest ticketrequest = new RetrieveCrmTicketRequest();

            ticketrequest.OrganizationName = myorg.OrganizationName;

            ticketrequest.UserId = username;

            ticketrequest.Password = password;

            RetrieveCrmTicketResponse ticketresponse = (RetrieveCrmTicketResponse)disco.Execute(ticketrequest);

 

            //Token

            CrmAuthenticationToken token = new CrmAuthenticationToken();

            token.CrmTicket = ticketresponse.CrmTicket;

            token.AuthenticationType = 2;

            token.OrganizationName = myorg.OrganizationName;

 

            //Services

            service = new CrmService();

            service.CrmAuthenticationTokenValue = token;

            service.Url = myorg.CrmServiceUrl;

            meta = new MetadataService();

            meta.CrmAuthenticationTokenValue = token;

            meta.Url = myorg.CrmMetadataServiceUrl;

 

            //Other Properties

            webappurl = myorg.WebApplicationUrl;

            friendlyname = myorg.FriendlyName;

            organizationid = myorg.OrganizationId;

            organizationname = orgname;

        }

       

        /// <summary>

        /// CRM Live

        /// </summary>

        /// <param name="orgname"></param>

        /// <param name="passportticket"></param>

        public CrmConnect(string orgname, string passportticket, bool production)

        {

            //Discovery

            CrmDiscoveryService disco = new CrmDiscoveryService();

            if (production == true)

            {

                disco.Url = "https://dev.crm.dynamics.com/mscrmservices/2007/passport/CrmDiscoveryService.asmx";

            }

            else

            {

                //Not Implemented for Non-Production Live Instances

            }

            RetrieveOrganizationsRequest orgrequest = new RetrieveOrganizationsRequest();

            orgrequest.PassportTicket = passportticket;

            RetrieveOrganizationsResponse orgresponse = (RetrieveOrganizationsResponse)disco.Execute(orgrequest);

 

            OrganizationDetail myorg = new OrganizationDetail();

 

            foreach (OrganizationDetail org in orgresponse.OrganizationDetails)

            {

                if (org.OrganizationName.ToLower() == orgname)

                {

                    myorg = org;

                    break;

                }

            }

 

            //Ticket

            RetrieveCrmTicketRequest ticketrequest = new RetrieveCrmTicketRequest();

            ticketrequest.OrganizationName = myorg.OrganizationName;

            ticketrequest.PassportTicket = passportticket;

            RetrieveCrmTicketResponse ticketresponse = (RetrieveCrmTicketResponse)disco.Execute(ticketrequest);

 

            //Token

            CrmAuthenticationToken token = new CrmAuthenticationToken();

            token.CrmTicket = ticketresponse.CrmTicket;

            token.AuthenticationType = 1;

            token.OrganizationName = myorg.OrganizationName;

 

            //Services

            service = new CrmService();

            service.CrmAuthenticationTokenValue = token;

            service.Url = myorg.CrmServiceUrl;

            meta = new MetadataService();

            meta.CrmAuthenticationTokenValue = token;

            meta.Url = myorg.CrmMetadataServiceUrl;

 

            //Other Properties

            webappurl = myorg.WebApplicationUrl;

            friendlyname = myorg.FriendlyName;

            organizationid = myorg.OrganizationId;

            organizationname = orgname;

        }

       

        /// <summary>

        /// Standard CRM Web Service

        /// </summary>

        public CrmService CrmService

        {

            get

            {

                return service;

            }

        }

 

        /// <summary>

        /// CRM Metadata Web Service

        /// </summary>

        public MetadataService MetadataService

        {

            get

            {

                return meta;

            }

        }

 

        /// <summary>

        /// The URL of the CRM Web Application

        /// </summary>

        public string WebAppUrl

        {

            get

            {

                return WebAppUrl;

            }

        }

 

        /// <summary>

        /// The Friendly 'Long' Name of the Organization

        /// </summary>

        public string FriendlyName

        {

            get

            {

                return friendlyname;

            }

        }

 

        /// <summary>

        /// The GUID of the Organization

        /// </summary>

        public Guid OrganizationId

 &nbs