Ulthran
Ulthran

Reputation: 308

Can JavaScript in a Site Access SharePoint Lists?

If I have a site, completely independent of Microsoft (a Wordpress site), could I access data from lists on a SharePoint site using JS on the external site? For example, I can access lists with the following code in the SharePoint site:

function retrieveChemicalsListItems() {
var clientContext = new SP.ClientContext('/sites/________/');
var oList = clientContext.get_web().get_lists().getByTitle('ChemicalsTable');

var camlQuery = new SP.CamlQuery();
//<Where><Geq><FieldRef Name=\'ID\'/>' + '<Value Type=\'Number\'>1</Value></Geq></Where> <RowLimit>10</RowLimit>
camlQuery.set_viewXml('<View><Query></Query></View>');
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onChemicalsQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

}

If I just took this, with the same URL and everything, and put it on any other site, would it work just the same?

Upvotes: 0

Views: 945

Answers (1)

Semushin Dmitrii
Semushin Dmitrii

Reputation: 176

Do you use Sharepoint online or on premise? If you use online, go to link with tutorial for SP Online. But it's not good idea to pass login and password through js code, because everybody will see it. It's a security threat.

Or see this for onprem: link 1, link2. Also, using CSOM can help with developing application:

using (ClientContext context = new ClientContext("sp_site_url")) {
    context.Credentials = new NetworkCredential("user", "password", "domain");
    List list = context.Web.Lists.GetByTitle("MyList");
    context.Load(list);
    context.ExecuteQuery();
    // process list
}

Another way is develop your own web service which will have access to Sharepoint site and you will be able to call it with using your custom authentication.

Upvotes: 1

Related Questions