SharePoint Online REST APIs (Part III): Pages
In the SharePoint Online REST APIs series, I’ll be sharing the most common APIs I use. I mainly use these APIs in Power Automate, so I’ll base the information in this series on the data you need for a Send an HTTP request to SharePoint action.
So far I have covered libraries and lists. In this article we’ll explore how to interact with SharePoint modern pages. This is not a comprehensive list; rather a list of calls that I use when I can’t use predefined Power Automate actions. I have used the color red to identify interchangeable values.
Publish a page
_api/web/GetFileByServerRelativeUrl('/sites/Site Name/Site Pages/Page Name.aspx')/Publish()
This call will convert a draft page (minor version) into a published page (major version).
Promote page to news
After you’ve published your page, you may want to convert if into a News Page. This call is slightly different to the previous one. Instead of finding the page via the path, it uses the item ID. In this example, the ID is 43.
List all Organizational News sites
Perhaps you want to go through all the Organizational News sites in your tenant. This call will return all the Site and Web IDs of these sites.
Check out a page
This call will check out a specific page. It uses the item ID. In this example, the ID is 35.
Get page content
_api/web/lists/GetByTitle('Site Pages')/items?$select=Title,CanvasContent1,FileLeafRef&$filter=FileLeafRef eq 'Page-Filename.ext'
I have covered how to get the page content in a previous article. For more information, please read this article.
Get the properties of an image on a page
_api/web/GetFileByServerRelativePath(decodedurl='/sites/Site Name/SiteAssets/SitePages/Page Name/ImageName.png')/Properties
This call will return the properties of an image that has been inserted on a page. Properties include the image dimensions, modified date etc.
Create a new page from a template
_api/web/GetFileByServerRelativePath('/sites/Site Name/SitePages/Templates/Page-Filename.aspx')/copyTo('/sites/Site Name/SitePages/New-Page-Filename.aspx')
This call will create a new page from a page template. Templates are saved in the Templates folder in the Site Pages library.
Save page as a draft
"Page content code here..."
Finally, we can save a page as a draft. I have inserted placeholder content in the body for the page content. In this section, you will insert code that makes up the page (see the CanvasContent1 column for an example). You can also copy an existing page by using the Development Tools in your browser. I will cover how to do this in a future article.
14 Replies to “SharePoint Online REST APIs (Part III): Pages”
This was useful. Thank you!
You’re very welcome!
You made my day. Thanks a lot.
Was searching for publishing via api to use in Power Automate.
I’m so happy to hear! Thank you for commenting!
Firs of all thanks for the synthesized iformation. I’m trying to create a page with Power Automate using the last two endpoints in your list. First one is to create the page and the second to populate the page with the data. The flow goes without errors but the end result is a copy of the template page with chaged name. The content is unchanged. More info about my problem can be found here https://powerusers.microsoft.com/t5/Building-Flows/Send-http-request-to-SharePoint/td-p/1323311. Any suggestions are welcome. Thanks in advance.
Hi, thanks for reading! API calls can be tricky to troubleshoot (especially when it’s saying everything is fine). I’ve had issues in the past where a page wouldn’t change if my HTML was not right (SharePoint can be picky about the littlest change). I have a post that I need to write about this process, however, it’s quite complicated and frankly I just keep putting it off. I noticed that you are not referencing a template file in your code, but appears to be another SharePoint page. Also are you going to the Developer Tools in your browser and getting the code from there? This post outlines the process and might be helpful if you haven’t seen it before (step 3 outlines the Dev Tools step): https://www.c-sharpcorner.com/blogs/automate-modern-sharepoint-page-creation-using-power-automate
I have read this article before and it is quite similar to the youtube video I posted in the other topic. Today I tried to run the request manually with the help of little bit of JS. In the console I got the error that the security validation of the page was invalid. The recommended approach in such cases is to add the X-requestdigest property in the header. However I have no idea how to do that in power automate.
So I was able to get the flow working with static hard coded content, but not with dynamic. It turns out that I was using wrong ID and that was the reason why nothing was changed. Now the problem is how to get the content from the word file and put it on the page. The PageContent option from GetFileContent is taking too much time and eventually the flow fails because the session is ended. Any Ideas why this happens?
As far as I’m aware, Power Automate returns the data from Word as binary. Depending on how big or complicated your document is, this might cause the action to time out. To read a document, you would most likely need to use the AI tools in Azure.
EDIT: I found this post which explains how you can get the text. It’s not straight forward, but looks like you might be able to extract the text by XML elements, then manipulate it from there: https://www.tachytelic.net/2021/05/power-automate-extract-text-from-word-docx-file/
Thank you so much for this valuable information. Right now I’am being able to extract the information and all the flows pass except the last one where I am putting the output of the compose into the request body and want to save the page as a draft. Is it possible because the output is array and this messes up the last step? The word file I’m using for testing has only one line of text, so there is not so much info.
If anyone is interested the output of the compose expression should be represented with join(outputs(‘Compose’),’;’) so that it can be put in the body of the request.
hi, i am unable to promote my page to news using the api call. it says the expression status code 400, even though its correctly getting the page metadata. Why would this be the case? This would be the last step in my flow and i cant get it to work!
Thank you for your valuable posts, they helped me a lot.
I was using Inspect element -> Request Payload to change something in my SharePoint page then I used the save page as draft and publish API. But I need to get the Request Payload with an API too in order to change the CanvasContent, I tried with ‘Get page content’ API referenced in one of your articles, but I get the HTML and I can convert only to text not as the JSON format I get with the request payload. I need this because some of the canvas content can be changed manually with edit click, and some parts will be changed within the flow, so I need to get the present content and not to use the static request payload I copied in my flow.
Any idea how to manage this, thank you in advance!!
Hi is there a option to get the Views of single pages listed all in same URL ?