static void Main(string[] args) { string json = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sposettings.json"); SPOSettings setting = SPOSettings.GetSettings(json); using (ClientContext context = GetClientContext(setting)) { Web web = context.Web; var sitePolicies = ProjectPolicy.GetProjectPolicies(context, web); context.Load(sitePolicies); context.ExecuteQuery(); if (sitePolicies != null && sitePolicies.Count > 0) { var policy = sitePolicies.FirstOrDefault(p => p.Name == setting.PolicyName); if (policy != null) { ProjectPolicy.ApplyProjectPolicy(context, web, policy); context.ExecuteQuery(); ProjectPolicy.CloseProject(context, web); context.ExecuteQuery(); } } } }
/// <summary> /// Close a site, if it has a site policy applied and is currently not closed /// </summary> /// <param name="web"></param> /// <returns>True if site was closed, false otherwise</returns> public static bool SetClosedBySitePolicy(this Web web) { if (web.HasSitePolicyApplied() && !IsClosedBySitePolicy(web)) { ProjectPolicy.CloseProject(web.Context, web); web.Context.ExecuteQueryRetry(); return(true); } return(false); }
/// <summary> /// Close a site, if it has a site policy applied and is currently not closed /// </summary> /// <param name="web"></param> /// <returns>True if site was closed, false otherwise</returns> private static async Task <bool> SetClosedBySitePolicyImplementation(this Web web) { if (await web.HasSitePolicyAppliedImplementation() && !await web.IsClosedBySitePolicyImplementation()) { ProjectPolicy.CloseProject(web.Context, web); await web.Context.ExecuteQueryRetryAsync(); return(true); } return(false); }
private static async Task <bool> SetClosedBySitePolicyImplementation(this Web web) #endif { #if ONPREMISES if (web.HasSitePolicyAppliedImplementation() && !IsClosedBySitePolicyImplementation(web)) #else if (await web.HasSitePolicyAppliedImplementation() && !await web.IsClosedBySitePolicyImplementation()) #endif { ProjectPolicy.CloseProject(web.Context, web); #if ONPREMISES web.Context.ExecuteQueryRetry(); #else await web.Context.ExecuteQueryRetryAsync(); #endif return(true); } return(false); }