Wednesday, 26 June 2013

Use CRM 2011 web service in Silverlight 5

In the silverlight application add CRM 2011 webservice as Service Reference.

The CRM 2011 organization url will be like below.

http://CRMServer/OrgName/XRMServices/2011/Organization.svc?wsdl

Include the Service reference name in the class where we create the helper methods to get the Iorganizationservice object.

Use below helper methods in the class to your project.

internal static class SilverlightUtility
{
   public static IOrganizationService GetSoapService()
   {
    Uri serviceUrl = CombineUrl(GetServerBaseUrl(),"/XRMServices/2011/Organization.svc/web");
     BasicHttpBinding binding = new BasicHttpBinding(Uri.UriSchemeHttps == serviceUrl.Scheme
     ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.TransportCredentialOnly);
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.MaxBufferSize = int.MaxValue;
            binding.SendTimeout = TimeSpan.FromMinutes(2);

    return new CrmSdk.OrganizationServiceClient(binding, new EndpointAddress(serviceUrl));
 }

 public static Uri GetServerBaseUrl()
 {
   string serverUrl = (string)GetContext().Invoke("getServerUrl");
  //Remove the trailing forwards slash returned by CRM Online
  //So that it is always consistent with CRM On Premises
  if (serverUrl.EndsWith("/"))serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
  return new Uri(serverUrl);
 }

 public static Uri CombineUrl(Uri baseValue, string value)
 {
    if (null == baseValue)
    {
      throw new ArgumentNullException("baseValue");
    }
    else if (string.IsNullOrEmpty(value))
    {
      return baseValue;
    }

    //Ensure that a double '/' is not being added
    string newValue = baseValue.AbsoluteUri;
    if (!newValue.EndsWith("/", StringComparison.Ordinal))
    {
      //Check if there is a character at the beginning of value
      if (!value.StartsWith("/", StringComparison.Ordinal))
      {
         newValue += "/";
      }
    }
    else if (value.StartsWith("/", StringComparison.Ordinal))
    {
      value = value.Substring(1);
    }

    //Create the combined URL
    return new Uri(newValue + value);
 }

 #region Private Methods
 private static ScriptObject GetContext()
 {
   ScriptObject xrmProperty = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
   if (null == xrmProperty)
   {
    //It may be that the global context should be used
     try
     {
       ScriptObject globalContext = (ScriptObject)HtmlPage.Window.Invoke("GetGlobalContext");
       return globalContext;
     }
     catch (System.InvalidOperationException)
     {
       throw new InvalidOperationException("Property \"Xrm\" is null and the Global Context 
       is not available.");
     }

   }

   ScriptObject pageProperty = (ScriptObject)xrmProperty.GetProperty("Page");
   if (null == pageProperty)
   {
       throw new InvalidOperationException("Property \"Xrm.Page\" is null");
   }

   ScriptObject contextProperty = (ScriptObject)pageProperty.GetProperty("context");
   if (null == contextProperty)
   {
     throw new InvalidOperationException("Property \"Xrm.Page.context\" is null");
   }

   return contextProperty;
 }
 #endregion


}

 Now you can access the GetSoapService method to get the IOrganization method.

IOrganizationService service = SilverlightUtility.GetSoapService();

No comments:

Post a Comment