Site Policy in SharePoint

If you're using a SharePoint product then you've probably got some sites to manage — sites you've created, sites your users have created, and sites other administrators have created. Maybe some of these sites are tracking small projects that will outlive their usefulness in a short amount of time. There might even be more than a few sites that no one uses anymore, and those sites may have content or even site mailboxes that are just taking up space.

Wouldn't it be great if there was a way to apply retention policies to sites in the same way you do with items in your sites? We've got just the thing — Site Policy for SharePoint Server.

 

Site Policies allow you to create a retention policy that can be applied to a site. Site Policy includes the following options:

  • Site Closure: The date the site is put into a "Closed" state. Closed does not stop users and admins from continuing to use the site and its content, but is just meant to denote that the site is not being actively used any longer and will be moving to the next site lifecycle stage. Sites can be manually closed. Site owners can also manually reopen closed sites (Note: only site collection administrators can reopen closed site collections.)
  • Site Deletion: The period of time following site creation or site closure that the site should be permanently deleted. When a site is deleted all its content and sub-webs are also deleted.
  • Postponement: This option will allow a site owner to manually postpone site deletion for a period of time determined by the Site Policy.
  • E-mail Notification: Before a site is deleted, an e-mail will be sent to site owners alerting them about the pending site deletion. Site Policy determines the period of time before scheduled site deletion that the e-mail is sent as well as recurrence frequency and timing of the e-mail alert.

 

  • Read-Only Mode: This setting puts the site collection and its subwebs into a read-only mode upon closure. Users can contact their site collection administrators to manually reopen the read-only site collection if need be.

 
If you just want to create a policy for a short-term project (to ensure it is automatically cleaned up when it is not needed), you can create a Site Policy that automatically deletes a site six months after it is created and sends a recurring warning e-mail to the site owners starting three months from the delete date. After that date, the site (and its associated Site Mailbox if there is one) will be deleted automatically by the Expiration Policy timer job unless a site owner manually postpones deletion.

Perhaps you want to put a site collection in read-only mode after a certain time. You can create a Site Policy, for example, that closes a site collection five years after its creation. Upon closure, the site collection can be set to read-only mode. One year following site closure, the entire site collection will be deleted.
 
Once you have created your Site Policy, you can directly apply it to sites by publishing Site Policies from a content type hub to use in all the sites in your farm, or even apply them during site creation time with self-service site creation.

To publish the Site Policies you must first set up Content Type Syndication on a Content Type Hub. Once that is complete, create your Site Policies on the Content Type Hub Site. To the right of your Site Policy list, you’ll see a new Publish Policy column with a link to “Manage publishing for this policy”. Clicking the link will bring you to the publishing page where you can publish (push the Site Policy to all consuming site collections in a read-only state), re-publish (push down changes to previously published Site Policies), or un-publish (leave the published policy on the consuming site collections in an editable state).

 
If you want users to be able to create their own sites and choose a Site Policy to be applied at creation time, start by setting up Self-Service Site Creation. Once that is done, you can use the Self-Service Site Creation administration page in Central Administration (or Tenant Admin in SharePoint Online under Settings) to choose one of three states for Site Policy: Hidden from users (no Site Policy will be applied), An optional choice (users may apply a Site Policy or none at all), or A required choice (user must choose a Site Policy before the site will be created).


 
After completing this configuration, users will be presented with a new dialog box on their Self-Service Creation dialog with the published Site Policies available for the site.

If a site is about to be deleted or if the site collection is in read-only mode, an information bar will appear at the top of the site for all users. This way, users can be made aware and have a chance to contact their site administrator in the event they believe the site should not be deleted, be reopened, and so on.


 
Site Policy also ensures that sites with associated Site Mailboxes are also kept in the same state as the site itself (Closed, Open, or Deleted). Whenever the state of the site changes, a work item is sent to the Site Policy and Exchange Site Mailbox Policy Update Timer Job. By default, this Timer Job will run once per day and update the state of the associated Site Mailbox.
 
