CSOM vs Policy vs SharePoint Online

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.

August 16th, 2015 3:59pm

Hi,

We can't use the Policy.CreatePolicy method in Client-side, the syntax as below:

public static void CreatePolicy(
	SPContentType ct,
	Policy globalPolicy
)
ct
     Type:Microsoft.SharePoint.SPContentType
     The SPContentType object in which to create the information policy.
globalPolicy
      Type: Microsoft.Office.RecordsManagement.InformationPolicy.Policy
      The site information policy to copy into the specified content type.

I find the ProjectPolicy class in Microsoft.SharePoint.Client.InformationPolicy namespace, the following article for your reference:

.NET client API reference for SharePoint 2013

https://msdn.microsoft.com/en-us/library/office/jj193041.aspx

Best Regards,

Dennis

Free Windows Admin Tool Kit Click here and download it now
August 17th, 2015 9:51pm

Hi,

I have two questions:

1. Why we can't use Policy.CreatePolicy method in Client-side

2. Do U know how to implement it to my above code

Thx, Michal

August 18th, 2015 3:45pm

Hi Michal,

Please see the SPContentType object(Server Object Model), so we can't use in client-side.

Best Regards,

Dennis

Free Windows Admin Tool Kit Click here and download it now
August 18th, 2015 8:58pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics