Export (0) Print
Expand All

CrmService Sample Code

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

This sample demonstrates the following concepts:

  • A helper method to get the CrmService Web service for an organization.
  • A helper method to retrieve the error code from a SoapException.
  • Use of the GetTimeZoneCodeByLocalizedName message.
  • Use of the GetAllTimeZonesWithDisplayName message.

This sample code can be found in the following file in the SDK download: Server\HowTo\CS\CrmServiceUtility.cs

Note    When you add the source file CrmServiceUtility.cs to your project, you must change the namespace name to match your project namespace in order for it to compile. Alternatively, you can cut and paste the class below into your code because it does not contain a namespace.

Example

[C#]
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 Microsoft Dynamics CRM users' appointments.
      // Set up 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 Microsoft Dynamics CRM users' appointments.
      // Set up 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;
   }
}

See Also

Concepts

Reference

 


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft