Working with site policies in SharePoint 2013

Preview:

DESCRIPTION

Session slides from SharePoint and Project Conference Adriatics 2013. Similar to lists and content types in previous SharePoint versions, with SharePoint 2013 it is possible to set and enforce retention rules to entire SharePoint site collections. In this session we will describe what are site retention policies, what we need to set up in order to be able to define and apply site retention policies, and how to work with site retention from code.

Citation preview

SHAREPOINT AND PROJECT CONFERENCE ADRIATICS 2013

ZAGREB, NOVEMBER 27-28 2013

Working with site policies in SharePoint 2013

DRAGAN PANJKOV, PLANB.

B: WWW.DRAGAN-PANJKOV.COMT: @PANJKOV

sponsors

About• Dragan Panjkov• Working with SharePoint since 2007• www.dragan-panjkov.com• www.twitter.com/panjkov

• PlanB. d.o.o.• www.planb.ba

• SharePoint user group• www.1sug.com

Agenda• Information Management Policies• Site Policies• Configuration steps needed to enable Site Policies on

sites• Creating Site Policies in SharePoint UI• Working with Site Policies using Server OM• Working with Site Policies using Client OM

Information management policy?• An information management policy is a set of rules that govern the availability and behavior of a certain type of important content. Policy enables administrators to control and evaluate who can access information, how long to retain information, and how effectively people are complying with the policy.

In other words• Information Management Policy connects business need

and technical implementation of the solution to ensure that standards are met

BusinessOutcomes

TechnicalConsiderations

InformationClassification

Information Management

Policies

ServiceArchitecture

InformationArchitecture

Information Management

Standards

Service Management

Policies

FunctionalPreferences

InformationManagementArchitecture

FunctionalDesign

Project ConstraintsBudget, Timeframe, Resources

Information Management Policy Scopes• List• Library• Content Type• Site

Site Retention?• How do we control site lifecycle?• Are we able to track old and unused sites?• Are we able to easily delete old, unused, expired sites?

Site Policies• Opportunity to predefine retention rules for sites• Assign retention policy at site creation• Site Policies can be configured that sites are:• Closed and then deleted automatically• Deleted automatically after certain period of time• Marked as read-only

• Site Policies can be published in Content Type Hub

Enabling Site Policy functionalityIn UI:• Enable features in Site Collection• Library and Folder Based Retention• Site Policy• Hidden feature “Record Resources” activated automatically

In onet.xml<SiteFeatures> <Feature ID="5bccb9a4-b903-4fd1-8620-b795fa33c9ba" Name="RecordResources" /> <Feature ID="063c26fa-3ccc-4180-8a84-b6f98e991df3" Name="LocationBasedPolicy" /> <Feature ID="2fcd5f8a-26b7-4a6a-9755-918566dba90a" Name="ProjectBasedPolicy" /></SiteFeatures>

CREATING AND APPLYING SITE POLICIES IN UI

Creating Site Policies in UI

Creating Site Policies in UI

Creating Site Policies in UI

Creating Site Policies in UI

Creating Site Policies in UI

Applying Site Policies in the UI

Applying Site Policies in the UI

Applying Site Policies in the UI

Applying Site Policies in the UI

Site Policies in SSOM

namespace Microsoft.Office.RecordsManagement.InformationPolicy{ public class ProjectPolicy { public string Description { get; internal set; } public string EmailBody { get; set; } public string EmailBodyWithTeamMailbox { get; set; } public string EmailSubject { get; set; } public string Name { get; internal set; }

public static void ApplyProjectPolicy(SPWeb web, ProjectPolicy projectPolicy); public static void CloseProject(SPWeb web); public static bool DoesProjectHavePolicy(SPWeb web); public static ProjectPolicy GetCurrentlyAppliedProjectPolicyOnWeb(SPWeb web); public static DateTime GetProjectCloseDate(SPWeb web); public static DateTime GetProjectExpirationDate(SPWeb web); public static List<ProjectPolicy> GetProjectPolicies(SPWeb web); public static bool IsProjectClosed(SPWeb web); public static void OpenProject(SPWeb web); public static void PostponeProject(SPWeb web); public void SavePolicy(); }}

SERVER-SIDE OBJECT MODEL DEMOS

Create Site Policies

// ProjectPolicy Content Type IDSPContentTypeId policyCTID= new SPContentTypeId("0x010085EC78BE64F9478aAE3ED069093B9963");SPContentTypeCollection contentTypes = site.RootWeb.ContentTypes;

// ProjectPolicy is parent content type SPContentType parentContentType = contentTypes[policyCTID];

// we create new content type based on ProjectPolicypolicyContentType = new SPContentType(parentContentType, contentTypes, "New Project Policy");policyContentType = contentTypes.Add(policyContentType);policyContentType.Group = parentContentType.Group;policyContentType.Hidden = true;policyContentType.Update();

// Final step is to create new Policy with new content typePolicy.CreatePolicy(policyContentType, null);

Read Site Policies that exist on site private static void ReadProjectPolicies() { using (SPSite targetSite = new SPSite(siteUrl)) { SPWeb targetWeb = targetSite.RootWeb; List<ProjectPolicy> projectPolicies = ProjectPolicy.GetProjectPolicies(targetWeb);

if (projectPolicies!= null && projectPolicies.Count > 0) { Console.WriteLine("Project Policies on web {0}", siteUrl); foreach (var item in projectPolicies) { Console.WriteLine("Name: {0}",item.Name); Console.WriteLine("Desc: {0}",item.Description); Console.WriteLine(); }

} Console.WriteLine();

try { targetWeb.Dispose(); } catch { } } }

Get Applied Policy private static void GetAppliedPolicy() { using (SPSite targetSite = new SPSite(siteUrl)) { SPWeb targetWeb = targetSite.RootWeb; ProjectPolicy appliedPolicy = ProjectPolicy.GetCurrentlyAppliedProjectPolicyOnWeb(targetWeb);

if (appliedPolicy != null) { Console.WriteLine("Currently applied Project Policy on web {0}", siteUrl); Console.WriteLine(appliedPolicy.Name); Console.WriteLine(appliedPolicy.Description); } else { Console.WriteLine("Project Policy is not applied on web {0}", siteUrl); }

Console.WriteLine();

try { targetWeb.Dispose();

} catch { } } }

Is Policy Applied? private static void IsPolicyApplied() { using (SPSite targetSite = new SPSite(siteUrl)) { SPWeb targetWeb = targetSite.RootWeb; bool isPolicyApplied = ProjectPolicy.DoesProjectHavePolicy(targetWeb);

if (isPolicyApplied) { Console.WriteLine("Web has policy applied {0}", siteUrl); } else { Console.WriteLine("Project Policy is not applied on web {0}", siteUrl); }

Console.WriteLine(); try { targetWeb.Dispose();

} catch { } } }

Apply Policyprivate static void ApplyPolicySSOM() { string policyName = "DEVSITE DoNotClose-ReadOnly Policy";

using (SPSite targetSite = new SPSite(siteUrl)) { SPWeb targetWeb = targetSite.RootWeb; List<ProjectPolicy> webPolicies = ProjectPolicy.GetProjectPolicies(targetWeb); ProjectPolicy selectedPolicy = webPolicies.Where(p => p.Name == policyName).FirstOrDefault(); if (selectedPolicy != null) { ProjectPolicy.ApplyProjectPolicy(targetWeb, selectedPolicy); targetWeb.Update(); Console.WriteLine("Successfully applied policy '{0}' on web '{1}'", policyName, targetWeb); } } }

Get Project Close and Expiration Date private static void GetProjectCloseExpirationDate() { using (SPSite targetSite = new SPSite(siteUrl)) { SPWeb targetWeb = targetSite.RootWeb;

DateTime closedDate = ProjectPolicy.GetProjectCloseDate(targetWeb); DateTime expirationDate = ProjectPolicy.GetProjectExpirationDate(targetWeb);

if (closedDate != DateTime.MinValue) { Console.WriteLine("Close Date: {0}", closedDate); }

if (expirationDate != DateTime.MinValue) { Console.WriteLine("Expiration Date: {0}", expirationDate); }

Console.WriteLine();

try { targetWeb.Dispose(); } catch { } } }

