Comments (11)
- Angular 4, SharePoint On-Premises, localhost development and SP-REST-Proxy
- Accessing SharePoint Data Locally
from sp-rest-proxy.
Thank you for the library. Sadly, despite following these great examples, I still could not get integration with a standalone SP instance. Using jQuery for the moment but keeping an eye on this library.
from sp-rest-proxy.
Hi @Draghonite, thank you for your message and the interest.
You can definitely use sp-rest-proxy with jQuery.ajax or even raw XMLHttpRequest or Fetch.
In your local dev server, requests should be targeted to proxy's endpoint. And in the code, based on if it's a local environment or not http client wrapper should decide where to target requests in a runtime.
from sp-rest-proxy.
@koltyakov Thanks a lot for your hardword, there should be simple way to program in Angular 5+ , PnpJS inside sharepoint classic page or SPFX. I heard Microsoft is going to come up with a page where we can develop Angular SPA !!!!!
from sp-rest-proxy.
@koltyakov Thanks a lot for finding out SP proxy to handle the SharePoint REST APIs.
I have tried the http://johnliu.net/blog/2017/9/angular-4-sharepoint-on-premises-localhost-development-and-sp-rest-proxy article but i could not able to make it.
I'm using Angular 7 with SharePoint 2013. We are not using SPFx.
Could you please let us know if you have step by step guide to configure the SP-REST-Proxy in Angular 7 environment.
Thanks
from sp-rest-proxy.
Hi @Gopinath83,
Thanks for using the library!
Not being Angular user it's difficult to provide you an optimal way. I have no clue in many Ng configs and decisions they made in their generator. Followed the recent docs and created this as a walkthrough mini guide:
npm install -g @angular/cli
ng new ng-sp-proxy
cd ng-sp-proxy
npm i -D sp-rest-proxy
touch proxy.conf.js
/* proxy.conf.js */
const port = 9090; // better using env var
const PROXY_CONFIG = {
'/_api/*': {
target: `http://localhost:${port}`,
secure: true
}
};
module.exports = PROXY_CONFIG;
Add "proxyConfig": "proxy.conf.js" to angular.json (details in Angular CLI official docs).
touch proxy.api.js
/* proxy.api.js */
const RestProxy = require('sp-rest-proxy');
const port = 9090; // better using env var
const configPath = './config/private.json';
const settings = { configPath, port };
const restProxy = new RestProxy(settings);
restProxy.serve();
Add npm task or run node ./proxy.api.js
(enter creds on the first run).
Run npm run start
and node ./proxy.api.js
simultaneously.
If you know how to reconfigure web pack dev server options in Ng CLI (I have no idea) when this approach is preferred (no parallel tasks are required, tighter integration with dev server).
Use loadPageContext as a context helper to use _spPageContextInfo in a local serve mode.
P.S.
If you'll end up with a guide which works for you it would be cool if you can contribute with the corresponding angular config doc. In case you're blogging, would love to add a link to a blog post with Angular 7 + Proxy configuration instruction. Thanks!
from sp-rest-proxy.
Hi @koltyakov,
I followed your instruction how to setup Angular project to use sp-rest-proxy.
However, I have some issues there:
-
loadPageContext() is executed, and _spPageContextInfo is filled in. There I see for example that webAbsoluteUrl is http://localhost:4200/sites/test
-
When I next try within loadPageContext to query sharePoint data, I am getting Http 404 error, since it's making request to localhost:4200/sites/test. Proxy itself is working, when I make a request trough it, I am getting results back from SP.
Do you have any idea what could be wrong here?
It's funny that it's working like a charm in react, but seems so Angular's proxy is making some problems here.
from sp-rest-proxy.
Hi Marko,
I and Angular are on the different side of the road. ;)
The only can suggest in this situation is to start proxy and Angular dev server in parallel and configure code with "if localhost then send API calls to proxy otherwise to current hostname".
from sp-rest-proxy.
Hi Andrew,
Thank you! That's exactly what I did in meantime.
`ngOnInit()
{
let self = this;
loadPageContext().then(async s =>
{
sp.setup({
sp: {
headers: {
Accept: "application/json;odata=verbose"
},
baseUrl: self.isLocalDevMode() ? 'http://localhost:9090' : _spPageContextInfo.webAbsoluteUrl
}
});
sp.web.select("Title").get().then(w =>
{
console.log(w.Title);
self.title = w.Title;
}).catch((err) => { console.log(err); });
}).catch((err) =>
{
console.error(err);
});
}`
public isLocalDevMode() { return window.location.href.indexOf('localhost') >= 0; }
What I don't understand here is why do I need loadPageContext, when it returns anyway wrong url (it's returning url that is localhost + server relative url of target SharePoint site.
My understanding is that when I call this url, in background it will intercept that call and pass it trough proxy to get data from SharePoint. If I leave it without setting up base url with proxy address, then every call to pnpjs will fail.
Have you see maybe this before?
Thank you
Marko
from sp-rest-proxy.
One moment:
It's better placing absolute URL which corresponds to a real site abs URL, e.g. if your site is https://c.sharepoint.com/sites/something
, proxies URL should be http://localhost:port/sites/something
, but not just http://localhost:port
. Even while proxy will understand and add the relative part, I recommend mirror the URLs this way.
What I don't understand here is why do I need loadPageContext, when it returns anyway wrong url (it's returning url that is localhost + server relative url of target SharePoint site.
It's made for a scheme where Webpack Dev server's proxy bypasses API calls to sp-rest-proxy. This schema ends up with the same localhost origin for SPA static assets and API.
When configured properly, loadPageContext
not only gives an absolute URL, but a part of the _spPageContextInfo
such as user info, language to name just a few.
The method works well but this the assumption that first API hostname is the same as the one dev assets hosted on:
And I sure that an Angular project (scaffolded with CLI) can be theoretically configured identically as those majority of React apps there we just extend DevServer with before handler or any framework with a custom Webpack config and Dev Server as well, but taking a look into what Ng CLI scaffolds I didn't get the idea how.
from sp-rest-proxy.
from sp-rest-proxy.
Related Issues (20)
- Identical requests - succeeding and failing HOT 4
- Can this react app be deployed as a web app on to a windows server so that users can access the app outside of the server ? HOT 6
- Host "sp-rest-proxy" as an independent web-app on Azure or pass authentication in configs to be used in CI/CD pipeline HOT 1
- Can this proxy be used when the host SP environment (on prem) has a "strict-origin-when-cross-origin" HTTP referrer policy? HOT 3
- sharepoint MFA bypass HOT 2
- [Feature query] Can the proxy be made to connect to multiple sites? HOT 1
- sp-rest-proxy with sharepointplus HOT 10
- POST request fails when calling the proxy (adding extra path to sharepoint request) HOT 5
- Proxy Error: Cannot read properties of null (reading 'toString') (see more in sp-rest-proxy console) HOT 4
- Version 3.3.4 broken for OnpremiseUserCredentials HOT 5
- Cannot read properties of null (reading 'toString') HOT 3
- On Demand - cannot read properties of null / childprocess.spawnsync EONENT Error HOT 2
- Proxy approach for Azure Active Directory HOT 2
- Typescript import HOT 1
- Setting siteUrl programatically HOT 4
- sp-rest-proxy does not work with Node 18.16.0 (current LTS) HOT 5
- Thank you HOT 1
- Docker HOT 2
- JSOM using sp-rest-proxy / clarification needed HOT 9
- 401 When Running in Docker HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sp-rest-proxy.