cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

Change the content of a SharePoint page

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...  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.

4 REPLIES 4
SilannaIT-Dale
Frequent Visitor

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 🙂

 

You are a legend mate 😊

This one helped me a lot to improvise my flow. 

 

Cheers

Krunal

Hi @krunal-amin,

 

glad I could help!

 

Kind Regards,

Dale

Hi there do you got template which can be exported?

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!    This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Community MembersNumber SolutionsSuper UsersNumber Solutions Deenuji 9 @NathanAlvares24  17 @Anil_g  7 @ManishSolanki  13 @eetuRobo  5 @David_MA  10 @VishnuReddy1997  5 @SpongYe  9JhonatanOB19932 (tie) @Nived_Nambiar  8 @maltie  2 (tie)   @PA-Noob  2 (tie)   @LukeMcG  2 (tie)   @tgut03  2 (tie)       Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 2: Community MembersSolutionsSuper UsersSolutionsPower Automate  @Deenuji  12@ManishSolanki 19 @Anil_g  10 @NathanAlvares24  17 @VishnuReddy1997  6 @Expiscornovus  10 @Tjan  5 @Nived_Nambiar  10 @eetuRobo  3 @SudeepGhatakNZ 8     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 3:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji32ManishSolanki55VishnuReddy199724NathanAlvares2444Anil_g22SudeepGhatakNZ40eetuRobo18Nived_Nambiar28Tjan8David_MA22   Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 4:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji11FLMike31Sayan11ManishSolanki16VishnuReddy199710creativeopinion14Akshansh-Sharma3SudeepGhatakNZ7claudiovc2CFernandes5 misc2Nived_Nambiar5 Usernametwice232rzaneti5 eetuRobo2   Anil_g2   SharonS2  

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages