Getting Authentication Prompt When Accessing SharePoint Web Services

You have a SharePoint site that is configured to be accessible by anonymous users, and the site's permissions are also configured to allow anonymous users. 

You create a JavaScript based web part to access SharePoint's web services, pull list items, and display them using the GetListItems SOAP 1.2 call. 

For some reason, users are prompted to authenticate when accessing your site. 

I discovered that the fix for this is in the web request content type.

If you set the content type to "text/xml; charset=utf-8" as in the SOAP 1.1 envelope sample, SharePoint won't prompt you to authenticate.



Here's the /_vti_bin/lists.asmx?op=GetListItems output for reference:

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
POST /_vti_bin/lists.asmx HTTP/1.1
Host: moss2007hv.kor.cmil.mil
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/GetListItems"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <listName>string</listName>
      <viewName>string</viewName>
      <query>
        <xsd:schema>schema</xsd:schema>xml</query>
      <viewFields>
        <xsd:schema>schema</xsd:schema>xml</viewFields>
      <rowLimit>string</rowLimit>
      <queryOptions>
        <xsd:schema>schema</xsd:schema>xml</queryOptions>
      <webID>string</webID>
    </GetListItems>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <GetListItemsResult>
        <xsd:schema>schema</xsd:schema>xml</GetListItemsResult>
    </GetListItemsResponse>
  </soap:Body>
</soap:Envelope>

SOAP 1.2
POST /_vti_bin/lists.asmx HTTP/1.1
Host: moss2007hv.kor.cmil.mil
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <listName>string</listName>
      <viewName>string</viewName>
      <query>
        <xsd:schema>schema</xsd:schema>xml</query>
      <viewFields>
        <xsd:schema>schema</xsd:schema>xml</viewFields>
      <rowLimit>string</rowLimit>
      <queryOptions>
        <xsd:schema>schema</xsd:schema>xml</queryOptions>
      <webID>string</webID>
    </GetListItems>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <GetListItemsResult>
        <xsd:schema>schema</xsd:schema>xml</GetListItemsResult>
    </GetListItemsResponse>
  </soap12:Body>
</soap12:Envelope>


 Here's some sample code to make a JavaScript call to SharePoint Web Services:

  var soapEnvelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
        "<soap12:Body>" +
        "<GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" +
        "<listName>OPR</listName>";

        soapEnvelope = soapEnvelope.concat(
        "</GetListItems>",
        "</soap12:Body></soap12:Envelope>");

 
        //using jQuery ajax to make the web request
        $.ajax({
            url: self.webUrl + "/_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnvelope,
            contentType: "text/xml; charset=\"utf-8\""  //"application/soap+xml; charset=\"utf-8\""
           , async: false
        }).done(function (data, textStatus, jqXHR) {
      

            var rows = data.getElementsByTagName("z:row");
            if (rows.length == 0) {
                rows = data.getElementsByTagName("row");
            }
            for (var r = 0; r < rows.length; r++) {
                var row = rows[r];
                var ID = "";
                var Title = "";

                for (var a = 0; a < row.attributes.length; a++) {
                    var aname = row.attributes[a].name;
                    var avalue = row.attributes[a].value;
                    if (aname == "ows_ID") {
                        ID = avalue;
                    }
                    else if (aname == "ows_Title") {
                        Title = avalue;
                    }


                } //end for attributes
            


                $("#OutputDiv").html("Title: " + Title+"<br />");

            } //end for rows

Comments

Post a Comment

Popular posts from this blog

How To use ASPNET_SetReg to store encrypted data in the registry and then decrypt the data for use in your app

PowerShell Script to Clean the Windows Installer Directory