Open Closed Site ELEVATED private static void OpenProject() { using (SPSite site = new SPSite(siteUrl)) { SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite targetSite = new SPSite(siteUrl)) { SPWeb targetWeb = targetSite.RootWeb; bool isClosed = ProjectPolicy.IsProjectClosed(targetWeb); Console.WriteLine("Site is Closed! {0}", siteUrl); if (isClosed) { Console.WriteLine("Trying to open site.");

ProjectPolicy.OpenProject(targetWeb); Console.WriteLine("Site is now Open! {0}", siteUrl); } Console.WriteLine();

try { targetWeb.Dispose(); } catch { } } }); } }

Site Policies in CSOMnamespace Microsoft.SharePoint.Client.InformationPolicy{ [ScriptType("SP.InformationPolicy.ProjectPolicy", ServerTypeId = "{ec5e0a70-0cc3-408f-a4dc-1bb3495aac75}")] public class ProjectPolicy : ClientObject { [EditorBrowsable(EditorBrowsableState.Never)] public ProjectPolicy(ClientRuntimeContext context, ObjectPath objectPath);

[Remote] public static void ApplyProjectPolicy(ClientRuntimeContext context, Web web, ProjectPolicy projectPolicy); [Remote] public static void CloseProject(ClientRuntimeContext context, Web web); [Remote] public static ClientResult<bool> DoesProjectHavePolicy(ClientRuntimeContext context, Web web); [Remote] public static ProjectPolicy GetCurrentlyAppliedProjectPolicyOnWeb(ClientRuntimeContext context, Web web); [Remote] public static ClientResult<DateTime> GetProjectCloseDate(ClientRuntimeContext context, Web web); [Remote] public static ClientResult<DateTime> GetProjectExpirationDate(ClientRuntimeContext context, Web web); [Remote] public static ClientObjectList<ProjectPolicy> GetProjectPolicies(ClientRuntimeContext context, Web web); protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader); [Remote] public static ClientResult<bool> IsProjectClosed(ClientRuntimeContext context, Web web); [Remote] public static void OpenProject(ClientRuntimeContext context, Web web); [Remote] public static void PostponeProject(ClientRuntimeContext context, Web web); [Remote] public void SavePolicy(); }}

CLIENT-SIDE OBJECT MODEL DEMOS

Read Site Policies that exist on siteprivate static void ReadProjectPoliciesCsom()

{ ClientContext policyContext = new ClientContext(siteUrl); Web targetWeb = policyContext.Web; ClientObjectList<ProjectPolicy> projectPolicies = ProjectPolicy.GetProjectPolicies(policyContext, targetWeb); policyContext.Load(projectPolicies); policyContext.ExecuteQuery();

if (projectPolicies != null && projectPolicies.Count > 0) { Console.WriteLine("Project Policies on web {0}", siteUrl); foreach (var item in projectPolicies) { Console.WriteLine("Name: {0}", item.Name); Console.WriteLine("Desc: {0}", item.Description); Console.WriteLine(); }

} Console.WriteLine(); }

Get Applied Policy private static void GetAppliedPolicyCsom() { ClientContext policyContext = new ClientContext(siteUrl); Web targetWeb = policyContext.Web; ClientResult<bool> isPolicyApplied = ProjectPolicy.DoesProjectHavePolicy(policyContext, targetWeb); policyContext.ExecuteQuery();

if (isPolicyApplied.Value) { ProjectPolicy appliedPolicy = ProjectPolicy.GetCurrentlyAppliedProjectPolicyOnWeb(policyContext, targetWeb); policyContext.Load(appliedPolicy); policyContext.ExecuteQuery();

if (appliedPolicy.TypedObject.ServerObjectIsNull != true) { Console.WriteLine("Currently applied Project Policy on web {0}", siteUrl); Console.WriteLine(appliedPolicy.Name); Console.WriteLine(appliedPolicy.Description); }

} else { Console.WriteLine("Project Policy is not applied on web {0}", siteUrl);

} Console.WriteLine(); }

