Use pnp js to query other sharepoint sites using Plain Javascript

%3CLINGO-SUB%20id%3D%22lingo-sub-116754%22%20slang%3D%22en-US%22%3EUse%20pnp%20js%20to%20query%20other%20sharepoint%20sites%20using%20Plain%20Javascript%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-116754%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20query%20a%20document%20library%20from%20a%20other%20sharepoint%20site%20(Not%20a%20Subsite)%26nbsp%3Bin%20the%20same%20tenant%2C%20but%20not%20able%20to%20find%20a%20way%3C%2FP%3E%3CP%3EI%20can%20query%20as%20follows%20to%20retrieve%20from%20the%20current%20sharepont%20site%20context.%20Is%20it%20possible%20to%20query%20from%20other%20sharepoint%20sites%20using%20pnp%20js%3F%3C%2FP%3E%3CPRE%3E%3CSPAN%3E%3CFONT%20face%3D%22Consolas%22%3Evar%3C%2FFONT%3E%3C%2FSPAN%3E%26nbsp%3Bdeferred%26nbsp%3B%3D%26nbsp%3B%24.Deferred()%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%24pnp.sp.web.lists.getByTitle(%3CSPAN%3E%3CFONT%20face%3D%22Consolas%22%3E'Doc%26nbsp%3BList'%3C%2FFONT%3E%3C%2FSPAN%3E).items.filter(%3CSPAN%3E%3CFONT%20face%3D%22Consolas%22%3E%22AuthorId%26nbsp%3Beq%26nbsp%3B'%22%3C%2FFONT%3E%3C%2FSPAN%3E%26nbsp%3B%2B%26nbsp%3B_spPageContextInfo.userId%26nbsp%3B%2B%26nbsp%3B%3CSPAN%3E%3CFONT%20face%3D%22Consolas%22%3E%22'%22%3C%2FFONT%3E%3C%2FSPAN%3E).get().then(%3CSPAN%3E%3CFONT%20face%3D%22Consolas%22%3Efunction%3C%2FFONT%3E%3C%2FSPAN%3E%26nbsp%3B(results)%26nbsp%3B%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bdeferred.resolve(results)%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-116754%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-116867%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20pnp%20js%20to%20query%20other%20sharepoint%20sites%20using%20Plain%20Javascript%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-116867%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20the%20site%20(SPWeb%20on%20the%20other%20site%20collection)%20is%20with%20the%20same%20hostname%20it%20can%20be%20requested.%3C%2FP%3E%3CP%3EA%20web%20object%20should%20be%20created%20using%20%60let%20web%20%3D%20new%26nbsp%3Bpnp.Web('https%3A%2F%2Fhost%2Fsites%2Fsite%2Fweb')%60.%20Web%20absolute%20path%20should%20be%20passed%20as%20a%20parameter%20when%20creating%20a%20new%20web%20object%20instance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24pnp%20object%20is%20a%20global%20shim.%20In%20the%20examples%2C%20it's%20more%20usual%20to%20meet%20%60pnp%60.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%2C%20in%20the%20code%20sample%20above%2C%20the%20logic%20will%20fail%20because%26nbsp%3BUserId%20for%20the%20same%20user%20is%20different%20on%20the%20different%20site%20collections%2C%20UserId%20can%20be%20requested%20with%20%60web.currentUser%60.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20yet%20another%20piece%20of%20advice%2C%20as%20PnP%20JS%20Core%20already%20deal%20with%20promises%20you%20do%20not%20need%20to%20wrap%20it%20with%20jQuery%20deferred%26nbsp%3Band%20can%20return%20native%20ES6%20promises.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20check%20the%20code%20sample%20in%20TypeScript%3A%3C%2FP%3E%3CPRE%3Eimport%20%7B%20Web%20%7D%20from%20'pnp'%3B%0A%0Alet%20getUserItemsFromAnotherWeb%20%3D%20(webAbsoluteUrl%3A%20string%2C%20listName%3A%20string)%3A%20Promise%26lt%3Bany%26gt%3B%20%3D%26gt%3B%20%7B%0A%20%20let%20web%20%3D%20new%20Web(webAbsoluteUrl)%3B%0A%20%20return%20web.currentUser.get()%0A%20%20%20%20.then(u%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20return%20u.Id%3B%0A%20%20%20%20%7D)%0A%20%20%20%20.then(userId%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20return%20web.lists%0A%20%20%20%20%20%20%20%20.getByTitle(listName).items%0A%20%20%20%20%20%20%20%20.filter(%60AuthorId%20eq%20'%24%7BuserId%7D'%60).get()%3B%0A%20%20%20%20%7D)%3B%0A%7D%3B%3C%2FPRE%3E%3CP%3Eand%20in%20ES5%2C%20if%20you%20really%20need%20a%20vanilla%20JS%20for%20any%20reasons%3A%3C%2FP%3E%3CPRE%3Evar%20getUserItemsFromAnotherWeb%20%3D%20function(webAbsoluteUrl%2C%20listName)%20%7B%0A%20%20var%20web%20%3D%20new%20%24pnp.Web(webAbsoluteUrl)%3B%0A%20%20return%20web.currentUser.get()%0A%20%20%20%20.then(function(u)%20%7B%0A%20%20%20%20%20%20return%20u.Id%3B%0A%20%20%20%20%7D)%0A%20%20%20%20.then(function(userId)%20%7B%0A%20%20%20%20%20%20return%20web.lists%0A%20%20%20%20%20%20%20%20.getByTitle(listName).items%0A%20%20%20%20%20%20%20%20.filter('AuthorId%20eq%20%22'%20%2B%20userId%20%2B%20'%22').get()%3B%0A%20%20%20%20%7D)%3B%0A%7D%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOh%2C%20and%20final%20thing%20getting%20lists%20by%20title%20(%60web.lists.getByTitle%60)%20can%20cause%20none%20valid%20code%20if%20a%20list%20can%20be%20renamed.%20I%20would%20recommend%20getting%20lists%20with%20%60web.getList('_list_relative_uri_')%60.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20help.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-116808%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20pnp%20js%20to%20query%20other%20sharepoint%20sites%20using%20Plain%20Javascript%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-116808%22%20slang%3D%22en-US%22%3EYou%20should%20be%20able%20to%20specify%20which%20site%20you%20want%20to%20connect%20to%20-%20this%20is%20what%20I%20found%3A%3CBR%20%2F%3Evar%20w%20%3D%20new%20%24pnp.Web(%22%7BSite%20Url%7D%22)%3B%3CBR%20%2F%3Ein%20the%20Basic%20Operations%20page%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-JS-Core%2Fwiki%2FBasic--Operations%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-JS-Core%2Fwiki%2FBasic--Operations%3C%2FA%3E%3CBR%20%2F%3EHope%20this%20helps%20you%20on%20the%20way%20%3A)%3C%2Fimg%3E%20John%3C%2FLINGO-BODY%3E
Highlighted
Contributor

I am trying to query a document library from a other sharepoint site (Not a Subsite) in the same tenant, but not able to find a way

I can query as follows to retrieve from the current sharepont site context. Is it possible to query from other sharepoint sites using pnp js?

var deferred = $.Deferred();
   $pnp.sp.web.lists.getByTitle('Doc List').items.filter("AuthorId eq '" + _spPageContextInfo.userId + "'").get().then(function (results) {
       deferred.resolve(results);
   });

 

2 Replies
Highlighted
You should be able to specify which site you want to connect to - this is what I found:
var w = new $pnp.Web("{Site Url}");
in the Basic Operations page:
https://github.com/SharePoint/PnP-JS-Core/wiki/Basic--Operations
Hope this helps you on the way :) John
Highlighted

