Domanda

I'm working on a Node.js app and I'd like to use some basic SharePoint services inside the app.
I've already done this using java and SOAP web services (copy, list, search) And now I'd like to do the same thing in node.js and RESTful web services.
Is there a good API wrapper you could recommend or some sample code for using SharePoint-REST-API ?
(I'm new to RESTful services)

I'm also working on SharePoint 2013 and SharePoint Online and would like to use oAuth2 for both of them, is that possible ?

È stato utile?

Soluzione

It depends heavily on your requirements and on the app type you are going to build (web application or just console daemon, azure function, etc.). Does it require user context or not?

Here are the options available:

  1. Create Azure AD application, give required permissions to SharePoint and use node-adal library for OAuth (authorization code flow, client creds flow). Works only with Azure AD apps, you may have user context (authorization code flow) or application only permissions with client creds flow. Works for SharePoint online only.
  2. Create SharePoint addin. Now you have options depending on your requirements.
    a. For SharePoint online you can try to use passport-sharepoint express middleware for authenticating inside SharePoint. You will get user context. It works only with low-trust addins. b. If user context is not necessary and it's ok to use addin only permissions everywhere in your app, you can use sp-request. It allows you to create access token with client creds flow for both Online and on-premise

For mapping consider PnP-JS-Core, but it doesn't work well with nodejs, because the primary purpose of this library is browser. Hopefully I've created a sample on how you can setup it up for nodejs here - node-pnpjs-sample

UPD
You know, there is no "silver bullet" (especially when we are talking about SharePoint and nodejs). For SharePoint Online I would recommend Azure AD app and node-adal. But you need to think carefully if Azure AD app is acceptable for your requirements. A good starting point in descision will be following article deciding between the SharePoint app model and the Office 365 APIs. If you selected Azure AD for SharePoint online, go ahead with node-adal. If you selected SharePoint addin instead of Azure AD app, then you can use either sp-request or passport-sharepoint (again depending if user context is required or not).

For on-premise (high-trust) I don't know about module similar to passport-sharepoint, so in case if you need user context here, you need to search or implement on your own. If user context isn't required, you can go ahead with sp-request for on-premise.
Since you are not able to decide right now, may be that's worthwhile to spend some trying different options in order to make decision.

Try to answer on the following questions:

  • Is that a nodejs web application or console or desktop or daemon service?
  • Is authentication for users required?
  • Do you need in your app to use user context (run search queries with user identity, separate access to the different parts of the app (or SharePoint) based on the user permissions, list security trimming, etc)?

UDP2
So the problem: nodejs backend service, which requires user context, need to talk with SharePoint as connector between mobile app and SharePoint.

In that case, as your service needs user context and you are not able to interact with user directly (because of service), I think the mobile app need to send you authentication data about the user. This depends on the app implementation. That might be an OAuth access token, a cookie, or any other authentication header the mobile app might use. Of course all communication should be performed via secure https channel.
Your service will attach that authentication data to http request to SharePoint.

Altri suggerimenti

I've seen a few REST wrappers for Node.js, some from this Stack Exchange post in March. Is there anything in particular you'd like to accomplish?

node-sharepoint-rest - How to use

csom-node

sp-request

In addition to the other answers here, something you might be interested in, as you have asked for an API wrapper for the SharePoint REST services, is:

the BRAVO Core Library

which is exactly that. They have wrapped the SP REST service calls in an API that has a syntax much more similar to server side C# code, or Powershell.

[As an aside: that's one of the ways I use it. If I do not have remote access to a SharePoint server, and need to do something I would normally turn to Powershell for (usually just exploratory stuff), I will create a blank Wiki page on the site, upload the BRAVO Core Library somewhere, link to the script on the page, open the page in a browser (loading the script), and open the browser dev tools. I can then do "Powershell via REST" in the browser console window. Super handy.]


Edit: Just saw that he reworked the whole thing in TypeScript. Haven't played around with that yet, but looks interesting.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top