Search code examples
microsoft-graph-apimicrosoft-graph-teams

Get all messages and replies from Microsoft teams channel - Graph paging


I am using the script that has been published on this website [https://myteamsday.com/2019/10/08/exporting-team-channel-messages/][1]

which exports channel messages to a OneNote file .the script works fine, My issue and question is that I can not add paging to the script ( in order to retrieve all messages using "@odata.nextLink") I am a newbie in developing and MS graph, I know that I need to add an if statement or While() to the code but I am not sure how to do that.

Here is where the script reads all messages and replies and writes them to a Onenote file

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

foreach ($message in $graphResponse.value)
{
$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>
</html>
'

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

Any help is much appreciated


Solution

  • Here first I am making the call and then I get the 20 messages with nextLink. Taking that nextLink I am just using a while loop where nextLink is not null and calling the API with that nextLink again and saving the result in $result variable. Try below code, it worked for me.

    $result= $null
    $messagesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages"
    $graphResponse = Invoke-RestMethod -Method Get -Uri $messagesURI  -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
    while($graphResponse."@odata.nextLink" -ne $null)
    {
    $result = $result + $graphResponse."value"
    $graphResponse = Invoke-RestMethod -Method Get -Uri $graphResponse."@odata.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>
    </html>
    '
    
    $graphResponse = Invoke-RestMethod -Method Post -Uri $addPageURL -Headers @{"Authorization"="Bearer $delegatedaccessToken"} -Body $pageHtml -ContentType "text/html"