0

Firstly a confession, I knowingly added site columns to default content types. Our organisation is rebuilding its intranet and this seemed the easiest way to apply managed properties to a bunch of new sites.

Perhaps due to a page refresh issue, I accidentally added a site column twice. Before checking the implications, I deleted the duplicated site column not realising that it would orphan all instances of the column. I resolved to clean up manually but that's become a mess.

Without wrecking the whole site, I would like to return site content types to their default state. I've seen this answer https://sharepoint.stackexchange.com/a/26225/94004, which looks promising. My question is what else happens when this is done? What gets reset, and what remains?

Thanks Hans

0

The stsadm commands do not work for SharePoint Online. You can use SharePoint Online PowerShell instead. (ps. I have tested with PnP PowerShell and it doesn’t work in this scenario.)

The script will deactivate and then reactivate the CTypes Feature in your site. All default site content types will be removed and then recreated, removing all columns and templates added manually.

Here is a sample script for your reference:

#Load SharePoint Online 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 Disable Feature in SharePoint Online
Function Disable-SPOFeature {
    param ($SiteCollURL,$UserName,$Password,$FeatureGuid)
    Try {    
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollURL)
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
        $Ctx.Credentials = $Credentials
        $Site=$Ctx.Site
 
        #Check the Feature Status
        $FeatureStatus =  $Site.Features.GetById($FeatureGuid)
        $FeatureStatus.Retrieve("DefinitionId")
        $Ctx.Load($FeatureStatus)
        $Ctx.ExecuteQuery()
 
        #Deactivate the feature if its enabled
        if($FeatureStatus.DefinitionId -ne $null){
            Write-Host "Disabling Feature $FeatureGuid..." -ForegroundColor Yellow
            $Site.Features.Remove($FeatureGuid, $true) | Out-Null
            $Ctx.ExecuteQuery()
            Write-Host "Feature has been disabled on site $SiteCollURL!" -ForegroundColor Green
        }
        else{
            Write-host "Feature is Not Active on the Site collection!" -ForegroundColor Red
        }
    }
    Catch{
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}
 
#Function to Enable Feature in SharePoint Online
Function Enable-SPOFeature{
    param ($SiteCollURL,$UserName,$Password,$FeatureGuid)
    Try{    
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollURL)
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
        $Ctx.Credentials = $Credentials
        $Site=$Ctx.Site
 
        #Check the Feature Status
        $FeatureStatus =  $Site.Features.GetById($FeatureGuid)
        $FeatureStatus.Retrieve("DefinitionId")
        $Ctx.Load($FeatureStatus)
        $Ctx.ExecuteQuery()
 
        #sharepoint online activate feature using powershell (if its not enabled already)
        if($FeatureStatus.DefinitionId -eq $null){
            Write-Host "Enabling Feature $FeatureGuid..." -ForegroundColor Yellow
            $Site.Features.Add($FeatureGuid, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None) | Out-Null
            $Ctx.ExecuteQuery()
            Write-Host "Feature Enabled on site $SiteCollURL!" -ForegroundColor Green
        }
        else{
            Write-host "Feature is Already Active on the Site collection!" -ForegroundColor Red
        }
    }
    Catch{
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}
  
#Parameters to Activate Feature
$SiteCollURL = "<SiteURL>"
$UserName = "<AdminAccount>"
$Password = "<Password>"
$FeatureGuid = [System.Guid] ("695b6570-a48b-4a8e-8ea5-26ea7fc1d162") #CTypes Feature
$SecurePassword = ConvertTo-SecureString $Password –asplaintext –force 
 
#Disable Feature
Disable-SPOFeature -SiteCollURL $SiteCollURL -UserName $UserName -Password $SecurePassword -FeatureGuid $FeatureGuid

#Enable Feature
Enable-SPOFeature -SiteCollURL $SiteCollURL -UserName $UserName -Password $SecurePassword -FeatureGuid $FeatureGuid

References: SharePoint Online: Deactivate Feature using PowerShell. / SharePoint Online: Activate a Feature using PowerShell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.