SharePoint API测试系列——Manage SharePoint Site Policy & Expiration Email with CSOM API

对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

SharePoint API测试系列——Manage SharePoint Site Policy & Expiration Email with CSOM API的相关文章

SharePoint API测试系列——Records.BypassLocks测试

对于SharePoint中已经是Record的Item,我们想要修改他的属性,这在UI界面是无法完成的: 这时需要通过Records.BypassLocks API来完成.设计一个tool,利用Records.BypassLocks API来修改Recorded Items的属性(这里拿Title举例),界面如下: 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using Sy

SharePoint API测试系列——对Recorded Item进行OM Methods操作(委托的妙用)

对Recorded Item动态调用OM Methods进行测试,界面如下: 输入Site的URL,File在SharePoint中的相对URL,并选择OM操作类型,点击Test进行测试.代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.

ASP.NET Web Api 实践系列(二)Get/Post方式调用Web Api

本文给出Get/Post方式访问Web Api的帮助方法,对于Put/Delete方式的调用跟Post调用类似. 一.Web Api调用帮助类 下面给出Web Api调用帮助类的代码: 1 using System; 2 using System.Collections.Generic; 3 using System.Net.Http; 4 using System.Net.Http.Headers; 5 using System.Text; 6 using System.Web; 7 8 na

初学者的API测试技巧

API(应用程序编程接口)测试是一种直接在API级别执行验证的软件测试.它是集成测试的一部分,它确认API是否满足测试人员对功能.可靠性.性能和安全性的期望.与UI测试不同,API测试是在没有GUI层执行操作的. API测试技巧 Web API有两大类Web服务:SOAP和REST.SOAP(简单对象访问协议)是W3C标准定义的一种标准协议,用于发送和接收Web服务请求和响应.REST(表示状态传输)是使用HTTP的基于Web标准的体系结构.与基于SOAP的Web服务不同,没有针对RESTful

初学者的Api测试技巧!值得收藏!

初学者的Api测试技巧!API测试是一种直接在API级别执行验证的软件测试.它是集成测试的一部分,它确认API是否满足测试人员对功能.可靠性.性能和安全性的期望.与UI测试不同,API测试是在没有GUI层执行操作的.API测试技巧Web API有两大类Web服务:SOAP和REST.SOAP(简单对象访问协议)是W3C标准定义的一种标准协议,用于发送和接收Web服务请求和响应.REST(表示状态传输)是使用HTTP的基于Web标准的体系结构.与基于SOAP的Web服务不同,没有针对RESTful

sharepoint 2016 学习系列篇(15)-自定义列表应用篇-(4)数据权限配置

当数据已经录入到列表中之后,接下来,朋友们可能会想知道,有些数据,只想给某些用户看到,或者编辑,列表是否支持这样的操作. 大微软的sharepoint平台,对于用户的需求,可以说是考虑得很周全的,权限管理,当然也是不可能漏掉的,而且权限管理,还是sharepoint上一个非常突出的亮点,数据安全性管理的颗粒度,很完善.接下来,我们来看下,如何在sharepoint的自定义列表中,给数据配置不同的用户访问权限,这些当然也是不需要开发,就能实现的. 前面讲到,用户访问sharepoint平台网站的时

SharePoint 2013 Designer系列之自定义列表表单

在SharePoint的使用中,默认的样式过于单调经常困扰着我们,其实,SharePoint使用Designer工具,可以很轻松解决这一问题,制作出各式各样漂亮的页面.下面,让我们简单介绍下这一过程. 1.首先创建一个测试列表,里面的字段如下: 2.看一眼默认的Dispform.aspx页面,对于新闻来说,基本属于不能看: 3.使用SharePoint Designer 2013打开列表,找到Dispform.aspx页面,右键在高级模式下编辑该文件,如下图: 4.一般都是隐藏默认的td,然后新

SharePoint 2013 Designer系列之数据视图筛选

在SharePoint中,我们经常需要对列表进行简单的筛选,这时,数据视图就有作用了,我们可以定制对于字段的筛选,来进行展示:特别的,筛选不同于搜索,并没有对于附件或者文档的全文检索,如果需要全文检索,可以使用列表的垂直搜索功能. 1.新建一个测试页面,然后右键在高级模式下编辑,如下图: 2.在PlaceHolderMain节点里,加入webpartzone,用来添加数据视图: 3.数据视图选择News列表,如下图: 4.列表视图就选择第一个就可以了,如下图: 5.查看测试页面,如下图: 6.在

[转载]SharePoint 2013测试环境安装配置指南

软件版本 Windows Server 2012 标准版 SQL Server 2012 标准版 SharePoint Server 2013 企业版 Office Web Apps 2013 备注:安装之前,需要各个服务器的IP地址,机器名称设置好,并且已经加入到域中. 一 安装AD域控制器 1. 添加AD服务 a) 打开服务器管理器,选择[添加角色和功能] b) 在[开始之前]页面直接点击[下一步] c) 选择[基于角色或基于功能的安装]后,点击[下一步] d) 选择[从服务器池中选择服务器