Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Working With: Webs

Patrick Rodgers edited this page Jan 31, 2017 · 7 revisions

Webs are one of the fundamental entry points when working with SharePoint. Webs serve as a container for lists, features, sub-webs, and all of the entity types.

Get A Web's properties

import pnp from "sp-pnp-js";

// basic get of the webs properties
pnp.sp.web.get().then(w => {

    console.log(w.Title);
});

// use odata operators to get specific fields
pnp.sp.web.select("Title").get().then(w => {

    console.log(w.Title);
});

// use with a simple getAs to give the result a type
pnp.sp.web.select("Title").getAs<{ Title: string }>().then(w => {

    console.log(w.Title);
});

Get Complex Properties

Some properties, such as AllProperties, are not returned by default. You can still access them using the expand operator.

import pnp from "sp-pnp-js";

pnp.sp.web.select("AllProperties").expand("AllProperties").get().then(w => {

    console.log(w.AllProperties);
});

Get a Web Directly

You can also use the Web object directly to get any web, though of course the current user must have the necessary permissions. This is done by importing the web object.

import { Web } from "sp-pnp-js";

let web = new Web("https://my-tenant.sharepoint.com/sites/mysite");

web.get().then(w => {

    console.log(w);
});

Update Web Properties

You can update web properties using the update method. The properties available for update are listed in this table. Updating is a simple as passing a plain object with the properties you want to update.

import { Web } from "sp-pnp-js";

let web = new Web("https://my-tenant.sharepoint.com/sites/mysite");

web.update({
    Title: "New Title",
    CustomMasterUrl: "{path to masterpage}",
    Description: "My new description",
}).then(w => {

    console.log(w);
});

Delete a Web

import { Web } from "sp-pnp-js";

let web = new Web("https://my-tenant.sharepoint.com/sites/mysite");

web.delete().then(w => {

    console.log(w);
});
Clone this wiki locally