SharePoint Online: Change UI to Modern Experience or Classic Experience using PowerShell

Requirement: Change Classic Experience to Modern Experience and Modern UI to Classic in SharePoint Online.

How to Activate Modern Experience from Classic?
The modern experience in SharePoint Online lists and libraries is faster, mobile friendly and easier to use.  It brings new functionalities and components, However, some features can only be used in the classic experience. So, you may need to switch between modern UI and classic experience in SharePoint Online. To enable modern experience in SharePoint Online, use the link "Exit classic Experience" at the bottom left corner.
sharepoint online disable modern experience
This turns ON modern experience. To disable modern experience and switch back to classic in SharePoint Online use the link "Return to Classic SharePoint"
sharepoint online enable modern experience

PowerShell to Switch Between Modern Experience and Classic Experiences
To enable modern experience, we have to disable classic experience feature and to disable modern experience we should re-enable the classic experience feature in SharePoint Online. This PowerShell script is spitted into two different functions.
  1. Sets the given feature status to enabled or disabled at given scope such as: Site or Web
  2. Activates the relevant feature at appropriate scope(s) to set the UI experience.
You can use this PowerShell script to enable or disable modern experience at site collection or site levels.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Enable or Disable Feature in SharePoint Online
Function Set-SPOFeatureStatus
{
    [CMDLetBinding()]
    Param
    (
        [Parameter(Mandatory=$True)][Microsoft.SharePoint.Client.ClientContext]$Context,
        [Parameter(Mandatory=$True)][GUID]$FeatureGUID,
        [Parameter(Mandatory=$True)][ValidateSet('Site','Web')][String]$Scope,
        [Parameter(Mandatory=$True)][ValidateSet('Enable','Disable')][String]$Status
    )
    If($Scope -eq "Site")
    {
        #Get the Site
        $Site = $Context.Site
        #Get the Feature Status
        $FeatureStatus =  $Site.Features.GetById($FeatureGuid)
        $FeatureStatus.Retrieve("DefinitionId")
        $Context.Load($FeatureStatus)
        $Context.ExecuteQuery()

        If($Status -eq "Enable")
        {
            #Activate the feature if its not enabled already
            If($FeatureStatus.DefinitionId -eq $null)
            {
                #Enable the Feature
                $Site.Features.Add($FeatureGuid, $True, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None) | Out-Null
                $Context.ExecuteQuery()
                Write-host -f Green "`tFeature has been Enabled at Site Level!"
            }
            Else
            {
                Write-host -f Yellow "`tFeature is Already Enabled at Site Level!"
            }
        }
        Elseif($Status -eq "Disable")
        {
            #De-Activate the feature if its enabled already
            If($FeatureStatus.DefinitionId -ne $null)
            {
                #Disable feature
                $Site.Features.Remove($FeatureGuid, $True) | Out-Null
                $Context.ExecuteQuery()
                Write-host -f Green "`tFeature has been Disabled at Site Level!"
            }
            Else
            {
                Write-host -f Yellow "`tFeature is Already Disabled at Site Level!"
            }
        }
    }
    ElseIf($Scope -eq "Web")
    {
        #Get the web
        $Web = $Context.Web
        #Get the Feature Status
        $FeatureStatus =  $Web.Features.GetById($FeatureGUID)
        $FeatureStatus.Retrieve("DefinitionId")
        $Context.Load($FeatureStatus)
        $Context.ExecuteQuery()
    
        If($Status -eq "Enable")
        {
            #Activate the feature if its not enabled already
            If($FeatureStatus.DefinitionId -eq $null)
            {
                #Enable Feature
                $Web.Features.Add($FeatureGUID, $True, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)| Out-Null
                $Context.ExecuteQuery()
                Write-host -f Green "`tFeature has been Enabled at Web Level!"
            }
            Else
            {
                Write-host -f Yellow "`tFeature is Already Enabled at Web Level!"
            }
        }
        ElseIf($Status -eq "Disable")
        {
            #De-Activate the feature if its enabled already
            If($FeatureStatus.DefinitionId -ne $null)
            {
                #Disable Classic Experience feature, which Enables Modern UI
                $Web.Features.Remove($FeatureGUID, $True) | Out-Null                
                $Context.ExecuteQuery()
                Write-host -f Green "`tFeature has been Disabled at Web Level!"   
            }
            Else
            {
                Write-host -f Yellow "`tFeature is Already Disabled at Web Level!"
            }
        }
    }
}