Is Policy Applied? private static void IsPolicyAppliedCsom() { ClientContext policyContext = new ClientContext(siteUrl); Web targetWeb = policyContext.Web; ClientResult<bool> isPolicyApplied = ProjectPolicy.DoesProjectHavePolicy(policyContext, targetWeb); policyContext.ExecuteQuery();

if (isPolicyApplied.Value) { Console.WriteLine("Web has policy applied {0}", siteUrl); } else { Console.WriteLine("Project Policy is not applied on web {0}", siteUrl); }

Console.WriteLine(); }

Apply Policy private static void ApplyPolicyCsom() { string policyName = "DEVSITE DoNotClose-ReadOnly Policy";

ClientContext policyContext = new ClientContext(siteUrl);

Web targetWeb = policyContext.Web; ClientObjectCollection<ProjectPolicy> webPolicies = ProjectPolicy.GetProjectPolicies(policyContext, targetWeb); policyContext.Load(webPolicies); policyContext.ExecuteQuery();

ProjectPolicy selectedPolicy = webPolicies.Where(p => p.Name == policyName).FirstOrDefault(); if (selectedPolicy != null) { ProjectPolicy.ApplyProjectPolicy(policyContext, targetWeb, selectedPolicy); policyContext.ExecuteQuery();

Console.WriteLine("Successfully applied policy '{0}' on web '{1}'", policyName, siteUrl); }

Console.WriteLine();

}

Get Project Close and Expiration Date private static void GetProjectCloseExpirationDateCsom() { ClientContext policyContext = new ClientContext(siteUrl); Web targetWeb = policyContext.Web;

ClientResult<DateTime> closedDate = ProjectPolicy.GetProjectCloseDate(policyContext, targetWeb); ClientResult<DateTime> expirationDate = ProjectPolicy.GetProjectExpirationDate(policyContext, targetWeb); policyContext.ExecuteQuery();

if (closedDate.Value != DateTime.MinValue) { Console.WriteLine("Close Date: {0}", closedDate.Value); }

if (expirationDate.Value != DateTime.MinValue) { Console.WriteLine("Expiration Date: {0}", expirationDate.Value);

} Console.WriteLine(); }

Close Site private static void CloseProjectCsom() { ClientContext policyContext = new ClientContext(siteUrl); Web targetWeb = policyContext.Web;

ClientResult<bool> isClosed = ProjectPolicy.IsProjectClosed(policyContext, targetWeb); policyContext.ExecuteQuery();

if (!isClosed.Value) { ProjectPolicy.CloseProject(policyContext, targetWeb); } policyContext.ExecuteQuery();

isClosed = ProjectPolicy.IsProjectClosed(policyContext, targetWeb); policyContext.ExecuteQuery(); if (isClosed.Value) { Console.WriteLine("Project is closed! {0}", siteUrl); } else { Console.WriteLine("Project is NOT closed!"); }

Console.WriteLine();

}

Resources• http://

msdn.microsoft.com/en-us/library/microsoft.office.recordsmanagement.informationpolicy.projectpolicy_members.aspx• http://technet.microsoft.com/en-us/library/jj219569.aspx• http://

blog.dragan-panjkov.com/archive/2013/06/30/creating-site-policy-in-sharepoint-2013-using-server-code.aspx• http://

blog.dragan-panjkov.com/archive/2013/10/27/configuring-site-policy-in-sharepoint-2013-using-server-code.aspx• http://

blogs.technet.com/b/tothesharepoint/archive/2013/03/28/site-policy-in-sharepoint.aspx• http://

stevemannspath.blogspot.com/2012/08/sharepoint-2013-site-retention-getting.html• http://www.booden.net/ProjectPolicy.aspx

questions?

WWW.DRAGAN-PANJKOV.COM

@PANJKOV

thank you.

SHAREPOINT AND PROJECT CONFERENCE ADRIATICS 2013

ZAGREB, NOVEMBER 27-28 2013

Recommended