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(应用程序编程接口)测试是一种软件测试,可以直接 ...
随机推荐
- 如何在MyEclipse中将项目部署Tomcat
如果系统里面还没有配置Tomcat,请参照<Windows下如何配置tomcat环境变量>. 1.新建一个web project项目.如下: 效果如下: 2.编写一个实现Servlet接口 ...
- scala中:: , +:, :+, :::, +++的区别
4种操作符的区别和联系 一. :: 该方法被称为cons,意为构造,向队列的头部追加数据,创造新的列表. 用法为 x::list,其中x为加入到 头部的元素,无论x是列表与否,它都只将成为新生成列 ...
- oracl查询锁表语句
技能源于生活的不断实践,实践是对知识的不断扩展和总结.汇总.进而形成思想体系! --查询锁表语句 select sess.sid, sess.serial#, lo.oracle_username, ...
- 关联更新SQL语句
update F_A_Info set level=b.level from F_A_Info a,F_A_Info_QUAN b where a.id=b.id
- hibernate4无法保存数据
hibernate4无法保存数据 author: hiu 以后都发文章我都备注一下作者了,hiu就是我了 红色字体更新日期:2014-07-08 初次使用hibernate4,使用getCurrent ...
- php新特性:trait 关键字使用
1.trait关键字:含义[特性] 1.1 和require include 区别: 后两者需要 实例化一个类或者静态调用,而trait相当于继承,但又不是extends关键字,它解决了单继承. 2. ...
- 一个PHP操作大变量的例子
By C extensions we can directly manipulate the large PHP variables, such as:GET,POST,SERVER You can ...
- 关于远程访问Oracle数据库的设置(共享数据库)
写在前面: 需求描述: 我笔记本上安装了oracle数据库, 现在同事也想连接这个数据库. 也就是设置为别人能远程访问我本地的数据库. 思路: 通过在我笔记本WiFi共享,别人连接该W ...
- 初学HTML一些基本控件语句
<html> <head> <title> 这是网页的标题</title> </head> <body> <h2>& ...
- TortoiseSVN 覆盖SVN仓库最新版本提交
情况背景: ibank.pdm文件最新版本有问题,版本号为5051. 我想在5050的版本上修改后提交,覆盖5051版本的修改,也就是经过我修改后的5050版本,覆盖5051版本的修改,提交成功并成为 ...