GitHub Icon Image
GitHub

Open Office documents in the Client

Summary

One of the most common requests from customers is that any office document in SharePoint site should open in the desktop client application so here goes:

Implementation

  • Open VS Code
  • Create a new file
  • Write a script as below
  • Change the variables to target to your environment
  • Run the script

Screenshot of Output

Example Screenshot

  • PnP PowerShell
  • CLI for Microsoft 365

# SharePoint online site url
$siteUrl = "https://contoso.sharepoint.com/sites/SPConnect"

# Scope of feature: Web or Site
$featureScope = "Site"

# SharePoint Feature ID: in this case "Open Documents in Client Applications by Default (OpenInClient)"
$featureId = "8a4b8de2-6fd8-41e9-923c-c7c3c00f8295"	

# Another common scenario is Activating Document ID feature (Feature ID: b50e3104-6812-424f-a011-cc90e6327318)

# Connect to SharePoint Online site
Connect-PnPOnline -Url $siteUrl -Interactive

# Get Feature from SharePoint site
$spFeature = Get-PnPFeature -Scope $featureScope -Identity $featureId

# Check if feature found or not
if ($spFeature.DefinitionId -eq $null) {  
    Write-host "Activating Feature ($featureId)..." 
	
    # Activate the site feature
	Enable-PnPFeature -Scope $featureScope -Identity $FeatureId -Force
 
    Write-host -f Green "Feature ($featureId) has been activated Successfully!"
}
else {
    Write-host "Feature ($featureId) is already active on this site!"
}   
   

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


# SharePoint online site url
$siteUrl = "https://contoso.sharepoint.com/sites/SPConnect"

# Scope of feature: Web or Site
$featureScope = "Site"

# SharePoint Feature ID: in this case "Open Documents in Client Applications by Default (OpenInClient)"
$featureId = "8a4b8de2-6fd8-41e9-923c-c7c3c00f8295"	

# Another common scenario is Activating Document ID feature (Feature ID: b50e3104-6812-424f-a011-cc90e6327318)

# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
   m365 login
}

# Get all enabled features from SharePoint site
$allEnabledFeatures = m365 spo feature list --webUrl $siteUrl --scope $featureScope | ConvertFrom-Json

# Get feature from enabled features
$spfeature = $allEnabledFeatures | Where-Object { $_.DefinitionId -eq $featureId }

# Check if feature found or not
if($spfeature.DefinitionId -eq $null) {  
    Write-host "Activating Feature ($featureId)..." 
	
    # Activate the site feature
	m365 spo feature enable --webUrl $siteUrl --id $featureId --scope $featureScope
 
    Write-host -f Green "Feature ($featureId) has been activated Successfully!"
}
else {
    Write-host "Feature ($featureId) is already active on this site!"
}

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

Contributors

Author(s)
Kasper Larsen, Fellowmind
Ganesh Sanap

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI