Introduction

​Applying Site retention policy pro-grammatically is quite important for developers to provide a common interface for the end user where they can manage site retention policy.
This article provide JSOM(Javascript Object Model ) based code sample which can be referred as it is to manage site retention policy
.

Scenarios & Where it can be applied

​The below process flow is a typical corporate scenario, where a custom grid can be developed displaying all sub sites under a site collection with their site policies. Site Collection admin or site owners can apply site policies through this interface. The Interface can be built in SharePoint hosted add-in or directly inside a page using content editor web part.

​For applying site policy or to check the site policy status, the sample code provided in the next section can be used.

1 & 2. Site Collection Admin\Site Owners checks the interface & view the site policy.
3. Site Collection Admin\Site Owners applies the site policy from the interface build using JSOM.
4. Site Retention policy check the closure date and deletes the site once the required condition is met.

Code Samples

​The core file to manage site retention policies is  SP.Policy.js and the namespace used for this purpose is SP.InformationPolicy.ProjectPolicy.
Before proceeding with any of the below activity, make sure that the SP.Policy.JS is loaded explicitly as it is not loaded by default in SharePoint


Note: The user should at least have full control over the site to successfully execute the script

Get list of Site Retention Policy


Explanation

Here $.getScript ensures that the script is loaded and then calls the function getAvailablePolicies. Inside getAvailablePolicies  function, getProjectPolicies method under SP.InformationPolicy.ProjectPolicy is called and required parameter context & targetWeb is passed. On success function, we received the list of all policy which are available on the current site.

Code

$(document).ready(function(){
    var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.Policy.js", getAvailablePolicies);
});
   
function getAvailablePolicies() {
    context = SP.ClientContext.get_current();
    targetWeb = context.get_web();
    allPolicies = SP.InformationPolicy.ProjectPolicy.getProjectPolicies(context, targetWeb);
    context.load(allPolicies);
    context.executeQueryAsync(function () {
        policyEnumerator = allPolicies.getEnumerator();
        while (policyEnumerator.moveNext()) {
            _policy = policyEnumerator.get_current();
            console.log(_policy.get_name());
      }
    }, function (sender, args) {
        alert(args.get_message() + '\n' + args.get_stackTrace());
    });
}

Apply Site Policy

The below code sample applies the site policy by name(here the name of the policy is "Site Retention Policy")

Explanation


Inside applySitePolicy function, getProjectPolicies method under SP.InformationPolicy.ProjectPolicy is called and required parameter context & targetWeb is passed. On success function, we look for the policy with the specified name and apply it to the site by calling applyProjectPolicy Method. Once the code has been run, the activity can be checked by navigating to the target site's Site Settings -> Site Closure and Deletion (under Site Administration). The site policy name should appear in the drop down.


Code


$(document).ready(function(){
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Policy.js", applySitePolicy);
});
function applySitePolicy() {
    context = SP.ClientContext.get_current();
    targetWeb = context.get_web();
    policies = SP.InformationPolicy.ProjectPolicy.getProjectPolicies(context, targetWeb);
    context.load(policies);
    context.executeQueryAsync(function () {
        policyEnumerator = policies.getEnumerator();
        while (policyEnumerator.moveNext()) {
            _policy = policyEnumerator.get_current();
            if (_policy.get_name() == "Site Retention Policy") {
                SP.InformationPolicy.ProjectPolicy.applyProjectPolicy(context, targetWeb, _policy);
                context.executeQueryAsync(function () {
                    alert('policy applied to the site successfully');
                    }, function (sender,args) {
                    alert(args.get_message() + '\n' + args.get_stackTrace());
                });
                }
             else
                {
                console.log("policy not found");
                }
              }
    }, function (sender, args) {
        alert(args.get_message() + '\n' + args.get_stackTrace());
    });
}

Close Site

The below code sample applies the site policy by name(here the name of the policy is "Site Retention Policy") and close the site.

Explanation

Inside applyncloseProjectPolicy function, getProjectPolicies method under SP.InformationPolicy.ProjectPolicy is called and required parameter context & targetWeb is passed. On success function, we look for the policy with the specified name and apply it to the site by calling applyProjectPolicy Method. On successfully applying the site policy and the site is close by calling the method closeProject. Once the code has been run, the activity can be checked by navigating to the target site's Site Settings -> Site Closure and Deletion (under Site Administration). The site policy name should appear in the drop down and a button to reopen the site will also appear.


Code


$(document).ready(function(){ 
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Policy.js", applyncloseProjectPolicy);
});
  
function applyncloseProjectPolicy() {
    context = SP.ClientContext.get_current();
    targetWeb = context.get_web();
    policies = SP.InformationPolicy.ProjectPolicy.getProjectPolicies(context, targetWeb);
    context.load(policies);
    context.executeQueryAsync(function () {
        policyEnumerator = policies.getEnumerator();
        while (policyEnumerator.moveNext()) {
            _policy = policyEnumerator.get_current();
            if (_policy.get_name() == "Site Retention Policy") {
                SP.InformationPolicy.ProjectPolicy.applyProjectPolicy(context, targetWeb, _policy);
                context.executeQueryAsync(function () {
                    alert('policy applied');
                    SP.InformationPolicy.ProjectPolicy.closeProject(context, targetWeb);
                    context.executeQueryAsync(function () {
                    alert('Site Closure Applied');
                }, function (sender,args) {
                    alert(args.get_message() + '\n' + args.get_stackTrace());
                });
                }, function (sender,args) {
                    alert(args.get_message() + '\n' + args.get_stackTrace());
                });
            }
        }
    }, function (sender, args) {
        alert(args.get_message() + '\n' + args.get_stackTrace());
    });
}

Open Site

The below code sample opens the closed site.

Explanation


Inside openSitePolicy function, getProjectPolicies method under SP.InformationPolicy.ProjectPolicy is called and required parameter context & targetWeb is passed. On success function, we look for the policy with the specified name and apply it to the site by calling applyProjectPolicy Method. On successfully applying the site policy and the site is close by calling the method closeProject. Once the code has been run, the activity can be checked by navigating to the target site's Site Settings -> Site Closure and Deletion (under Site Administration). The Close this site now button should re-appear.



Code


$(document).ready(function(){ 
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Policy.js", openSitePolicy);
});
function openSitePolicy() {
    context = SP.ClientContext.get_current();
    targetWeb = context.get_web();
    SP.InformationPolicy.ProjectPolicy.openProject(context, targetWeb);
    context.executeQueryAsync(function () {              
                context.executeQueryAsync(function () {
                    alert('site is now open');
                    }, function (sender,args) {
                    alert(args.get_message() + '\n' + args.get_stackTrace());
                });
                }, function (sender, args) {
        alert(args.get_message() + '\n' + args.get_stackTrace());
    });
}

Additional methods available

Below are the methods that are available under SP.InformationPolicy.ProjectPolicy

DoesProjectHavePolicy: This method return true if the site policy is applied on this site.

GetCurrentlyAppliedProjectPolicyOnWeb: This method returns the policy applied to the site.

GetProjectCloseDate: This method returns the date when the site was closed. 

GetProjectExpirationDate: This method returns the date when the site was deleted.

IsProjectClosed: This method returns true if the site is closed.

Conclusion

Hope this article helps as there are very limited documentation on managing site policies using javascript object model. 

Reference

http://sharepointfieldnotes.blogspot.in/2015/05/get-handle-on-your-site-closure-and.html