Answered by:
CRM 2011 Plug-in: how to call third party web service from within the plug-in

Question
-
I need to write a plug-in on Create message of a Custom Entity. The plug-in should make a web service call to do required validations and return the message back. I would like to understand the steps involved to call a third party web service from within the plug-in.
Tuesday, March 1, 2011 6:50 AM
Answers
-
(Similar discussion is this thread: http://social.microsoft.com/Forums/sk-SK/crmdevelopment/thread/798074b4-0eb4-4a57-837d-d93f16f6c5ba)
I've just come across the WebPermission Security Exception and found this article helpful:
http://msdn.microsoft.com/en-us/library/gg334752.aspx
Sandboxed plug-ins can access the network through the HTTP and HTTPS protocols. This capability provides support for accessing popular Web resources like social sites, news feeds, Web services, and more. The following Web access restrictions apply to this sandbox capability.
- Only the HTTP and HTTPS protocols are allowed.
- Access to localhost (loopback) is not permitted.
- IP addresses cannot be used. You must use a named Web address that requires DNS name resolution.
I had used localhost as the url for the WCF service in my dev environment. Changing it to the server name solved the problem.
- Proposed as answer by DavidBerryMVP, Moderator Wednesday, September 28, 2011 7:08 AM
- Marked as answer by DavidBerryMVP, Moderator Thursday, October 6, 2011 5:23 PM
Tuesday, September 27, 2011 9:17 PM - Only the HTTP and HTTPS protocols are allowed.
All replies
-
I think, it should be like calling any ordinary webservice. Isn't it?
Add a web reference
Right Click web-reference (in Solution Explorer), Add Web Reference.
Instantiate the web service object
MyService wsMyService = new MyService();
Start using methods and properties
Label1.Text = wsMyService.GetValues();
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)Tuesday, March 1, 2011 7:09 AM -
If you are doing validation I would recommend calling your web service from javascript in the same form on save event and returning a message to show a notification bar with green, yellow, or red color displaying the returned message. The advantages of that are the speed and no post back to the server.
Islam Eldemery
http://idemery.net
Tuesday, March 1, 2011 9:00 AM -
Hi Sonuli,
Are u adding the reference to WCF or Webservice?
If it is WCF, add the "Service Reference"
if it is a custom webservice, go to Add Service Reference ==> Add WEbService Reference (a button is there on the dialog).
Type the url, click go and give it a name.
You have save yoru confiuration information like URL to the service in an MS CRM entity
Thanks, Ankit Shah
Inkey Solutions, India.
Microsoft Certified Business Management Solutions Professionals
http://www.inkeysolutions.com/MicrosoftDynamicsCRM.htmlTuesday, March 1, 2011 12:14 PM -
Thanks for youre response. I'm trying to use a web service built in JAVA. Moreover the web service is hosted somewhere on different server at remote location. If I add it as web reference and when I move my plug-in from my Dev environment to Test/QA/Prod etc.., do I need to change web reference one more time? Is there any place where I can put the web service url as config? Also do I need to get the proxy instead of Web Reference?Tuesday, March 1, 2011 3:58 PM
-
I have the same requirement. From the plug in i need to call the 3rd party Web services. So I have added the Service Reference. But The configuration is created in the App.Config. So all the end points are in the config file. But this is not working because the dll does not have the configuration data. I dont know how to add the configuration file to plug in.
Any one help is verymuch appriciated.
Thanks,
Jaya Kumar.
Monday, May 30, 2011 10:14 AM -
I suggest create a new entity that holds default configuration values and delete those configuration in the .config file, in your plugin retrieve the configuration from that entity and call your web service.
Islam Eldemery
http://idemery.net
- Proposed as answer by Islam Eldemery Monday, May 30, 2011 10:20 AM
Monday, May 30, 2011 10:19 AM -
1) You need to call the web service with a pre-event plugin. That way you'll ensure that the validation is run even if the form isn't opened. Javascript validation is fine for some purposes, but will only execute when the form is opened in the browser.
2) You need to pass the configuration parameters as an unsecure xml configuration string when you register the assembly in the plugin registration tool. That way you can use a different config for dev and prod, without the need to make changes to the code.
This class will make it easy to retrieve the configuration at runtime in your plugin:
class PluginConfiguration { private static string GetValueNode(XmlDocument doc, string key) { XmlNode node = doc.SelectSingleNode(String.Format("Settings/setting[@name='{0}']", key)); if (node != null) { return node.SelectSingleNode("value").InnerText; } return string.Empty; } public static Guid GetConfigDataGuid(XmlDocument doc, string label) { string tempString = GetValueNode(doc, label); if (tempString != string.Empty) { return new Guid(tempString); } return Guid.Empty; } public static bool GetConfigDataBool(XmlDocument doc, string label) { bool retVar; if (bool.TryParse(GetValueNode(doc, label), out retVar)) { return retVar; } return false; } public static int GetConfigDataInt(XmlDocument doc, string label) { int retVar; if (int.TryParse(GetValueNode(doc, label), out retVar)) { return retVar; } return -1; } public static string GetConfigDataString(XmlDocument doc, string label) { return GetValueNode(doc, label); } }
To use it, add the following constructor to your plugin class:
public YourPluginAssemblyConstructor(string unsecureConfig, string secureConfig) { ConfigXml = new XmlDocument(); ConfigXml.LoadXml(unsecureConfig); }
This loads the config from the database. To retrieve the settings at run time, use the following code:
string configValue= PluginConfiguration.GetConfigDataString(ConfigXml, "YourConfigValue");
Finally, you need to build your xml config document which you register along with the plugin:
You patse the xml into the textbox labeled "Unsecure configuration" in the Plugin registration tool<Settings> <setting name="YourConfigValue"> <value>blahblablah</value> </setting> </Settings>
- Proposed as answer by Torstein Utne Monday, May 30, 2011 10:29 AM
Monday, May 30, 2011 10:29 AM -
WCF Call getting me
System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Server stack trace:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Net.HttpWebRequest.CheckConnectPermission(Uri uri, Boolean needExecutionContext)
at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
at System.Net.HttpRequestCreator.Create(Uri Uri)
at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
at System.ServiceModel.Channels.HttpChannelFactory.GetWebRequest(EndpointAddress to, Uri via, NetworkCredential credential, TokenImpersonationLevel impersonationLevel, AuthenticationLevel authenticationLevel, SecurityTokenProviderContainer proxyTokenProvider, SecurityTokenContainer clientCertificateToken, TimeSpan timeout)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.GetWebRequest(EndpointAddress to, Uri via, SecurityTokenContainer clientCertificateToken, TimeoutHelper& timeoutHelper)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.GetWebRequest(EndpointAddress to, Uri via, TimeoutHelper& timeoutHelper)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)Exception rethrown at [0]:
at xx.xx.xx.Execute(IServiceProvider serviceProvider)
The action that failed was:
Demand
The type of the first permission that failed was:
System.Net.WebPermission
The Zone of the assembly that failed was:
MyComputerThursday, July 7, 2011 5:04 PM -
(Similar discussion is this thread: http://social.microsoft.com/Forums/sk-SK/crmdevelopment/thread/798074b4-0eb4-4a57-837d-d93f16f6c5ba)
I've just come across the WebPermission Security Exception and found this article helpful:
http://msdn.microsoft.com/en-us/library/gg334752.aspx
Sandboxed plug-ins can access the network through the HTTP and HTTPS protocols. This capability provides support for accessing popular Web resources like social sites, news feeds, Web services, and more. The following Web access restrictions apply to this sandbox capability.
- Only the HTTP and HTTPS protocols are allowed.
- Access to localhost (loopback) is not permitted.
- IP addresses cannot be used. You must use a named Web address that requires DNS name resolution.
I had used localhost as the url for the WCF service in my dev environment. Changing it to the server name solved the problem.
- Proposed as answer by DavidBerryMVP, Moderator Wednesday, September 28, 2011 7:08 AM
- Marked as answer by DavidBerryMVP, Moderator Thursday, October 6, 2011 5:23 PM
Tuesday, September 27, 2011 9:17 PM - Only the HTTP and HTTPS protocols are allowed.
-
You also may have to avoid using the DefaultNetworkCredentials with the sandbox connection; try specifying credentials for the connection directly.
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.Wednesday, September 28, 2011 7:08 AMModerator -
Sean,
You are the man. Saved my day :)
Regards.
Thursday, October 6, 2011 7:45 AM -
does this work for online plugin with credential passed explicitly to call a web service?Monday, January 30, 2012 6:10 PM