Export (0) Print
Expand All

Utility Sample Code

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

The SDK sample download contains a utility file together with this helper code. It can be found here:

    SDK\Server\Reference\
    SDK\Server\HowTo\

This code contains the following methods.

Method NameDescription
GetCrmServiceReturns an instance of the CrmService class. There are several overloads where you can specify the URL or the organization name.
GetMetadataServiceReturns an instance of the MetadataService class.
CreateSingleLabelCreates a label given the string and a language code.

[C#]
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;
      }
   }


}
[Visual Basic .NET]
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports CrmSdk
Imports MetadataServiceSdk

Namespace Microsoft.Crm.Sdk.Utility
   Public Class CrmServiceUtility
      Public Shared Function GetCrmService() As CrmService
         Return GetCrmService(Nothing, Nothing)
      End Function

      Public Shared Function GetCrmService(ByVal organizationName As String) As CrmService
         Return GetCrmService(Nothing, organizationName)
      End Function

      ''' <summary>
      ''' Set up the CRM Service.
      ''' </summary>
      ''' <param name="organizationName">My Organization</param>
      ''' <returns>CrmService configured with AD Authentication</returns>
      Public Shared Function GetCrmService(ByVal crmServerUrl As String, ByVal organizationName As String) As CrmService
         ' Get the CRM Users appointments
         ' Setup the Authentication Token
         Dim token As New CrmSdk.CrmAuthenticationToken()
         token.OrganizationName = organizationName

         Dim service As New CrmService()

         If crmServerUrl IsNot Nothing AndAlso crmServerUrl.Length > 0 Then
            Dim builder As New UriBuilder(crmServerUrl)
            builder.Path = "//MSCRMServices//2007//CrmService.asmx"
            service.Url = builder.Uri.ToString()
         End If

         service.Credentials = System.Net.CredentialCache.DefaultCredentials
         service.CrmAuthenticationTokenValue = token

         Return service
      End Function

      ''' <summary>
      ''' Set up the CRM Metadata Service.
      ''' </summary>
      ''' <param name="organizationName">My Organization</param>
      ''' <returns>MetadataService configured with AD Authentication</returns>
      Public Shared Function GetMetadataService(ByVal crmServerUrl As String, ByVal organizationName As String) As MetadataService
         ' Get the CRM Users appointments
         ' Setup the Authentication Token
         Dim token As New MetadataServiceSdk.CrmAuthenticationToken()
         token.OrganizationName = organizationName

         Dim service As New MetadataService()

         If crmServerUrl IsNot Nothing AndAlso crmServerUrl.Length > 0 Then
            Dim builder As New UriBuilder(crmServerUrl)
            builder.Path = "//MSCRMServices//2007//MetadataService.asmx"
            service.Url = builder.Uri.ToString()
         End If

         service.Credentials = System.Net.CredentialCache.DefaultCredentials
         service.CrmAuthenticationTokenValue = token

         Return service
      End Function

      ''' <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 Shared Function CreateSingleLabel(ByVal label As String, ByVal langCode As Integer) As MetadataServiceSdk.CrmLabel
         Dim crmNumber As New MetadataServiceSdk.CrmNumber()
         crmNumber.Value = langCode

         Dim locLabel As New MetadataServiceSdk.LocLabel()
         locLabel.LanguageCode = crmNumber
         locLabel.Label = label

         Dim crmLabel As New MetadataServiceSdk.CrmLabel()
         crmLabel.LocLabels = New MetadataServiceSdk.LocLabel() { locLabel }

         Return crmLabel
      End Function
   End Class


End Namespace

© 2010 Microsoft Corporation. All rights reserved.


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft