Code Monkey home page Code Monkey logo

Comments (11)

koltyakov avatar koltyakov commented on May 23, 2024

from sp-rest-proxy.

Draghonite avatar Draghonite commented on May 23, 2024

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.

koltyakov avatar koltyakov commented on May 23, 2024

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.

nsksaisaravana avatar nsksaisaravana commented on May 23, 2024

@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.

Gopinath83 avatar Gopinath83 commented on May 23, 2024

@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.

koltyakov avatar koltyakov commented on May 23, 2024

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.

mgordic avatar mgordic commented on May 23, 2024

Hi @koltyakov,

I followed your instruction how to setup Angular project to use sp-rest-proxy.

However, I have some issues there:

  1. loadPageContext() is executed, and _spPageContextInfo is filled in. There I see for example that webAbsoluteUrl is http://localhost:4200/sites/test

  2. 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.

koltyakov avatar koltyakov commented on May 23, 2024

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.

mgordic avatar mgordic commented on May 23, 2024

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.

koltyakov avatar koltyakov commented on May 23, 2024

One moment:

image

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:

image

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.

Gopinath83 avatar Gopinath83 commented on May 23, 2024

from sp-rest-proxy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.