Unauthorized error for ISAPI service of sharepoint

I have create ISAPI service in "ISAPI" folder of hive.

Now I want to access this service using rest api. but I'm getting unauthorized error when try to call it.

IDemoService.cs code (Interface file):

namespace ISAPIDemo.ISAPI
{
    [ServiceContract]
    public interface IDemoService
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "DemoServiceCall({SampleValue})")]
        string DemoServiceCall(string SampleValue);
    }
}

DemoService.svc code:

<%@ ServiceHost Language="C#" Debug="true"
    Service="ISAPIDemo.DemoService"
    CodeBehind="DemoService.svc.cs"
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory,
    Microsoft.SharePoint.Client.ServerRuntime, Version=15.0.0.0, Culture=neutral,
    PublicKeyToken=71e9bce111e9429c" %>

DemoService.svc.cs code:

namespace ISAPIDemo.ISAPI
{
    [BasicHttpBindingServiceMetadataExchangeEndpoint]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public sealed class SampleService : IDemoService
    {            
        public string DemoServiceCall(string SampleValue)
        {
            // Write the string to a file.
            System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
            file.WriteLine("In Demo Service");

            file.Close();
            return "test";
        }

    }
}

js code:

$(document).ready(function () {
    getISAPIData();
});

function getISAPIData() {   
    var serviceUri = "http://xxxxxxxxx/_vti_bin/DemoService.svc/DemoServiceCall(test)";
    $.ajax({
        type: "GET",
        contentType: "application/json",
        url: serviceUri,
        dataType: "json",
        success:
            function (response) {
                alert('Success' + response);                
            },
        error:
            function (err) {
                alert('Error ' + err);
            }
    });
}

these much code I have written for service and call it into js. Is it any thing that I had missed?




July 17th, 2015 9:20am

Hi,

you are getting unauthorized as you have not specify the the authentication in your call.

use the below link to know as how to pass the authentication

http://stackoverflow.com/questions/1002179/how-can-i-pass-windows-authentication-to-webservice-using-jquery

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 9:46am

Hi Nikhil,

I have followed steps as mention in link but I'm still getting unauthorized error.

I have create one more service and hosted it in layout folder instead of ISAPI folder. It is allowing to make call using ajax. but ISAPI service will not.
July 17th, 2015 11:09am

Hi,

Please try to add an empty web.config file to your mapped ISAPI directory in your SharePoint project, in that new web.config file, add the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
    <location path="WebService.asmx">
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <add name="Access-Control-Allow-Origin" value="*" />
                </customHeaders>
            </httpProtocol>
        </system.webServer>
        <system.web>
            <webServices>
                <protocols>
                    <add name="HttpGet" />
                    <add name="HttpPost" />
                    <add name="HttpPostLocalhost" />
                </protocols>
            </webServices>
            <customErrors mode="On"/>
        </system.web>
    </location>
</configuration>

https://www.infotekka.com/articles/22

If the issue still exists, you can also try to test the web service in the C# code or use Fiddler to monitor the requests and responses massages.

Best Regards

Free Windows Admin Tool Kit Click here and download it now
August 2nd, 2015 10:55pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics