Export (0) Print
Expand All

Upgrading Your Existing Code

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

This topic shows a sample from Microsoft Dynamics CRM 3.0 followed by the upgraded version, indicating the changes you need to make for this version. This sample shows how to convert a fax to a task. The code first creates an incoming Fax activity, and then converts it to a Follow-up Task with a due date a week after it was received.

Example for Microsoft Dynamics CRM 3.0

[C#]
using System;
using CrmSdk;

namespace Microsoft.Crm.Sdk.HowTo
{
   /// <summary>
   /// This sample shows how to convert a fax into a task.
   /// </summary>
   public class ConvertFaxToTask
   {
      public ConvertFaxToTask()
      {         
      }

      public static bool Run()
      {
         #region Setup Data Required for this Sample
         bool success = false;
         #endregion

         try
         {
            // Set up the CRM Service.
            CrmService service = new CrmService();
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;
         
            // Get the current user.
            WhoAmIRequest userRequest = new WhoAmIRequest();
            WhoAmIResponse user = (WhoAmIResponse) service.Execute(userRequest);

            // Create the fax object.
            fax fax = new fax();

            // Set the properties of the fax.
            fax.subject = "Test Fax";
            fax.description = "New Fax";

            // Create the party sending and receiving the fax.
            activityparty party = new activityparty();

            // Set the properties of Activityparty.
            party.partyid = new Lookup();
            party.partyid.type = EntityName.systemuser.ToString();
            party.partyid.Value = user.UserId;

            // The party sends and receives the fax.
            fax.from = new activityparty[] {party};
            fax.to = new activityparty[] {party};

            // Create the fax.
            Guid createdFaxId = service.Create(fax);

            // Retrieve the created fax.
            // Be aware that using AllColumns may adversely affect
            // performance and cause unwanted cascading in subsequent 
            // updates. A best practice is to retrieve the least amount of 
            // data required.
            fax newFax = (fax) service.Retrieve(EntityName.fax.ToString(),
                                                createdFaxId, new AllColumns());
            // Create the task object.
            task task = new task();

            // Set the properties of the task.
            task.subject = "Follow Up: " + newFax.subject;

            // Due date of the task.
            task.scheduledend = new CrmDateTime();

            // Get the date that the fax was received.
            DateTime endDate = DateTime.Parse(newFax.createdon.date);

            // Add one week to get the date that the task is due.
            endDate = endDate.AddDays(7);

            // Set the due date of the task.
            task.scheduledend.Value = endDate.ToString();

            // Create the task.
            Guid createdTaskId = service.Create(task);
      
            #region check success
            if (createdTaskId != Guid.Empty)
            {
               success = true;
            }
            #endregion

            #region Remove Data Required for this Sample
            service.Delete(EntityName.fax.ToString(), createdFaxId);
            service.Delete(EntityName.task.ToString(), createdTaskId);
            #endregion
         }
         catch (System.Web.Services.Protocols.SoapException)
         {
            // Add your error handling code here.
         }

         return success;
      }
   }
}

Example for Microsoft Dynamics CRM 4.0

[C#]
using System;
using Microsoft.Crm.Sdk.Utility;
using CrmSdk;

namespace Microsoft.Crm.Sdk.HowTo
{
   public class ConvertFaxToTask
   {
      static void Main(string[] args)
      {
         // TODO: Change the server URL and Organization to match your Microsoft Dynamics CRM Server and Microsoft Dynamics CRM organization.
         ConvertFaxToTask.Run("http://localhost:5555", "CRM_SDK");
      }

      public static bool Run(string crmServerUrl, string orgName)
      {
         #region Setup Data Required for this Sample

         bool success = false;

         #endregion

         try
         {
            // Set up the CRM Service.
            CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
            service.PreAuthenticate = true;
            
            // Get the current user.
            WhoAmIRequest userRequest = new WhoAmIRequest();
            WhoAmIResponse user = (WhoAmIResponse)service.Execute(userRequest);

            // Create the fax object.
            fax fax = new fax();

            // Set the properties of the fax.
            fax.subject = "Test Fax";
            fax.description = "New Fax";

            // Create the party sending and receiving the fax.
            activityparty party = new activityparty();

            // Set the properties of Activityparty.
            party.partyid = new Lookup();
            party.partyid.type = EntityName.systemuser.ToString();
            party.partyid.Value = user.UserId;

            // The party sends and receives the fax.
            fax.from = new activityparty[] { party };
            fax.to = new activityparty[] { party };

            // Create the fax.
            Guid createdFaxId = service.Create(fax);

            // Retrieve the created fax.
            // Be aware that using AllColumns may adversely affect
            // performance and cause unwanted cascading in subsequent 
            // updates. A best practice is to retrieve the least amount of 
            // data required.
            fax newFax = (fax)service.Retrieve(EntityName.fax.ToString(), createdFaxId, new AllColumns());

            // Create the task object.
            task task = new task();

            // Set the properties of the task.
            task.subject = "Follow Up: " + newFax.subject;

            // Set the due date of the task.
            task.scheduledend = new CrmDateTime();

            // Get the date that the fax was received.
            DateTime endDate = DateTime.Parse(newFax.createdon.date);

            // Add one week to get the date that the task is due.
            endDate = endDate.AddDays(7);

            // Set the due date of the task.
            task.scheduledend.Value = endDate.ToString();

            // Create the task.
            Guid createdTaskId = service.Create(task);

            #region check success

            if (createdTaskId != Guid.Empty)
            {
               success = true;
            }

            #endregion

            #region Remove Data Required for this Sample

            service.Delete(EntityName.fax.ToString(), createdFaxId);
            service.Delete(EntityName.task.ToString(), createdTaskId);

            #endregion
         }
         catch (System.Web.Services.Protocols.SoapException)
         {
            // Add your error handling code here.
         }

         return success;
      }
   }
}
[C#]

The following is the code for the helper classes to get the Web 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 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.
         // 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;
      }
   }


}

See Also

Other Resources

Upgrading from Microsoft Dynamics CRM 3.0


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2015 Microsoft