Hi.
I have problem with CSOM vs SharePoint Content Policy. During running below script I receive error :
Cannot convert argument "ct", with value: "System.Object[]", for "CreatePolicy" to type "Microsoft.SharePoint.SPContentType": "Cannot convert the
"System.Object[]" value of type "System.Object[]" to type "Microsoft.SharePoint.SPContentType"."
At line:1 char:1
+ [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CreatePolicy($con ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Script:
$User = "XXXXXXX"
$SiteURL = "https://contoso.sharepoint.com/sites/ABC"
$16Hive = "C:\script\"
Add-Type -Path "$16Hive\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$16Hive\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-host -Prompt "Please enter your password" -AsSecureString
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
$rootWeb = $Context.Web
$rootSite = $Context.Site
$Context.Load($rootWeb)
$Context.Load($rootSite)
$Context.ExecuteQuery()
$featureDefinitionScope = [Microsoft.SharePoint.Client.FeatureDefinitionScope]::Farm
$featureId = "063c26fa-3ccc-4180-8a84-b6f98e991df3"
$features = $Context.Site.Features
try{
$Context.Load($features)
$Context.ExecuteQuery()
}
catch{$_;write-host "Error loading features - skipping";continue}
try{
$ff = $features.Add($featureId, $false, $featureDefinitionScope)
$Context.ExecuteQuery()
write-host "Feature " $featureId " activated successfuly" -foreground "Green"
}
catch {
if( $_.Exception.Message.Contains("is already activated at scope")){
write-host "Feature " $featureId "is already activated"
}
elseif( $_.Exception.Message.Contains("is not installed in this farm")){
write-host "Feature " $featureId "is not installed in this farm"
}
else{
write-host "An error ocurred while activating feature " $featureId ". Error detail: $($_)" -foreground "Red"
}
}
echo "Processing Content Types"
$avctypes = $rootSite.RootWeb.AvailableContentTypes
$Context.Load($avctypes)
$Context.ExecuteQuery()
$Name = "Post"
$Property = "Modified"
$Number = "14"
$Period = "days"
$contentType = $avctypes.Name -eq $Name
if($contentType){
[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CreatePolicy($contentType, $null);
$newPolicy = [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::GetPolicy($contentType);
$newPolicy.Items.Add("Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration",
"<Schedules nextStageId='2'>"+
"<Schedule type='Default'>"+
"<stages>"+
#"<data stageId='1' stageDeleted='true'></data>"+
"<data stageId='1'>"+
"<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>"+
"<number>"+$Number+"</number>"+"<property>"+$Property+"</property>"+
"<period>"+$Period+"</period>"+
"</formula>"+
"<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.Delete' />"+
"</data>"+
"</stages>"+
"</Schedule>"+
"</Schedules>");
$newPolicy.Update();
$contentType.Update($true)
}
}
#}
$web.Dispose()
$site.Dispose()
echo "Finished!!!"
Thanks for help with solving this issue.