If the site (SPWeb on the other site collection) is with the same hostname it can be requested.

A web object should be created using `let web = new pnp.Web('https://host/sites/site/web')`. Web absolute path should be passed as a parameter when creating a new web object instance.

 

$pnp object is a global shim. In the examples, it's more usual to meet `pnp`.

 

Also, in the code sample above, the logic will fail because UserId for the same user is different on the different site collections, UserId can be requested with `web.currentUser`.

 

And yet another piece of advice, as PnP JS Core already deal with promises you do not need to wrap it with jQuery deferred and can return native ES6 promises.

 

Please check the code sample in TypeScript:

import { Web } from 'pnp';

let getUserItemsFromAnotherWeb = (webAbsoluteUrl: string, listName: string): Promise<any> => {
  let web = new Web(webAbsoluteUrl);
  return web.currentUser.get()
    .then(u => {
      return u.Id;
    })
    .then(userId => {
      return web.lists
        .getByTitle(listName).items
        .filter(`AuthorId eq '${userId}'`).get();
    });
};

and in ES5, if you really need a vanilla JS for any reasons:

var getUserItemsFromAnotherWeb = function(webAbsoluteUrl, listName) {
  var web = new $pnp.Web(webAbsoluteUrl);
  return web.currentUser.get()
    .then(function(u) {
      return u.Id;
    })
    .then(function(userId) {
      return web.lists
        .getByTitle(listName).items
        .filter('AuthorId eq "' + userId + '"').get();
    });
};

 

Oh, and final thing getting lists by title (`web.lists.getByTitle`) can cause none valid code if a list can be renamed. I would recommend getting lists with `web.getList('_list_relative_uri_')`.

 

Hope this help.