In this article, we will cover how to use the PnP JS Core library to get the web url from any page url with in the SharePoint Site Collection. To know more about this JavaScript library, check the below links

In this post,

How to get web url from page url using PnP JavaScript Library.

Syntax:

pnp.sp.site.getWebUrlFromPageUrl(absolutePageUrl) - 
Returns the website URL based on the absolute Page URL passed as parameter

Example:

The below steps and code snippets used to get a web url from the page url in SharePoint Site Collection using PnP JavaScript library,

  1. Download Required files to use PnP-JS-Core library from the below links and upload that to Site Asstes or Style Library
    • Download pnp.js  PnP JS file
    • Download fetch.js Used by PnP js file to handle web requests and responses (Required in IE)
    • Download promise.js Used by PnP js file to handle web requests and responses (Required in IE)
  2. Create new web part page and insert Content Editor web part
  3. Create a sample.html file in Site Assets or Style library and insert the below code snippet

<script type="text/javascript" src="/siteassets/scripts/fetch.js"></script> 
<script type="text/javascript" src="/siteassets/scripts/promise.min.js"></script> 
<script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>
 
Web URL: <span id="sample"></span>
 
<script type="text/javascript">
//The below PnP method from SiteCollection object returns the web url from page url
$pnp.sp.site.getWebUrlFromPageUrl('https://ktskumar.sharepoint.com/sites/team/SitePages/Home.aspx')
 .then(function(res) {
 document.getElementById('sample').innerHTML = res;
});
</script>

Typescript Example:

Typescript is the superset of JavaScript and this PnP JS library is developed using the typescript specification. For developing the SharePoint Framework web parts and make the client side development easier, we can vote for typescript. The below is the typescript example for retrieving the web url from any page url in  Site Collection.


import pnp from "sp-pnp-js";

pnp.sp.site.getWebUrlFromPageUrl('https://ktskumar.sharepoint.com/sites/team/SitePages/Home.aspx')
 .then(res => {
 document.getElementById('sample').innerHTML = res;
});

Output:

Web URL: https://ktskumar.sharepoint.com/sites/team
Get Web URL from a Page URL in SharePoint Site Collection
Tagged on:                 

Leave a Reply

Your email address will not be published. Required fields are marked *

does viagra work if women take (b) photomicrograph (original magnification, Atildemdash100 hematoxylin-eosin stain) shows a uniform cellular neoplasm composed of whorls of smooth muscle cells with little intervening collagen. Secondly, hysterectomy was offered as the panacea for all these issues as doctors believed that women were better off without their uteri because they could then have sex every day of the month! ( i think they thought they were doing the husbands a favour). http://beta-blinds.co.uk/sbq-278274/ Secondly, hysterectomy was offered as the panacea for all these issues as doctors believed that women were better off without their uteri because they could then have sex every day of the month! ( i think they thought they were doing the husbands a favour). Com.. http://dragonflycreations.ca/ogr-275961/ 7 out of 5 stars (12) 4. threadfed.com/vdd-275000/ 02. generic viagra online buy tratermin.com/exh-278463/ Instead, i explored other options, seeking out resources to help me unearth a more natural route. The researchers found that both studies identified a candidate variant in 17q25. luzumar.es/gqp-277285/ user reviews viagra oral Doctors think that these cancers do not arise from an already-existing fibroid. docupress.es/twp-277105/ Fibroids that are in the muscle portion of the uterus or toward the outside can be removed by making a cut in the uterus over the fibroid then removing the fibroid and sewing the uterus closed. http://traher.es/urv-277813/ Learn more women children's health the center for women children's health is a hub for supporting women's and children's individual healthcare needs. Healthmegamall.