Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SharePoint Online Site Policy #203

Open
Abhijeet1719 opened this issue Apr 7, 2022 · 7 comments
Open

SharePoint Online Site Policy #203

Abhijeet1719 opened this issue Apr 7, 2022 · 7 comments

Comments

@Abhijeet1719
Copy link

Abhijeet1719 commented Apr 7, 2022

@PowershellScripts
I have created site policy in content type hub and trying to apply site policy to site collection using powershell but its not applying. I am able to activate at site collection level but not able to apply it. I tried it with another approach i.e applied it manually and then remove it and after that I tried with powershell it worked...bt I want to applied it without manually activating and de activating.
Can anyone please suggest?
Also, Our tenancy has multiple site collections and webs, and all have a Site Policy applied. I've now been asked to remove the policy from all sites. Doing it manually is not an option.

Is there a CSOM or PnP command or snippet I can execute via PowerShell that will allow me to remove a Site Policy from a site?

@PowershellScripts
Copy link
Owner

Hi @Abhijeet1719

Have a look at https://github.com/PowershellScripts/SharePointOnline-ScriptSamples/tree/develop/Site%20Management/Site%20Policy/RemovePolicy

I wrote that one for you

It is still in the testing phase, so please let me know what you think and how it is working for you

@Abhijeet1719
Copy link
Author

Abhijeet1719 commented Apr 19, 2022

Hi @Abhijeet1719

Have a look at https://github.com/PowershellScripts/SharePointOnline-ScriptSamples/tree/develop/Site%20Management/Site%20Policy/RemovePolicy

I wrote that one for you

It is still in the testing phase, so please let me know what you think and how it is working for you

@PowershellScripts Thanks for this..

but I am getting error with this script.

Please find attached screenshot.
image

https://social.technet.microsoft.com/Forums/office/en-US/d3d86389-6be7-48ec-ad09-1e30968255a6/unable-to-remove-a-readonly-content-type-from-a-list-or-library?forum=sharepointadminprevious

@PowershellScripts
Copy link
Owner

That's good feedback. I'm guessing it comes from the fact that you published the content type from the content type hub. Let me try to simulate that and I will see what can be done.

@PowershellScripts
Copy link
Owner

@Abhijeet1719 What do you get when you try Get-PnPPolicy on the site?

@Abhijeet1719
Copy link
Author

Abhijeet1719 commented Apr 20, 2022

@PowershellScripts I tried below command but same error...
Get-PnPSitePolicy -Name $PolicyName

@Abhijeet1719
Copy link
Author

Abhijeet1719 commented Apr 20, 2022

@PowershellScripts
Another strange thing is I am trying to run below script to apply policy using powershell but i am not able to apply it. I applied- removed it manually and then again I tried to apply using script its worked. So first you have to manually apply it then only its working. For new site collection where I didnt apply manually for that site collection below script is not working.

-------Script---------
Import-Module SharePointPnPPowerShellOnline

#Reading CSV
$filePath = $PSScriptRoot + "\SiteURL.csv"
$CSVData = Import-Csv -LiteralPath $filePath

foreach ($row in $CSVData)
{
$SiteUrl = $row.Site
#$cred= Get-Credential
#Add-PnPStoredCredential -Name $SiteName -Username $cred.UserName -Password $cred.Password
$connection = Connect-PnPOnline -Url $SiteUrl -UseWebLogin

$PolicyName= "SPOSitePolicy"

#Call the function to apply site policy

Apply-PnPSitePolicy -SiteUrl $SiteUrl -PolicyName $PolicyName

}

#Apply Site Policy for a Site collection
Function Apply-PnPSitePolicy([String]$SiteUrl, [String]$PolicyName)
{
#Connect to the Site
try
{
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
$ContentTypeID = "0x010085EC78BE64F9478AAE3ED069093B9963"
#Check if "Site Policy" Feature is active
$SitePolicyFeature = Get-PnPFeature -Identity "2fcd5f8a-26b7-4a6a-9755-918566dba90a" -Scope Site -Web $SiteUrl
If($SitePolicyFeature.DefinitionId -eq $null)
{
#Activate "Site Policy" Feature for the site collection
Enable-PnPFeature -Identity "2fcd5f8a-26b7-4a6a-9755-918566dba90a" -Scope Site
Write-Host "Site Policy Feature is Activated at $($SiteUrl)" -ForegroundColor Green
#Start-Sleep 30
}

    #Get Policy to Activate
    $SitePolicyToActivate = Get-PnPSitePolicy -Name "SPOSitePolicy"
   # $SitePolicyToActivate = Get-PnPSitePolicy -AllAvailable

   # $SitePolicyToActivate = Get-PnPContentType | where {($_.Id -match $ContentTypeID) -and ($_.Id -notlike $ContentTypeID)} | select name, id

    foreach($sitePolicy in $SitePolicyToActivate)
    {
        Write-Host "Policy name-" $sitePolicy.Name
    }
    
    If ($SitePolicyToActivate)
    {            
        #Remove content type
       # Remove-PnPContentType -Identity $PolicyName -Force
        
        #Apply Site Policy
        Set-PnPSitePolicy -Name $SitePolicyToActivate.Name
        
        #Close the site
        Set-PnPSiteClosure -State Closed

        Write-Host "Site Policy Applied to $($SiteUrl)" -ForegroundColor Green
    }
    Else
    {
        write-Host "Site Policy '$($PolicyName)' not found in Site $($SiteUrl)" -ForegroundColor Yellow
    }
}
catch
{
    $e = $_.Exception   
        $line = $_.InvocationInfo.ScriptLineNumber   
        $msg = $e.Message   
        Write-Host –ForegroundColor Red "Caught Exception: $e at $line"   
        Write-Host $msg   
        Write-Host "Something went wrong"  
}

}

@Abhijeet1719
Copy link
Author

@PowershellScripts HI Do u have update for me?

Thanks....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants