Exporting team channel messages into OneNote page using Graph API

There is now and then a need to delete a channel or team. While moving documents and other content in team to a new team or onto other storage the question remains about messages. Sometimes the need is even as simple as moving a channel to a another team (reorganizing). Very often messages are wanted to be stored.

Updated 26.1.2021 : how to add paging example. (community contribution to this post)

Setting the scope of this post

  • Choose a team and channel. Retrieve their IDs using Graph Explorer – for example
  • Using these examples you can retrieve 999 last messages of your chosen channel. If you want to retrieve more messages you need to build paging to Graph API call.
  • You will get the messages in backward order (latest first). In order to get the result look better: write messages to a array first and write messages in reverse order
  • I will create a new OneNote and Section in this example, but you can retrieve a existing OneNote ID and Section if you wish so.

Create the OneNote and a section

First, let’s crate a OneNote to our team:

	"displayName": "Export Notebook"

To add a section, you need to grab the NoteBookID from creation

    "displayName": "Backup of Channel Demo"

Then you need to store the SectionID and initialize a page. It is just HTML in fact. As you notice, the Section ID actually defines what Notebook is used in that team.

HTML start can be something like this:
    <title>Messages in NCC 1701 Crew-team</title>
    <p><b>This is what has been said in the Backup these -channel</b></p>

Retrieving messages

You should have Team ID and Channel ID stored somewhere. If you don’t: use Graph Explorer, for example, to retrieve them. Using delegated permissions (you need to have access to the team) get messages and go through them – adding messages to the OneNote. This time I use a PowerShell code snippet for this due to looping. Writing to OneNote can be done using Application Permissions.

The result

We have the messages with timestamp, writer name and content in the OneNote which we can then move to any location we like to. Since OneNote supports HTML format even gifs are stored. Just note: the latest messages are on the top.

Note: this does not move message attachments or anything that is not inside the message. GIFs are stored in there because they are just a HREF.

Get the PowerShell script from GitHub

I have upload the script how to do this to my GitHub profile. Feel free to get it from there.


Don’t forget to check for latest changes and additions in Docs.Microsoft.Com Graph API reference.

How to add paging to the results

I was contacted by Eyesun ( https://twitter.com/aysan_haghani ) asking for details how to add paging to results. Unfortunately I didn’t have the time at the moment but she managed to do it by herself and shared the result with me to be placed to the blog article for everyone to benefit! Big Thank You for Eyesun!

$result= $null
$messagesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages"
$graphResponse = Invoke-RestMethod -Method Get -Uri $messagesURI  -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
.nextLink" -ne $null)
$result = $result + $graphResponse."value"
$graphResponse = Invoke-RestMethod -Method Get -Uri $graphResponse."
.nextLink"  -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
$result = $result + $graphResponse."value"
#go through files and copy them to target

foreach ($message in $result)
$messageID = $message.id 
$pageHtml = $pageHtml + '<p>' + $message.createdDateTime+" "+ $message.from.user.displayName +" <b>" + $message.subject+"</b>:"+$message.body.content +'</p>'
$repliesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages/" + $messageID + "/replies"
$repliesResponse = Invoke-RestMethod -Method Get -Uri $repliesURI  -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
foreach ($reply in $repliesResponse.value ) {
$pageHtml = $pageHtml + '<p>&nbsp; &nbsp; &nbsp; &nbsp; ' + $reply.createdDateTime+" reply: "+ $reply.from.user.displayName +" " + $reply.subject+":"+$reply.body.content +'</p>'
$pageHtml = $pageHtml + '<p>---------------------------------------------------------------------------------------------</p>'


$pageHtml = $pageHtml + '  </body>

$graphResponse = Invoke-RestMethod -Method Post -Uri $addPageURL -Headers @{"Authorization"="Bearer $delegatedaccessToken"} -Body $pageHtml -ContentType "text/html"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.