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.

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:

https://graph.microsoft.com/beta/groups/{teamId}/onenote/notebooks
JSON:
{
	"displayName": "Export Notebook"
}

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

https://graph.microsoft.com/beta/groups/{teamId}/onenote/notebooks/{notebookID}/sections
{
    "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.

https://graph.microsoft.com/beta/groups/{teamId}/onenote/sections/{SectionID}/pages
HTML start can be something like this:
<html>
  <head>
    <title>Messages in NCC 1701 Crew-team</title>
  </head>
  <body>
    <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.

https://github.com/veskunopanen/Teams-Graph-API

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

Vastaa

Täytä tietosi alle tai klikkaa kuvaketta kirjautuaksesi sisään:

WordPress.com-logo

Olet kommentoimassa WordPress.com -tilin nimissä. Log Out /  Muuta )

Google photo

Olet kommentoimassa Google -tilin nimissä. Log Out /  Muuta )

Twitter-kuva

Olet kommentoimassa Twitter -tilin nimissä. Log Out /  Muuta )

Facebook-kuva

Olet kommentoimassa Facebook -tilin nimissä. Log Out /  Muuta )

Muodostetaan yhteyttä palveluun %s

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