Featured image of post Automating with PowerShell: Automatically following all Sharepoint Sites or Teams for all users

Automating with PowerShell: Automatically following all Sharepoint Sites or Teams for all users

So a while back we had a client that uses a lot of sharepoint sites. The client only used Sharepoint online, and found it hard to find all the sites in one place. We pointed them to https://YOURDOMAIN.sharepoint.com/_layouts/15/sharepoint.aspx which gives a nice overview of sites and teams.

They came back to us saying it was a little bit of a hassle to use the overview as it only shared recently used sites, or sites that have been followed manually. Of course we wanted to help them get over this hassle so we scripted this; The following script allows you to grab all sites the user is a member of. It then adds the site to the favorites for that user. You can schedule this so each new user automatically gets added.

The Script

The script uses the Secure Application Model or just a generic Azure Application with permissions to read all sites for your tenants, and all your CSP tenants. The script finds each Team the user has joined and adds them to the favorites for that user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$TenantID = 'CUSTOMERTENANT.ONMICROSOFT.COM'
$ApplicationId = "YOURPPLICTIONID"
$ApplicationSecret = "YOURAPPLICATIONSECRET"

$body = @{
    'resource'      = 'https://graph.microsoft.com'
    'client_id'     = $ApplicationId
    'client_secret' = $ApplicationSecret
    'grant_type'    = "client_credentials"
    'scope'         = "openid"
}

$ClientToken = Invoke-RestMethod -Method post -Uri "https://login.microsoftonline.com/$($tenantid)/oauth2/token" -Body $body -ErrorAction Stop
$headers = @{ "Authorization" = "Bearer $($ClientToken.access_token)" }
$Users = (Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/users" -Headers $Headers -Method Get -ContentType "application/json").value.id

foreach ($userid in $users) {
    $AllTeamsURI = "https://graph.microsoft.com/beta/users/$($UserID)/JoinedTeams"
    $Teams = (Invoke-RestMethod -Uri $AllTeamsURI -Headers $Headers -Method Get -ContentType "application/json").value
    foreach ($Team in $teams) {
        $SiteRootReq = Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/groups/$($Team.id)/sites/root" -Headers $Headers -Method Get -ContentType "application/json"
        $AddSitesbody = [PSCustomObject]@{
            value = [array]@{
                "id" = $SiteRootReq.id
            }
        } | convertto-json
        $FavoritedSites = Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/users/$($userid)/followedSites/add" -Headers $Headers -Method POST -body $AddSitesBody -ContentType "application/json"
        write-host "Added $($SiteRootReq.webURL) to favorites for $userid"
}

}

And that’s it! As always, Happy PowerShelling!

All blogs are posted under AGPL3.0 unless stated otherwise
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy