CRM 4.0 Service Utility Class

CRM 4.0 service utility classs


// Classes to create Metadata Service & CRM Service
using System;
using System.Collections.Generic;
using System.Text;
using CrmSdk;
using MetadataServiceSdk;
 
namespace Microsoft.Crm.Sdk.Utility
{
   public class CrmServiceUtility
   {
      public static CrmService GetCrmService()
      {
         return GetCrmService(null, null);
      }
 
      public static CrmService GetCrmService(string organizationName)
      {
         return GetCrmService(null, organizationName);
      }
 
      /// <summary>
      /// Set up the CRM Service.
      /// </summary>
      /// <param name="organizationName">My Organization</param>
      /// <returns>CrmService configured with AD Authentication</returns>
      public static CrmService GetCrmService(string crmServerUrl, string organizationName)
      {
         // Get the CRM Users appointments
         // Setup the Authentication Token
         CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken();
         token.OrganizationName = organizationName;
      
         CrmService service = new CrmService();
 
         if (crmServerUrl != null &&
            crmServerUrl.Length > 0)
         {
            UriBuilder builder = new UriBuilder(crmServerUrl);            
            builder.Path = "//MSCRMServices//2007//CrmService.asmx";
            service.Url = builder.Uri.ToString();
         }
 
         service.Credentials = System.Net.CredentialCache.DefaultCredentials;
         service.CrmAuthenticationTokenValue = token;
 
         return service;
      }
 
      /// <summary>
      /// Set up the CRM Metadata Service.
      /// </summary>
      /// <param name="organizationName">My Organization</param>
      /// <returns>MetadataService configured with AD Authentication</returns>
      public static MetadataService GetMetadataService(string crmServerUrl, string organizationName)
      {
         // Get the CRM Users appointments
         // Setup the Authentication Token
         MetadataServiceSdk.CrmAuthenticationToken token = new MetadataServiceSdk.CrmAuthenticationToken();
         token.OrganizationName = organizationName;
 
         MetadataService service = new MetadataService();
 
         if (crmServerUrl != null &&
            crmServerUrl.Length > 0)
         {
            UriBuilder builder = new UriBuilder(crmServerUrl);            
            builder.Path = "//MSCRMServices//2007//MetadataService.asmx";
            service.Url = builder.Uri.ToString();
         }
         
         service.Credentials = System.Net.CredentialCache.DefaultCredentials;
         service.CrmAuthenticationTokenValue = token;
 
         return service;
      }
      
      /// <summary>
      /// Create a Crm label
      /// </summary>
      /// <param name="label">string label value for LocLabel</param>
      /// <param name="langCode">Language Code for CrmLabel</param>
      /// <returns></returns>
      public static MetadataServiceSdk.CrmLabel CreateSingleLabel(string label, int langCode)
      {
         MetadataServiceSdk.CrmNumber crmNumber = new MetadataServiceSdk.CrmNumber();
         crmNumber.Value = langCode;
 
         MetadataServiceSdk.LocLabel locLabel = new MetadataServiceSdk.LocLabel();
         locLabel.LanguageCode = crmNumber;
         locLabel.Label = label;
 
         MetadataServiceSdk.CrmLabel crmLabel = new MetadataServiceSdk.CrmLabel();
         crmLabel.LocLabels = new MetadataServiceSdk.LocLabel[] { locLabel };
 
         return crmLabel;
      }
   }
 
 
}