How to find and replace the content of the Pages in Sharepoint using Microsoft graph API?

Akbar Husain 5 Reputation points
2023-03-27T05:46:25.96+00:00

Hello,

What I am doing here is, first of all fetching the pages from sharepint and want to find and replace the content of that pages and save it back to the sharepoint site.

Example: I have created one page and in that page I have copied the link of google i.e. https://www.google.com. Now I have to update that text or link such that it will be https://www.microsoft.com. and this will be done using microsoft graph api.

Step 1: To fetch the Sharepoint Site Pages and I hvae fetched all the published pages succesfully.

Step 2: Now I have to find the text like https://www.ggogle.com and replace it with https://www.microsoft.com and save the updated page to the sharepoint site.

Anyone have any idea how can I achieve this?

Below is the sample code for fetching the pages from the Sharepoint:

Private Async Function GetSitePagesAsync(ByVal siteId As String, ByVal orderBy As Short, ByVal sortDescending As Boolean) As Task(Of List(Of SitePage))
        
            If StringIsNullOrEmpty(siteId) Then Throw New ArgumentNullException("Site Id")
         
            Dim objSitePages As New List(Of SitePage)
            Dim objPageSitePages As SitePagesCollectionPage = Await _Client.Sites(siteId).Pages.Request().GetAsync
            objSitePages = Await GetPagingData(objPageSitePages, objSitePages, Nothing)
            If (Not objSitePages Is Nothing) AndAlso objSitePages.Count > 0 Then
                'objSitePages.RemoveAll(Function(f) IsSitePage(f) = False)
                Try
                    If orderBy = OptOrderBy.NAME Then
                        If sortDescending = True Then
                            objSitePages = objSitePages.OrderByDescending(Function(f) f.Name).ToList
                        Else
                            objSitePages = objSitePages.OrderBy(Function(f) f.Name).ToList
                        End If
                    End If
                Catch ex As Exception
                    LogError(ex)
                End Try
            End If
            Return objSitePages
        End Function


Private Async Function GetPagingData(ByVal pobjSrc As Object, ByVal pobjDest As Object, ByVal pdtFromDate As Date?) As Task(Of Object)

            If pobjSrc Is Nothing Then Return pobjDest
            Dim blnSearchByDate As Boolean = False
            Dim dtUtcFromDate As DateTimeOffset
            If (TypeOf pobjSrc Is DriveItemChildrenCollectionPage) AndAlso (Not pdtFromDate Is Nothing) AndAlso (pdtFromDate.HasValue) Then
                blnSearchByDate = True
                dtUtcFromDate = pdtFromDate.Value.ToUniversalTime
            End If
            If blnSearchByDate Then
                pobjDest.AddRange(DirectCast(pobjSrc, DriveItemChildrenCollectionPage).CurrentPage.Where(Function(f As DriveItem) If(Not f.File Is Nothing, f.LastModifiedDateTime.Value > dtUtcFromDate, (f.File Is Nothing))))
            Else
                pobjDest.AddRange(pobjSrc.CurrentPage)
            End If
            While (Not pobjSrc.NextPageRequest Is Nothing)
                pobjSrc = Await pobjSrc.NextPageRequest.GetAsync
                If blnSearchByDate Then
                    pobjDest.AddRange(DirectCast(pobjSrc, DriveItemChildrenCollectionPage).CurrentPage.Where(Function(f As DriveItem) If(Not f.File Is Nothing, f.LastModifiedDateTime.Value > dtUtcFromDate, (f.File Is Nothing))))
                Else
                    pobjDest.AddRange(pobjSrc.CurrentPage)
                End If
            End While
            Return pobjDest
        End Function
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,843 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,573 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CharanyaB-MSFT 1,891 Reputation points Microsoft Vendor
    2023-03-27T07:19:34.2866667+00:00

    Hello @Akbar Husain,

    Thanks for reaching out!

    Currently, it is not possible to find and replace the contents of a SharePoint page using Microsoft graph API.

    Only certain page properties can be updated from API request, check the below document for more details about updating a SitePage.

    https://learn.microsoft.com/en-us/graph/api/sitepage-update?view=graph-rest-beta&tabs=http#request-body

    Hope this helps.

    If the reply is helpful, please click "Accept Answer" and kindly upvote it. If you have additional questions about this answer, please click "Comment".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.