Add new Site Policy in SharePoint Online by code

%3CLINGO-SUB%20id%3D%22lingo-sub-55839%22%20slang%3D%22en-US%22%3EAdd%20new%20Site%20Policy%20in%20SharePoint%20Online%20by%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55839%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20there%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20looking%20for%20a%20way%20to%20create%20a%20Site%20Policy%20in%20a%20SharePoint%20Online%20site%20collection%20by%20code%20(C%23%20or%20PowerShell).%20I%20am%20wondering%20that%20for%20assigning%20a%20Site%20Policy%20to%20a%20site%20classes%20and%20methods%20are%20available%20in%20the%20CSOM%2C%20but%20there%20is%20nothing%20for%20creating%20the%20policies.%20And%20when%20there%20is%20anything%2C%20it's%20in%20a%20hidden%20place%2C%20where%20nobody%20can%20find%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20also%20tried%20to%20create%20the%20content%20type%20for%20the%20Site%20Policy%20by%20code%20to%20have%20a%20workaround%2C%20but%20the%20content%20type%20needs%20additional%20information%20in%20its%20XmlDocument%20member%2C%20but%20via%20Rest%20it%20is%20not%20possible%20to%20save%20this%20data.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20reason%20why%20I%20need%20the%20Site%20Policy%20is%20the%20site%20lifecycle%2C%20we%20need%20to%20implement.%20After%20a%20specific%20amount%20of%20time%20the%20site%20collection%20should%20be%20set%20into%20a%20read-only%20mode.%20The%20only%20way%20I%20see%2C%20is%20the%20Site%20Policy%20with%20its%20%22The%20site%20collection%20will%20be%20read%20only%20when%20it%20is%20closed%22%20setting.%20If%20there%20is%20any%20other%20way%20(no%2C%20removing%20the%20permissions%20is%20not%20a%20solution)%2C%20then%20I%20maybe%20do%20not%20need%20the%20Site%20Policy.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20hints%20are%20welcome.%20Maybe%20something%20for%20the%20PnP%20team%20(and%20they%20do%20a%20really%20good%20work!).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EOlaf%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-55839%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-56189%22%20slang%3D%22en-US%22%3ERe%3A%20Add%20new%20Site%20Policy%20in%20SharePoint%20Online%20by%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-56189%22%20slang%3D%22en-US%22%3E%3CP%3EOk%2C%20not%20what%20I%20really%20wanted%2C%20but%20an%20approach%20to%20create%20a%20site%20policy%20automatically%2C%20when%20a%20site%20collection%20is%20created.%20I%20don't%20know%20why%2C%20but%20without%20the%20modifications%20on%20the%20content%20type%20for%20the%20site%20policy%2C%20the%20content%20type%20hub%20won't%20publish%20the%20site%20policy%20content%20type%20to%20other%20site%20collections.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Folafd.wordpress.com%2F2017%2F03%2F23%2Fapply-site-policy-automatically-on-site-creation-in-sharepoint-online%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Folafd.wordpress.com%2F2017%2F03%2F23%2Fapply-site-policy-automatically-on-site-creation-in-sharepoint-online%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMaybe%20there%20is%20anyone%20from%20the%20SharePoint%20product%20group%2C%20who%20can%20tell%20me%2C%20whether%20the%20approach%20is%20a%20no-go%20or%20not.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EOlaf%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hi there,

 

I am looking for a way to create a Site Policy in a SharePoint Online site collection by code (C# or PowerShell). I am wondering that for assigning a Site Policy to a site classes and methods are available in the CSOM, but there is nothing for creating the policies. And when there is anything, it's in a hidden place, where nobody can find it.

 

I have also tried to create the content type for the Site Policy by code to have a workaround, but the content type needs additional information in its XmlDocument member, but via Rest it is not possible to save this data.

 

The reason why I need the Site Policy is the site lifecycle, we need to implement. After a specific amount of time the site collection should be set into a read-only mode. The only way I see, is the Site Policy with its "The site collection will be read only when it is closed" setting. If there is any other way (no, removing the permissions is not a solution), then I maybe do not need the Site Policy.

 

Any hints are welcome. Maybe something for the PnP team (and they do a really good work!).

 

Thanks

Olaf

 

1 Reply
Highlighted

Ok, not what I really wanted, but an approach to create a site policy automatically, when a site collection is created. I don't know why, but without the modifications on the content type for the site policy, the content type hub won't publish the site policy content type to other site collections.

 

https://olafd.wordpress.com/2017/03/23/apply-site-policy-automatically-on-site-creation-in-sharepoin...

 

Maybe there is anyone from the SharePoint product group, who can tell me, whether the approach is a no-go or not.

 

Thanks,

Olaf