从 Microsoft 团队频道获取所有消息和回复 - 图分页

时间:2021-01-20 21:35:28

标签: microsoft-graph-api microsoft-graph-teams

我使用的是本网站发布的脚本[https://myteamsday.com/2019/10/08/exporting-team-channel-messages/][1]

将频道消息导出到 OneNote 文件。脚本工作正常,我的问题是我无法向脚本添加分页(为了使用“@odata.nextLink”检索所有消息)我是新手在开发和 MS 图中,我知道我需要在代码中添加 if 语句或 While(),但我不知道该怎么做。

这里是脚本读取所有消息和回复并将它们写入 Onenote 文件的位置

$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"

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

在这里,我首先拨打电话,然后通过 nextLink 收到 20 条消息。使用 nextLink 我只是使用一个 while 循环,其中 nextLink 不为空,并再次使用该 nextLink 调用 API 并将结果保存在 $result 变量中。试试下面的代码,它对我有用。

$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"

答案 1 :(得分:0)

谢谢,Shiva,我正在 PowerShell 中尝试,这是错误消息

enter image description here