user4612290
user4612290

Reputation: 321

How to connect to CRM 2015 on-premise from a Siverlight App without depending on the Context?

I'm creating a Silverlight application that should retrieve data from the CRM. I tried the tutorial here but I failed to debug my application in Visual Studio due to the invalidity of the Context when GetServerBaseUrl is called

Uri serviceUrl = CombineUrl(GetServerBaseUrl(), "/XRMServices/2011/Organization.svc/web");

I understand that I can connect to the CRM using a connection string and using dlls from the SDK from this question, however the first link provided is broken and i can't see examples.

Upvotes: 1

Views: 471

Answers (2)

Kevin Ross
Kevin Ross

Reputation: 7215

In addition to Henk's answer here is a modified version of the function we use that works in with the old and new methods and finally falls back to using a hardcoded value. This is allows us to debug in visual studio without having to deploy to CRM

public static string GetServerBaseUrl(string FallbackValue = null)
    {


        try
        {
            string serverUrl = (string)GetContext().Invoke("getClientUrl");
            //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 serverUrl;
        }
        catch
        {
            //Try the old getServerUrl
            try
            {
                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 serverUrl;
            }
            catch
            {
                   return FallbackValue;   
            }
        }

    }

Upvotes: 1

Henk van Boeijen
Henk van Boeijen

Reputation: 7918

The code applies to Dynamics CRM 2011 and uses function getServerUrl. The function was declared obsolete for CRM 2011 already and has been removed from Dynamics CRM 2015.

Luckily you only have to make a small modification to the sample code:

public static Uri GetServerBaseUrl()
{
    string serverUrl = (string)GetContext().Invoke("getClientUrl");
    //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);
}

Here the literal "getServerUrl" was replaced by "getClientUrl".

Upvotes: 1

Related Questions