简体   繁体   中英

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.

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".

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;   
            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM