Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add end-to-end example of usage with sp-pnp-js #26

Closed
koltyakov opened this issue Sep 2, 2017 · 2 comments
Closed

Add end-to-end example of usage with sp-pnp-js #26

koltyakov opened this issue Sep 2, 2017 · 2 comments
Assignees

Comments

@koltyakov
Copy link
Owner

No description provided.

@koltyakov koltyakov self-assigned this Sep 2, 2017
@koltyakov koltyakov changed the title Add end-to-end exaple of usage with sp-pnp-js Add end-to-end example of usage with sp-pnp-js Oct 29, 2017
@koltyakov
Copy link
Owner Author

koltyakov commented Nov 26, 2017

(() => {

  /**
   * This part is only for testing simplification from a random page.
   * Loads pnp global shim from CDN.
   * ES6 imports works as well. $pnp stands for global shim object.
   */
  if (typeof $pnp === 'undefined') {
    let s = document.createElement('script');
    s.type = 'text/javascript';
    s.src = 'https://cdnjs.cloudflare.com/ajax/libs/sp-pnp-js/3.0.1/pnp.min.js';
    document.head.appendChild(s);
  }

  const waitFor = (cond, cb) => {
    if (!cond()) {
      setTimeout(() => waitFor(obj, cb), 100);
    } else {
      if (cb && typeof cb === 'function') {
        cb();
      }
    }
  };

  waitFor(() => typeof $pnp !== 'undefined', () => {

    // Usage in proxy mode

    const proxyUrl = 'http://localhost:8080'; // sp-rest-proxy's url for a dev project
    // _spPageContextInfo should be mimiced on locally served page
    if (typeof _spPageContextInfo === 'undefined') {
      _spPageContextInfo = {};
      _spPageContextInfo.webServerRelativeUrl = '/sites/site/web'; // Provide a web's relative URL
      _spPageContextInfo.webAbsoluteUrl = `${proxyUrl}${_spPageContextInfo.webServerRelativeUrl}`;
    }

    // PnP JS Core setup
    $pnp.setup({
      sp: {
        headers: {
          Accept: 'application/json;odata=verbose;'
        }
      }
      // `baseUrl` with `_spPageContextInfo.webAbsoluteUrl` can be used
      // or strict Web object creation as shown below
    });

    // Create new Web object with `webAbsoluteUrl` to proxy
    let web = new $pnp.Web(_spPageContextInfo.webAbsoluteUrl);

    // Using POST method to create an item as example
    web.getList(`${_spPageContextInfo.webServerRelativeUrl}/Lists/CustomList`)
      .items.add({
        Title: 'New Item'
      }).then(console.log);

  });

})();

Request from non-SharePoint page

image

image

image

image

Request in Proxy

image

@koltyakov
Copy link
Owner Author

Blog post article with setting up SPFx and Proxy with PnP JS Core section as a bonus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant