Get The Server URL in Silverlight using REST Endpoint with Silverlight Web Resources

Get The Server URL in Silverlight using REST Endpoint with Silverlight Web Resources

getServerUrlfromContext()

{

 serverUrl = (String)GetContext().Invoke(“getServerUrl”);

   //Remove the trailing forward 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);

   Uri ODataUri = new Uri(serverUrl + “/xrmservices/2011/organizationdata.svc/”, UriKind.Absolute);

   context = new crmContext(ODataUri) { IgnoreMissingProperties = true };

}

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;

  }

 }

}

 // Cal the getServerUrlfromContext() in Main Page function as follows

 public MainPage()

{

         InitializeComponent();

         getServerUrlfromContext();

}

 Cheers,

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s