December 3, 2020

Get Microsoft Teams Team name and link from file url

Using the NWS PowerTool (implemented with AutoHotkey), when you intellipaste a link to a file located in a Microsoft Teams SharePoint, before the link a nice link to the Team can be displayed.
Example:  Knowledge Brokers TeamGeneral > Knowledge Broker Toolkit > Overall Introduction_Knowledge Brokers.pptx (part highlighted is a link to the Team behind the file)
I explain here how this feature works.
  

Prerequisite

For this feature to work, you need to have setup PowerShell Teams functionality.
Refer to this post how to setup Teams PowerShell

Export Teams to cache file

This is done by the function ahk/Lib/Teams.ahk->Teams_ExportTeams function.
See extract below:

Teams_ExportTeams() {
; CsvFile := Teams_ExportTeams
; returns empty if not created/ failed

If Not Teams_PowerShellCheck()
    return
CsvFile = %A_ScriptDir%\Teams_list.csv
If FileExist(CsvFile)
    FileDelete, %CsvFile%
; Create .ps1 file
PsFile = %A_Temp%\Teams_ExportTeams.ps1
; Fill the file with commands
If FileExist(PsFile)
    FileDelete, %PsFile%

Domain := People_GetDomain()
If (Domain ="") {
    MsgBox 0x10, Teams Shortcuts: Error, No Domain defined!
    return
}

OfficeUid := People_GetMyOUid()
sText = Connect-MicrosoftTeams -AccountId %OfficeUid%@%Domain%
sText = %sText%`nGet-Team -User %OfficeUid%@%Domain% | Select DisplayName, MailNickName, GroupId, Description | Export-Csv -Path %CsvFile% -NoTypeInformation
FileAppend, %sText%,%PsFile%

; Run it;RunWait, PowerShell.exe -NoExit -ExecutionPolicy Bypass -Command %PsFile% ;,, Hide
RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command %PsFile% ,, Hide
return CsvFile
}

This function is explained in more details in this previous post Get team name from sharepoint url

Refreshing Teams Export

For runtime optimization, this isn't done each time but only on a user manual action.
You can refresh the Teams Export file via the Menu in the PowerTool Systray Icon (right-mouse click on the system icon) Settings->IntelliPaste->Refresh...


Getting link to the Team behind

In ahk/Lib/IntelliPaste.ahk->IntelliHtml function, extract below:

If RegExMatch(sInput,"/teams/(team_[^/]*)/",sMatch) {
    CsvFile = %A_ScriptDir%\Teams_list.csv
    If !FileExist(CsvFile) {
        CsvFile := Teams_ExportTeams() ; requires PowerShell
        If (CsvFile = "") 
            Goto, Cont1
    }
    MailNickName := sMatch1
    TeamName := ReadCsv(CsvFile,"MailNickName",MailNickName,"DisplayName")
    If Not (TeamName = "") {
        GroupId := ReadCsv(CsvFile,"MailNickName",MailNickName,"GroupId")
        TeamLink = https://teams.microsoft.com/l/team/?groupId=%GroupId%
        sHtml = <a href="%TeamLink%">%TeamName% Team</a>: %sHtml%
    }
}
Cont1:

From the GroupId of a Team you can get a link to it built like this:
TeamLink = https://teams.microsoft.com/l/team/?groupId=%GroupId%

See also

AutoHotkey: read csv table

Get team name from sharepoint url

Teams powershell setup

Sharing nice links from SharePoint/ OneDrive Sync location

No comments:

Post a Comment