Page Metadata using share point Rest API

Page Metadata using share point Rest API

ยท

1 min read

Hope you will enjoy this blog!๐Ÿ˜€

When you create a new share point page(SP Page),we can give details about page. Where these details resides?

  • Go to SP Page โ‡’ Page Details opens a panel right side

    image.png

page details panel

image.png

we are going to retrieve page properties using SharePoint rest API. Here page properties comes from column information of site pages library in SharePoint. For example PageType, category etc.,.

image.png

Site Pages Library

image.png

  • Create a spfx webpart.Choose framework react js or NO framework. I have chosen no framework.

  • Now go to .ts file and create a function and call rest API


private _callrestAPI=()=>{
this._getPageDetails.then((pageDetails)=>{
            if(pageDetails.value[0].PageType ==="Blog" || pageDetails.value[0].PageType ==="News"||pageDetails.value[0].PageType ==="info"){
              console.log(pageDetails.value[0].PageType)
            }
        });
private _getPageDetails=()=>{
        this.properties.currentPageId= this.context.pageContext.listItem ? this.context.pageContext.listItem.id : 0; 
/*this.context.pageContext.listItem.id => to retrieve current page ID,meaning get the page ID where webpart is hosting*/
        return this.context.spHttpClient.get(
            this.context.pageContext.web.absoluteUrl+
            "/_api/web/lists/GetByTitle('Site Pages')/Items?$select=ID,PageType,category&$filter=ID eq "+this.properties.currentPageId ,
            SPHttpClient.configurations.v1)
            .then((response:SPHttpClientResponse)=>{ 
                return response.json();});
    }

In this way we can fetch page details.

Thank you๐Ÿ˜€

ย