Search This Blog

Monday, May 30, 2016

How to filter the content type which has Information Management Policy Settings in SharePoint online

All the Information management policy settings information can be obtained from the SchemaXML of the content type.

Below XML is an extract from the schema XML which contains details about the policies. Here I do have a retention policy attached to my content type.

<XmlDocument NamespaceURI="office.server.policy">
- <p:Policy xmlns:p="office.server.policy" id="" local="true">
  <p:Name>DemoContentType</p:Name>
  <p:Description />
  <p:Statement />
- <p:PolicyItems>
- <p:PolicyItem featureId="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration" staticId="0x0101007F2D345A54104E038DF20E887A9D9E57|-225302813" UniqueId="a4a001ae-0512-40f4-b6dc-36a3861a6726">
  <p:Name>Retention</p:Name>
  <p:Description>Automatic scheduling of content for processing, and performing a retention action on content that has reached its due date.</p:Description>
- <p:CustomData>
- <Schedules nextStageId="2">
- <Schedule type="Default">
- <stages>
- <data stageId="1">
- <formula id="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn">
  <number>60</number>
  <property>DemoContentType</property>
  <propertyId>c8f6e1c4-183f-4bf6-8756-8428c39aab33</propertyId>
  <period>days</period>
  </formula>
  <action type="action" id="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin" />
  </data>
  </stages>
  </Schedule>
  </Schedules>
  </p:CustomData>
  </p:PolicyItem>
  </p:PolicyItems>
  </p:Policy>
  </XmlDocument>

Simple code snippet to traverse all the content types and get the content types which has a policy attached to it in SharePoint online using client object model.

string url = "siteURL";
            ClientContext ctx = new ClientContext(url);
            ctx.Credentials = new SharePointOnlineCredentials("email id", new NetworkCredential("email id", "XXXX").SecurePassword);

            ContentTypeCollection contentTypeCollection = ctx.Web.ContentTypes;

            ctx.Load(contentTypeCollection, ct => ct.Include(val => val.Name, val => val.Hidden, val => val.Id, val => val.SchemaXml));
            ctx.ExecuteQuery();

            foreach (ContentType contentType in contentTypeCollection)
            {
                if (!contentType.Hidden)
                {
                    XDocument xdoc = XDocument.Parse(contentType.SchemaXml);
                    IEnumerable<XElement> elments = xdoc.Descendants().Where(elm => elm.Name.NamespaceName == "office.server.policy");

                    if (elments.Count() > 0)
                    {
                        Console.WriteLine("\"" + contentType.Name + "\"," + contentType.Id);
                    }
                }
            }