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.
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).
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
Next you will need a Send an HTTP request to SharePoint tile
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.
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.
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":')
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":')
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'))
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'))
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'))
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"}
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
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.
Lastly, you will need to publish the changes.
To do this, use another Send an HTTP request to SharePoint tile
Note that the ItemId tile is from the file metadata tile from the start of the flow.
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 🙂