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,
https://MyServer/Sites/SiteCollection/_layouts/15/SharePoint.WCFService.Sample/Services/SampleService.svc/SampleServiceCall(test)
From the browser, we should be able to get the output as,
 <?xml version="1.0"?>
 <SampleServiceCallResponse xmlns="http://tempuri.org/">
  <SampleServiceCallResult>Success</SampleServiceCallResult>
 </SampleServiceCallResponse>
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;
try
{
TokenHelper.TrustAllCertificates();
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,
HttpContext.Current.User,
TokenHelper.IdentityClaimType.SMTP, TokenHelper.ClaimProviderType.SAML, true);
//For Ace Scenario, we will be using the IdentityClaimType.UPN. We are using SMTP for TEst Purpose
SampleServiceCall(strAccessToken);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
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);
Response.Write(doc.Root.Value.ToString());
}
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.
Leave a comment