Skip to main content

Notifications

Power Automate - Building Flows
Unanswered

Change the content of a SharePoint page

Posted on by 58

Hello community, I need to take the context of a SharePoint page and do some changes in it then save those changes. I tried with Get page content API (https://sharepointcass.com/2021/04/01/sharepoint-online-rest-apis-part-iii-pages/) or even get more details with this API call https://powerusers.microsoft.com/t5/Building-Flows/Send-an-http-request-and-return-content-of-a-page/td-p/553217,  but I get the canvas content as HTML, and I need it to be as when I take the context with Inspect element in the Request Payload, so I can do the changes and use send an http request to publish them. I have tried to convert from html but it's only to a plain text.

  • bijaygautam Profile Picture
    bijaygautam 2 on at
    Re: Change the content of a SharePoint page

    Hi there do you got template which can be exported?

  • SilannaIT-Dale Profile Picture
    SilannaIT-Dale 47 on at
    Re: Change the content of a SharePoint page

    Hi @krunal-amin,

     

    glad I could help!

     

    Kind Regards,

    Dale

  • krunal-amin Profile Picture
    krunal-amin 12 on at
    Re: Change the content of a SharePoint page

    You are a legend mate 😊

    This one helped me a lot to improvise my flow. 

     

    Cheers

    Krunal

  • SilannaIT-Dale Profile Picture
    SilannaIT-Dale 47 on at
    Re: Change the content of a SharePoint page

    Hi @SimonaB

     

    I myself wanted to do the same thing and could not find anything on the internet that covered it so I had a go at doing it myself with great success!

     

    I have managed to get this to work by using Microsoft's Graph API.

     

    Please read everything including the context so that you will best understand how to implement this solution for yourself.

     

    I use this flow to update an 'IT tip of the week' on our SharePoint site landing page as security training.

    SilannaITDale_17-1688016715167.png

     

    I have a SharePoint list that has 3 columns, Title, Used and Current. I pick a tip randomly from the list and alter the SharePoint page to display the new 'Tip' for the week.

     

    The Title column has the actual IT tip of the week and the Used/Current columns just have 1/0 values to indicate 'true/false'. In order to prevent double ups, the used column is set to '1' when the tip has been used.

     

    Once all the tips have been used, the Used column for all tips is set back to '0'. The Current column is important as it indicates which tip is currently used on the SharePoint site (I'll explain why this is important later).

    SilannaITDale_0-1688014370455.png

     

    Now, to actually update the site with the IT Tip.

     

    I have created a Scheduled Flow that runs once a week on Monday's.

     

    I have a few IT Tip pre-processing steps but they are specific to my use case, you won't need them.

     

    What you will need is to firstly get the metadata from your site page to obtain it's ID.

    You must select the Page that you want to edit in the File Identifier

    SilannaITDale_1-1688014543508.png

     

    Next you will need a Send an HTTP request to SharePoint tile

    SilannaITDale_2-1688014653592.png

    Fill it out as I have below. This will 'Checkout' your page to make sure that no one else is currently editing or can edit the page while the flow is running. The ItemId comes from the file metadata tile above.

    SilannaITDale_4-1688014735646.png

     

    Checking out the site returns a Body. This contains the .aspx page which we can now alter.

    NOTE: you don't need this step, it is just to demonstrate the output and is good for troubleshooting.

    SilannaITDale_5-1688014857469.png

     

    Next, you will need to find the index of the value 'CanvasContent1' from the Body object returned from the Checkout tile. This is done as we are trying to isolate the page contents from the metadata.

     

     

     

    indexOf(string(body('Checkout_Site_for_editing')),'"CanvasContent1":')

     

     

     

    SilannaITDale_6-1688014983923.png

     

    Next, you will need to find where the end of the page contents is within the Body object returned by the Checkout tile. I did this by looking through the Body object manually to find the next value after CanvasContent1 and searching for that value. That value is '"CoAuthState":'

     

     

     

    indexOf(string(body('Checkout_Site_for_editing')),'"CoAuthState":')

     

     

     

    SilannaITDale_7-1688015151341.png

     

    Next, we need to find the length of the CanvasContent. We can do this by subtracting the start index from the end index values that we just obtained above.

     

     

     

    sub(outputs('Index_End_of_CanvasContent1'),outputs('Index_Start_of_CanvasContent1'))

     

     

     

    SilannaITDale_8-1688015264675.png

     

    Next, we want to obtain the contents of the CanvasContent as it's own string. We can do this by using substring function with the Start Index and the Length of the CanvasContents which we have obtained above.

     

     

     

    substring(string(body('Checkout_Site_for_editing')),outputs('Index_Start_of_CanvasContent1'),outputs('Length_of_CanvasContent1'))

     

     

     

    SilannaITDale_9-1688015430000.png

     

    Now this is where you can make your desired changes to the SharePoint page. I would recommend copying the Body object into a text editor like Visual Studio Code and using Ctrl + F to find contents on your page.

     

    Once you know what the content is to replace you can use it in the replace function. For my specific use case, I am trying to replace the old/current tip of the week with a new tip of the week. This is why the Current column from my SharePoint list is important as I can determine from my SharePoint list what the text will be in my Body object that I need to replace.

     

     

     

    replace(outputs('Get_CanvasContent1'),variables('Old tip of the week'),variables('Tip of the week'))

     

     

     

    SilannaITDale_10-1688015734932.png

     

    Now we want to format the CanvasContents to match the HTTP payload structure so that the Graph API will recognise it. I have used a variable to store this but you could use a Compose tile with the concat() function as well. I just found the text variable to be easier to use. The Outputs variable is just my output from the tile above where my replace() function is. The replace() function will return the new string after the replacement has been made. which in our case is the CanvasContent with the changes made.

     

     

     

    {"__metadata": {"type": "SP.Publishing.SitePage"},"PageRenderingState":{},@{outputs('Compose_Canvas_Content')}"BannerImageUrl": "","Title":"Home"}

     

     

     

    SilannaITDale_12-1688016239871.png

     

    Now we need to Check in the changes we have made as a draft. To do this, use another Send an HTTP request to SharePoint tile

    SilannaITDale_2-1688014653592.png

     

    Fill it out as bellow. Note that the Template Site variable is the variable where I save my CanvasContents after I made changes. It is the tile from the step above. Also remember that the ItemId is from the file metadata tile from the first step.

    SilannaITDale_14-1688016343425.png

     

    Lastly, you will need to publish the changes. 

    To do this, use another Send an HTTP request to SharePoint tile

    SilannaITDale_2-1688014653592.png

     

    Note that the ItemId tile is from the file metadata tile from the start of the flow.

    SilannaITDale_16-1688016562096.png

     

    Now go refresh your SharePoint site and you should see the changes you made!

     

    I know this is a bit of a tricky flow to implement but there is no other way I know of how to do this.

     

    Let me know how it goes or if you have any troubles.

     

    Please mark this as the solution if it has helped you so others can find it.

     

    Thanks,

    Dale 🙂

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

October 2024 Newsletter…

October 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #4 How to Conntact Support…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 142,523

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,707

Leaderboard