SharePoint API测试系列——Manage SharePoint Site Policy & Expiration Email with CSOM API
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/
对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;
}
}
}
}
}
SharePoint API测试系列——Manage SharePoint Site Policy & Expiration Email with CSOM API的更多相关文章
- SharePoint API测试系列——Records.BypassLocks测试
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 对于SharePoint中已经是Record的Item,我们想要修改他的属性,这在UI界面是无法完 ...
- SharePoint API测试系列——对Recorded Item做OM操作(委托的妙用)
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 对Recorded Item动态调用OM Methods进行测试,界面如下: 输入Site的URL ...
- 初学者的API测试技巧
API(应用程序编程接口)测试是一种直接在API级别执行验证的软件测试.它是集成测试的一部分,它确认API是否满足测试人员对功能.可靠性.性能和安全性的期望.与UI测试不同,API测试是在没有GUI层 ...
- SharePoint 2010 PowerShell 系列 之 备份、还原、部署 .WSP
转:http://www.cnblogs.com/Fengger/archive/2012/08/24/2654093.html PowerShell系列目录 最近在部署测试环境,就顺便把PowerS ...
- [转载]SharePoint 2013测试环境安装配置指南
软件版本 Windows Server 2012 标准版 SQL Server 2012 标准版 SharePoint Server 2013 企业版 Office Web Apps 2013 备注: ...
- 系列文章--SharePoint 2013 Designer 入门教程
SharePoint的使用中,SharePoint Designer是非常重要的工具,我们可以通过Designer设计页面.母版页,维护.管理站点,也可以定制列表表单.数据视图,设计工作流等等.下面总 ...
- 实战:ADFS3.0单点登录系列-集成SharePoint
这是本系列第四篇了,终于轮到SharePoint上场了,但是本文不会过多讲解SharePoint安装等话题,而是直入主题,讲解如何进行配置,让其于ADFS配合完成SSO的工作. 注意:本文使用的Sha ...
- ASP.NET Web API 2系列(三):查看WebAPI接口的详细说明及测试接口
引言 前边两篇博客介绍了Web API的基本框架以及路由配置,这篇博客主要解决在前后端分离项目中,为前端人员提供详细接口说明的问题,主要是通过修改WebApi HelpPage相关代码和添加WebAp ...
- 自动化测试系列(二)|API测试
在上次的自动化测试系列(一)中为大家大体介绍了自动化测试的概念,本文主要针对API测试的概念及API测试在猪齿鱼Choerodon中的实践展开. API(应用程序编程接口)测试是一种软件测试,可以直接 ...
随机推荐
- x-code的使用技巧心得
xcode是苹果开发的一款图形化,而且用户交互很好的开发软件开发工具. 它支持 C语言 o-bjiect语言 c++ 等多种语言的开发.功能强大,俗话说的好,工欲善其事,必先利其器,以下将描写叙述一下 ...
- [Done]mysql in (#{list}) 只能查询/删除第一条的问题
数据如下(注意age是int类型): sql如下(注意是#不是$): java代码: Mybatis日志(只返回一笔记录): 直接在mysql中执行(age是int类型,注意参数带引号,确认jdbc是 ...
- Rational Rose 2003 逆向工程转换C++源代码成UML类图
主要介绍用户如何使用Rose的逆向工程生成UML模型,并用来进行C++代码的结构分析. Rational Rose可以支持标准C++和Visual C++的模型到代码的转换以及逆向工程.下面将详细地说 ...
- Nginx中Laravel的配置
server { listen 80; server_name sub.domain.com; set $root_path '/var/www/html/application_name/publi ...
- JavaScript-event参数传递详解
onmouseover="over(event)" onmouseout="out(event)" onclick="change(event)&qu ...
- HawkHost老鹰主机更换主域名方法
http://www.yd631.com/change-hawkhost-primary-domain/圣诞节优惠期间,很多童鞋们购买了老鹰主机,可能由于大家初次使用海外主机或者是CP面板的空间.购买 ...
- 数据存储的两种方式:Cookie 和Web Storage
数据存储的两种方式:Cookie 和Web Storage 1.Cookie Cookie的作用就像你去超市购物时,第一次给你办张购物卡,这个购物卡里存放了一些你的个人信息,下次你再来这个连锁超市时, ...
- 浅析若干Java序列化工具【转】
在Java中socket传输数据时,数据类型往往比较难选择.可能要考虑带宽.跨语言.版本的兼容等问题.比较常见的做法有: 采用java对象的序列化和反序列化 把对象包装成JSON字符串传输 Googl ...
- 关于ftpshell脚本中mget中去除多余交互式提示的方法
默认情况下ftp里面的交互式提示是开启的,平常如果下载多个文件时,这种提示很让人烦,如果是在shell脚本里面要从ftp服务器上一次mget多个文件,写个交互式很麻烦,最好是把这个交互式提示关掉. 进 ...
- Python学习笔记015——序列(字节数组 bytearray)
1 序列 常见的序列有:list tuple str bytes(字节串) bytearray 2 字节数组bytearray 可变的字节序列,相当于bytes的可变版本. 3 创建函数by ...