#Function to set UI  experience to Modern or Classic at Site or Web Level
Function Set-SPOUIExperience
{
    [CMDLetBinding()]
    Param
    (
        [String]$SiteURL, 
        [Parameter(Mandatory=$True)][ValidateSet('Modern','Classic')][String]$Experience,
        [Parameter(Mandatory=$True)][ValidateSet('Site','Web')][String]$Scope
    )

    Try {
        #Setup Credentials to connect
        $Cred = Get-Credential

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Site Scoped Feature "EnableDefaultListLibrarExperience" - Classic Experience
        $SiteFeatureGuid = New-Object System.Guid "E3540C7D-6BEA-403C-A224-1A12EAFEE4C4"
        
        #Web Scoped Feature "EnableDefaultListLibrarExperience" - Classic Experience
        $WebFeatureGUID = New-Object System.Guid "52E14B6F-B1BB-4969-B89B-C4FAA56745EF" 

        #Enable or Disable Modern Experience based on the parameters
        If($Scope -eq "Site")
        {
            #Get the web
            $Web = $Ctx.Web
            $Ctx.Load($Web)
            $Ctx.ExecuteQuery()

            If($Experience -eq "Modern")
            { 
                #Disable Classic Experience feature at site level
                Write-host -f Green "Enabling Modern Experience at the Site Collection by Disabling Classic UI Feature..."
                Set-SPOFeatureStatus -Context $Ctx -FeatureGUID $SiteFeatureGuid -Scope Site -Status Disable

                #Disable Classic Experience feature at web level
                Set-SPOUIExperience $Web.URL -Experience $Experience -Scope Web

                #Process each subsite in the site
                $Subsites = $Web.Webs
                $Ctx.Load($Subsites)
                $Ctx.ExecuteQuery()        
                Foreach ($SubSite in $Subsites)
                {
                    #Call the function Recursively
                    Set-SPOUIExperience $SubSite.URL -Experience $Experience -Scope Web
                }
            }
            ElseIf($Experience -eq "Classic")
            {
                #Enable Classic Experience feature at site level
                Write-host -f Green "Enabling Classic Experience at the Site Collection..."
                Set-SPOFeatureStatus -Context $Ctx -FeatureGUID $SiteFeatureGuid -Scope Site -Status Enable                
                
                #Enable Classic Experience feature at web level
                Set-SPOUIExperience $Web.URL -Experience $Experience -Scope Web

                #Process each subsite in the site
                $Subsites = $Web.Webs
                $Ctx.Load($Subsites)
                $Ctx.ExecuteQuery()        
                Foreach ($SubSite in $Subsites)
                {
                    #Call the function Recursively
                    Set-SPOUIExperience $SubSite.URL -Experience $Experience -Scope Web
                }
            }
        }
        ElseIf($Scope -eq "Web")
        {
            If($Experience -eq "Modern")
            {
                #Disable Classic Experience
                Write-host -f Green "Enabling Modern Experience at the Web $($SiteURL) by Disabling Classic UI Feature..."
                Set-SPOFeatureStatus -Context $Ctx -FeatureGUID $WebFeatureGUID -Scope Web -Status Disable                
            }
            ElseIf($Experience -eq "Classic")
            {
                #Enable Classic Experience
                Write-host -f Green "Enabling Classic Experience at the Web $($SiteURL)"
                Set-SPOFeatureStatus -Context $Ctx -FeatureGUID $WebFeatureGUID -Scope Web -Status Enable
            }
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Set Variable
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"

#Call the function to set UI experience
Set-SPOUIExperience -SiteURL $SiteURL -Experience Modern -Scope Site
When you activate the site level feature, it makes sure new sites you create follows the UI experience set. Similarly web level feature sets UI experience on existing webs. You can call the function with different parameters. E.g. To set classic experience at web level, use:
Set-SPOExperience -SiteURL $SiteURL -Experience Classic -Scope Web
This PowerShell script sets the UI experience for all lists and libraries at the given scope. However, settings at individual list and library level can override site wide settings: SharePoint Online: How to Change List UI to Modern Experience?
SharePoint Online: Change UI to Modern Experience or Classic Experience using PowerShell SharePoint Online: Change UI to Modern Experience or Classic Experience using PowerShell Reviewed by Salaudeen Rajack on May 19, 2019 Rating: 5

6 comments:

  1. Hi, the question is not relevant with the topic I was wondering is there any documentation for Bulk adding document libraries to quick launch menu using Sharepoint Online ? Thanks

    ReplyDelete
    Replies
    1. Many thanks for the reply.

      $List.OnquickLaunch=$True is already set but somehow can not add more than one document library.

      Tried $ListName = Import-Csv "C:\Users..\ListName.csv" path so it could read from csv, the names of Document Libraries but received the following error.

      Error Setting List Properties! Exception calling "ExecuteQuery" with "0" argument(s): "List ' ' doe
      s not exist at site with URL.

      Thanks for the support.

      Delete
  2. I have solved the issue. Sorry again. By making some modifications. Hope it would be useful for others :)

    $UserName="admin@tenant.onmicrosoft.com"
    $Password ="****"

    #Setup Credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the web and List
    $Web=$Ctx.Web

    $Lists = $Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    ForEach($List in $Lists)
    {
    #Get the List Name
    $List.OnQuickLaunch=$True
    $List.Update()
    }


    $Ctx.ExecuteQuery()

    }

    catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }


    Have to sort only document libraries from those lists. Hopefully I will make it :)



    ReplyDelete
    Replies
    1. Sure, Use:
      $DocLibraries = $Ctx.Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}


      Delete
  3. Many thanks Salaudeen :)

    Implemented this method.

    ForEach($List in $Lists)

    {

    if([int]$list.BaseTemplate -eq 101)

    {

    $listcount +=1
    Write-Host " – "$List.Title

    $List.OnQuickLaunch=$True
    $List.Update()

    }

    }

    Your blog is golden resource for CSOM users.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.