SharePoint Pals
 | Sign In
Making a REST Call to a Custom WCF Service from SharePoint 2013 Provider Hosted Application
by Sathish Nadarajan 19 Jun 2013
SharePoint MVP
Today  :   7     Total  :    17973

This article explains you how to make a REST call to a custom WCF service from a SharePoint 2013 Provider Hosted Application. In the previous Article, we saw how to create the WCF Service and test them from the browser. Now, let us have a look, how to invoke the service from our Provider Hosted Application and get some useful information from the Service.

To recall, our service has been created and by calling the url,


From the browser, we should be able to get the output as,

 <?xml version="1.0"?>
 <SampleServiceCallResponse xmlns="">

In the browser.

With this, let go to our Provider Hosted Application. Hope we had seen, enough information regarding the provider hosted application in our previous articles.

 //Button Click Event
 protected void btnSample_OnClick(object sender, EventArgs e)
     string SPHostUrl = Request.QueryString["SPHostUrl"];
     string strAccessToken;
         Uri hostWeb = new Uri(SPHostUrl);
 //Trying to get the Accesstoken from the CustomTokenHelper Class.  The //CustomTokenHelperClass is attached with this Article.
         strAccessToken = TokenHelper.GetS2SClientContextWithClaimsIdentity(hostWeb,
             TokenHelper.IdentityClaimType.SMTP, TokenHelper.ClaimProviderType.SAML, true);
         //For Ace Scenario, we will be using the IdentityClaimType.UPN.  We are using SMTP for TEst Purpose
     catch (Exception ex)
 public void SampleServiceCall (string AccessToken)
     string strUri = “https://MyServer/Sites/SiteCollection/_layouts/15/SharePoint.WCFService.Sample/Services/SampleService.svc/SampleServiceCall(test)“
     Uri uri = new Uri(strUri);
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
     request.Credentials = CredentialCache.DefaultCredentials;
     request.Accept = "application/atom+xml";
     request.Headers.Add("Authorization", "Bearer " + AccessToken);
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     StreamReader reader = new StreamReader(response.GetResponseStream());
     XDocument doc = XDocument.Load(reader);

From the Provider Hosted Application, say for example, we are going to call REST service on a button click.

To get the AccessToken, we are using the CustomTokenHelper class which was downloaded from Steve Peschka’s Article. The method GetS2SClientContextWithClaimsIdentity will return the ClientContext by Default. But we need to overload the method to return the AccessToken. The overloaded method can be something like,

 public static string GetS2SClientContextWithClaimsIdentity(
             Uri targetApplicationUri,
             System.Security.Principal.IPrincipal UserPrincipal,
             IdentityClaimType UserIdentityClaimType,
             ClaimProviderType IdentityClaimProviderType, bool AccessToken)
     //get the identity claim info first
     TokenHelper.ClaimsUserIdClaim id = RetrieveIdentityForSamlClaimsUser(UserPrincipal, UserIdentityClaimType);
     string realm = string.IsNullOrEmpty(Realm) ? GetRealmFromTargetUrl(targetApplicationUri) : Realm;
     JsonWebTokenClaim[] claims = UserPrincipal != null ? GetClaimsWithClaimsIdentity(UserPrincipal, UserIdentityClaimType, id, IdentityClaimProviderType) : null;
     string accessToken = GetS2SClaimsAccessTokenWithClaims(targetApplicationUri.Authority, realm, claims, id.ClaimsIdClaimType, id.ClaimsIdClaimValue);
     return accessToken;

The CustomClaimsToken helper class is attached with this article. Hope all of you remember how to use the tokenhelper class from the previous articles.

Download SourceCode

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation