Here are the steps to connect to WCF Web Service from SharePoint Hosted Add-in.
- Create WCF Web Service using the template available in Visual Studio.
- In Controller class, write the below interface.
- [OperationContract]
- [WebGet(ResponseFormat = WebMessageFormat.Json,
- UriTemplate = "SaveDocument?url={docUrl}")]
- byte[] SaveDocument(string docUrl);
- Create a method in code behind file, as shown below.
- public byte[] SaveDocument(string docUrl)
- {
-
- }
Note
Return type should be same as declared in interface. Here, “SaveDocument” method will be called on accessing the Web Service with this URL -
http://localhost/IISFolder/Controller.svc/SaveDocument?url=" + "urlParameter";
- Call Web Service from SharePoint Hosted Add-in using function.
- function test() {
-
- var requestUriTest = "http://localhost/IISFolder/Controller.svc/SaveDocument?url=" + "urlParameter";
- $.support.cors = true;
- return $.ajax({
- url: requestUriTest,
- type: "GET",
- data: "",
- headers: {
- "accept": "application/json;odata=verbose"
- },
- ContentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function(data) {
-
- },
- error: function(data) {
-
- alert(data);
- }
- });
- }