8

Is there any way of reading the documents names of a Document Library using JavaScript from a web application that is not hosted in SharePoint?

by the way, my requirement if for SharePoint 2010 not 2013.

I'm trying the REST service but do not see any authentication technique to provide credentials when consuming the web service from JavaScript.

Thanks in advance

2

2 Answers 2

4

Theoretically yes. My experience has been creating web services outside of SharePoint 2010 and reading data into SharePoint pages. Since 2010 runs on ASP.NET 3.5.1, it's easier to add CORS to your SharePoint application than 2013. With ASP.NET 4.0 and above you need to use a library like Thinktecture.IdentityModel (in the GAC) and add methods to the global.asax file of your application. Try adding this section to the <system.webServer> element of your SharePoint web.config file:

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://externaldomain"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept, X-Requested-With, X-File-Name"/>
        <add name="Access-Control-Allow-Methods" value="GET, POST"/>
    </customHeaders>
</httpProtocol>

If using jQuery, your request will resemble the following:

$.ajax.support.cors = true; //enable CORS support, default is true in v1.9+
var listName = "Your List Name".replace(/\s/g, ''); //no white spaces
var filter = encodeURI("?$filter=Title eq 'some title'");
$.ajax({
    url: "http://yoursharepointsite.com/_vti_bin/listdata.svc/" + listName + filter,
    type: "POST",
    cache: false,
    contentType: "text/xml; charset=utf-8",
    username: "username",
    password: "password",
    dataType: "xml"
})
.done(function(xmlDoc, status, responseObj) {
    //handle
})
.fail(function(xmlDoc, status, responseObj) {
    //handle
})
.always(function() {
    //handle
});

You can also retrieve list information with SOAP:

var listName = "yourListName";
var packet = '<?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><GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>' + listName + '</listName></GetList></soap:Body></soap:Envelope>';

$.ajax({
    url: "http://yoursharepointsite.com/_vti_bin/lists.asmx",
    type: "POST",
    dataType: "xml",
    data: packet,
    headers: {
        "SOAPAction": "http://schemas.microsoft.com/sharepoint/soap/GetList",
        "Content-Type": "text/xml; charset=utf-8"
    }
    }).done(function(xmlDoc, status, responseObj) {
        //handle
    })
    .fail(function(xmlDoc, status, responseObj) {
        //handle
    })
    .always(function() {
        //handle
    });
0

Unless you can host the app running outside of SharePoint on the same domain as your SharePoint server (which I don't personally know how to configure :-D) you wouldn't be able to do this without making some server-side solution on your external application that would allow it to make the request to SharePoint (with authentication) then serve the data back to the client -- Otherwise you're just running in to the 'same origin' restriction that prevents cross site scripting attacks in browser based Javascript.

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

SP.RequestExecutor in SP2013 circumvents this problem by using a proxy service, you would need to basically write the equivalent...

http://msdn.microsoft.com/en-us/library/office/fp161183(v=office.15).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.