OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

Trigger the edit page experience from your SharePoint Framework solution

SharePoint Framework SPFx
post

This post is over a year old, some of this information may be out of date.

This is a quick article to show you how you can open the edit page experience from your SharePoint Framework solution. Such a trigger could be useful so that you can trigger the page its edit mode from within your web part, footer, …

Nothing more to say about it, so here is the code:

const restApi = `${this.context.pageContext.web.absoluteUrl}/_api/sitepages/pages(${this.context.pageContext.listItem.id})/checkoutpage`;
this.props.context.spHttpClient.post(restApi, SPHttpClient.configurations.v1, {})
.then((data: SPHttpClientResponse) => {
if (data.ok) {
// Redirect the user to the edit page mode
let location: string = document.location.href;
if (location.indexOf('Mode=Edit') === -1) {
location = `${location}${(location.indexOf("?") === -1 ? "?" : "&")}Mode=Edit`;
}
document.location.href = location;
} else {
// Page is already checked out
data.json().then((err: IErrorMsg) => {
if (err && err.error && err.error.message) {
// Log the message from SharePoint
console.log(err.error.message);
}
});
}
view raw edit-page.ts hosted with ❤ by GitHub

Here is an example of what you could expect:

Show image Trigger edit page
Trigger edit page

When the page is checked out to someone else, you SharePoint lets you know who is editing it:

Show image Page is already checked out
Page is already checked out

Other actions

Other actions like saving, publishing and discarding are also availble:

  • _api/sitepages/pages({id})/SavePageAsDraft
  • _api/sitepages/pages({id})/DiscardPage
  • _api/sitepages/pages({id})/SavePage

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code