There is CSOM (Client Side Object Model) code available to remotely determine the status of the site and change its state. You can find out if the site is closed, apply a Site Policy, manually open the site, and much more. You can also use the Site Policy CSOM to customize the subject and content of the site deletion alert e-mail by inserting the tokens for the site url ("<!--{SiteUrl}-->"), site expiration date ("<!--{SiteDeleteDate}-->"), and (if you have an associated Site Mailbox) the Site Mailbox ("<!--{TeamMailboxID}-->") into the body of the custom e-mail.

Note: To be able to compile and execute the below example on a client machine you will need to install the SharePoint 2013 Client SDK. For more information about the Site Policy class and methods, please see the ProjectPolicy MSDN article.

Example:

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.InformationPolicy;
using System;

namespace SitePolicyEmailChanger
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteCollectionUrl = "";
            string relativeSiteUrl = "";

            // Get the site and web info
            Console.WriteLine("Site Policy E-mail Changer");
            Console.WriteLine("Enter the Site Collection URL: ");
            siteCollectionUrl = Console.ReadLine();
            Console.WriteLine("Enter the relative Site URL: ");
            relativeSiteUrl = Console.ReadLine();

            // Return the currently applied Site Policy
            ClientContext context = new ClientContext(siteCollectionUrl);
            Site site = context.Site;
            Web web = site.OpenWeb(relativeSiteUrl);
            ProjectPolicy policy = ProjectPolicy.GetCurrentlyAppliedProjectPolicyOnWeb(context, web);
            context.Load(policy,
                         p => p.Name,
                         p => p.Description,
                         p => p.EmailSubject,
                         p => p.EmailBody,
                         p => p.EmailBodyWithTeamMailbox);
            context.ExecuteQuery();

            // Display the current Site Policy properties and pause
            Console.WriteLine(String.Format("Policy Name is: {0}", policy.Name));
            Console.WriteLine(String.Format("Policy Description is: {0}", policy.Description));
            Console.WriteLine(String.Format("Policy E-mail Subject is: {0}", policy.EmailSubject));
            Console.WriteLine(String.Format("Policy E-mail Body is: {0}", policy.EmailBody));
            Console.WriteLine(String.Format("Policy E-mail Body (with Site Mailbox) is: {0}", policy.EmailBodyWithTeamMailbox));
            Console.WriteLine();
            Console.ReadLine();

            // Edit the Site Policy E-mail properties
            policy.EmailSubject = "Contoso Site Deletion Notice";
            policy.EmailBody = "The Contoso site <!--{SiteUrl}--> is set to expire on <!--{SiteDeleteDate}-->. If you have any questions or concerns, please contact your admin.";
            policy.EmailBodyWithTeamMailbox = "The Contoso site <!--{SiteUrl}--> associated with Site Mailbox <!--{TeamMailboxID}--> is set to expire on <!--{SiteDeleteDate}-->. If you have any questions or concerns, please contact your admin.";
            policy.SavePolicy();
            context.ExecuteQuery();

            // Refetch the edited Site Policy from the server
            policy = ProjectPolicy.GetCurrentlyAppliedProjectPolicyOnWeb(context, web);
            context.Load(policy,
                         p => p.Name,
                         p => p.Description,
                         p => p.EmailSubject,
                         p => p.EmailBody,
                         p => p.EmailBodyWithTeamMailbox);
            context.ExecuteQuery();

            // Display the new Site Policy properties and pause
            Console.WriteLine(String.Format("Policy Name is: {0}", policy.Name));
            Console.WriteLine(String.Format("Policy Description is: {0}", policy.Description));
            Console.WriteLine(String.Format("Policy E-mail Subject is NOW: {0}", policy.EmailSubject));
            Console.WriteLine(String.Format("Policy E-mail Body is NOW : {0}", policy.EmailBody));
            Console.WriteLine(String.Format("Policy E-mail Body (with Site Mailbox) is NOW: {0}", policy.EmailBodyWithTeamMailbox));
            Console.ReadLine();
        }
    }
}