Calling WCF Rest Service from a Plugin in CRM 2011 (Online)

Let us take a simple rest service example here

http://www.rockycherry.net/services/Photos/photos.svc/photos

It returns the string “You have asked for photos”.

Within the plugin class add references to following assemblies.

The sample code of the plugin


namespace TestPlugin
{
 public class Class1 : IPlugin
 {
 public void Execute(IServiceProvider serviceProvider)
 {
 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
 {

// using WebClient
 WebClient webClient1 = new WebClient();
 string data1 = webClient1.DownloadString("http://www.rockycherry.net/services/Photos/photos.svc/photos");

// using WebChannelFactory
 WebChannelFactory<IPhotos> factory = new WebChannelFactory<IPhotos>(new Uri("http://www.rockycherry.net/services/Photos/photos.svc"));
 IPhotos proxy = factory.CreateChannel();
 string photos = proxy.GetPhotos();

 }

 }
 }

[ServiceContract]
 public interface IPhotos
 {
 [WebGet(UriTemplate = "Photos")]
 [OperationContract]
 string GetPhotos();
 }
}


Register the plugin in Sandbox mode for CRM 2011 online.

Check this wonderful post for all the details

http://rockycherry.net/blog/?p=179

Hope it helps.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

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