Monday, October 13, 2014

Applying site policy programmatically in SharePoint 2013

Introduction

We all know that about Site Closure and deletion. Click here to know some notes about it. Here, i am going to give an idea to apply this site policy to subsites programmatically. After setting Site Closure and Deletion follow the below instructions.

Site Policy

Refer  Microsoft.Office.RecordsManagement.InformationPolicy dll in your project and inherit this in the page

using Microsoft.Office.RecordsManagement.InformationPolicy;

var contextWeb = SPContext.Current.Web;
 contextWeb.AllowUnsafeUpdates = true;

ProjectPolicy groupPolicy = (from s in ProjectPolicy.GetProjectPolicies(contextWeb.Site.OpenWeb()) where s.Name == "Your Sit Policy Name" select s).FirstOrDefault();
                ProjectPolicy.ApplyProjectPolicy(contextWeb, groupPolicy);
contextWeb.Update();
contextWeb.AllowUnsafeUpdates = false;

That's all Site Policy will apply in your site. To ensure that, Goto Site Closure and Deletion in Site Settings and check applied Site Policy.

No comments:

Post a Comment