CRM 2011 Online Plugin calling Authenticated Web Services

The ability to calls external web resources (sites and services) from within a CRM 2011 online plugin (running in the sandbox) is a major piece of integration kit.  The fact that the ability to do it in practice has been up in the air during the CRM 2011beta phase has been a bit of a worry.

The  code snippet below (from a CRM Online plugin) allows me to succesfully connect to a remote web service requiring Basic/API Realm credentials.

WebRequest request = WebRequest.Create(@”https://www.someexternalsite.com/WebServiceAPI/type”); request.UseDefaultCredentials = false; request.Credentials = new System.Net.NetworkCredential(“myusername”, “mypassword”); WebResponse response = request.GetResponse();

So long as you follow the plugin sandbox directives then you should be OK referencing your external resources. Thanks to Eric Pool for pointing out that you cant call a web service from an online plugin using default credentials. I neglected to explicitly set UseDefaultCredentials = false resulting in minor heart failure.