Dec 17 2019 10:51 PM
Need to access the list items where SPFX webpart is deployed in one SharePoint 2016 application (https://abc.sharepoint.com) and list is present in another SharePoint application (https://mysite.sharepoint.com) in same SharePoint 2016 server farm with same credentials.
SharePoint version: 2016 on-premise
SPFX: 1.1
Tried: used "@pnp/sp-addinhelpers" and @pnp/sp.
Code:
import { sp, SPRequestExecutorClient } from "@pnp/sp-addinhelpers";
function getitemfromotherdomain(){
sp.setup({
sp: {
fetchClientFactory: () => {
return new SPRequestExecutorClient();
},
},
});
const addInWebUrl= "https://abc.sharepoint.com/";
const hostWebUrl = "https://mysite.sharepoint.com/";
// make requests into the host web via the SP.RequestExecutor
sp.crossDomainWeb(addInWebUrl, hostWebUrl).get().then(w => {
console.log(w);
console.log(JSON.stringify(w, null, 4));
});
}
Source : https://pnp.github.io/pnpjs/sp-addinhelpers/docs/sp-rest-addin/
calling the Function:
const script1 = document.createElement("script");
script1.src="/_layouts/15/SP.RequestExecutor.js";
script1.async = true;
document.body.appendChild(script1);
getitemfromotherdomain();
Error occured: Failed to load resource: the server responded with a status of 403 (Forbidden) on calling of
error expanded:
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>-1, Microsoft.SharePoint.SPException</m:code>
<m:message xml:lang="en-US">There is no app context to execute this request.</m:message>
</m:error>
(index):1 Uncaught (in promise)