对CSOM(Not SSOM) Site Policy & Expiration Email的API的调用过程进行封装,做成tool,界面如下:
代码如下:
using System; using System.Windows.Forms; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.InformationPolicy; namespace CSOMForm { public partial class MainForm : System.Windows.Forms.Form { public MainForm() { InitializeComponent(); } private void start_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); //Get all the site policies and add them into the combox. var projectPolicies = GetAllSitePolicies(context, web); try { sitePolicies_comboBox.Items.Clear(); foreach (ProjectPolicy projectPolicy in projectPolicies) { sitePolicies_comboBox.Items.Add(projectPolicy.Name); } } catch (Exception ex) { returnDisplay_richTextBox.AppendText(ex.ToString() + System.Environment.NewLine); } } } //Get all site policies from the server. private ClientObjectList<ProjectPolicy> GetAllSitePolicies(ClientContext context, Web web) { //API test GetProjectPolicies. var projectPolicies = ProjectPolicy.GetProjectPolicies(context, web); context.Load(projectPolicies); context.ExecuteQuery(); return projectPolicies; } //The method to get the current applied site policy from the server. private ProjectPolicy GetCurrentAppliedSitePolicy(ClientContext context, Web web) { //API test GetCurrentlyAppliedProjectPolicyOnWeb. 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(); return policy; } //Apply the policy selected to the site. private void ApplyProjectPolicy_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); try { var policies = GetAllSitePolicies(context, web); ProjectPolicy selectedPolicy = null; foreach (ProjectPolicy policy in policies) { if (policy.Name == sitePolicies_comboBox.Text) { selectedPolicy = policy; } } //API test ApplyProjectPolicy. ProjectPolicy.ApplyProjectPolicy(context, web, selectedPolicy); returnDisplay_richTextBox.Text = "The policy we selected has been applied to the site. The current policy applied is:" + System.Environment.NewLine; //API test GetCurrentAppliedSitePolicy. ProjectPolicy AppliedPolicy = GetCurrentAppliedSitePolicy(context, web); context.Load(AppliedPolicy); context.ExecuteQuery(); //API test Name. returnDisplay_richTextBox.AppendText(AppliedPolicy.Name.ToString() + System.Environment.NewLine); //API test Description. returnDisplay_richTextBox.AppendText(AppliedPolicy.Description.ToString() + System.Environment.NewLine); } catch (Exception ex) { returnDisplay_richTextBox.Text = "You may not have a project policy yet. Please check it." + System.Environment.NewLine; returnDisplay_richTextBox.AppendText(ex.ToString()); } } } /* Close the site. The site must have a policy applied and the site is open. */ private void closeProject_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); //Close the site. try { //API test CloseProject. ProjectPolicy.CloseProject(context, web); //API test IsProjectClosed. returnDisplay_richTextBox.Text = "The site‘s project is now closed: "; var ifClosed = ProjectPolicy.IsProjectClosed(context, web); context.ExecuteQuery(); returnDisplay_richTextBox.AppendText(ifClosed.Value.ToString() + System.Environment.NewLine); } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } private void doesProjectHavePolicy_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); try { //API test DoesProjectHavePolicy. ClientResult<bool> result = ProjectPolicy.DoesProjectHavePolicy(context, web); context.ExecuteQuery(); returnDisplay_richTextBox.Text = "DoesProjectHavePolicy: "; returnDisplay_richTextBox.AppendText(result.Value.ToString()); } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } private void openProject_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); //Open the site. try { //API test OpenProject. ProjectPolicy.OpenProject(context, web); //API test IsProjectClosed. returnDisplay_richTextBox.Text = "The site‘s project is now closed: "; var ifOpen = ProjectPolicy.IsProjectClosed(context, web); context.ExecuteQuery(); returnDisplay_richTextBox.AppendText(ifOpen.Value.ToString() + System.Environment.NewLine); } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } private void getProjectCloseDate_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); try { //API test GetProjectCloseDate. returnDisplay_richTextBox.Text = "The project‘s close date is: "; var projectCloseDate = ProjectPolicy.GetProjectCloseDate(context, web); context.ExecuteQuery(); returnDisplay_richTextBox.AppendText(projectCloseDate.Value.ToString() + System.Environment.NewLine); } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } private void getProjectExpirationDate_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); try { //API test GetProjectExpirationDate. returnDisplay_richTextBox.Text = "The project‘s expiration date is: "; var projectExpireDate = ProjectPolicy.GetProjectExpirationDate(context, web); context.ExecuteQuery(); returnDisplay_richTextBox.AppendText(projectExpireDate.Value.ToString() + System.Environment.NewLine); } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } private void postponeProject_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); try { //API test PostponeProject. ProjectPolicy.PostponeProject(context, web); context.ExecuteQuery(); returnDisplay_richTextBox.Text = "The project has been postponed."; } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } /* You should select a policy first. */ private void setEmailSubject_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); try { //API test set EmailSubject. var policies = GetAllSitePolicies(context, web); ProjectPolicy selectedPolicy = null; foreach (ProjectPolicy policy in policies) { if (policy.Name == sitePolicies_comboBox.Text) { selectedPolicy = policy; } } selectedPolicy.EmailSubject = emailSubject_textBox.Text; selectedPolicy.SavePolicy(); context.ExecuteQuery(); context.Load(selectedPolicy,p => p.EmailSubject); context.ExecuteQuery(); returnDisplay_richTextBox.Text = "The selected project‘s EmailSubject is now: " + selectedPolicy.EmailSubject.ToString(); } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } /* You should select a policy first. */ private void setEmailBody_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); try { //API test set EmailBody. var policies = GetAllSitePolicies(context, web); ProjectPolicy selectedPolicy = null; foreach (ProjectPolicy policy in policies) { if (policy.Name == sitePolicies_comboBox.Text) { selectedPolicy = policy; } } selectedPolicy.EmailBody = emailBody_richTextBox.Text; selectedPolicy.SavePolicy(); context.ExecuteQuery(); context.Load(selectedPolicy, p => p.EmailBody); context.ExecuteQuery(); returnDisplay_richTextBox.Text = "The selected project‘s EmailBody is now: " + selectedPolicy.EmailBody.ToString(); } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } /* You should select a policy first. */ private void setEmailBodyWithTeamMailbox_button_Click(object sender, EventArgs e) { using (ClientContext context = new ClientContext(scUrl_textBox.Text)) { Site site = context.Site; Web web = site.OpenWeb(relative_siteUrl_textBox.Text); try { //API test set EmailBodyWithTeamMailbox. var policies = GetAllSitePolicies(context, web); ProjectPolicy selectedPolicy = null; foreach (ProjectPolicy policy in policies) { if (policy.Name == sitePolicies_comboBox.Text) { selectedPolicy = policy; } } selectedPolicy.EmailBodyWithTeamMailbox = emailBodyWithTeamMailbox_richTextBox.Text; selectedPolicy.SavePolicy(); context.ExecuteQuery(); context.Load(selectedPolicy, p => p.EmailBodyWithTeamMailbox); context.ExecuteQuery(); returnDisplay_richTextBox.Text = "The selected project‘s EmailBodyWithTeamMailbox is now: " + selectedPolicy.EmailBodyWithTeamMailbox.ToString(); } catch (Exception ex) { returnDisplay_richTextBox.Text = ex.ToString() + System.Environment.NewLine; } } } } }
时间: 2024-10-12 03:04:13