您的位置:首页 > 运维架构 > 网站架构

在C#开发中如何使用Client Object Model客户端代码获得SharePoint 网站、列表的权限情况

2014-06-24 13:53 841 查看
自从人类学会了使用火,烤制的方式替代了人类的消化系统部分功能,从此人类的消化系统更加简单,加速了人脑的进化;自从SharePoint 2010开始有了Client Side Object Model ,我们就可以不用在服务器上开发SharePoint解决方案了,开发的方式更加多元化,这又加速了SharePoint 更大范围的应用。

现在,我们可以在任一台PC上安装Visual Studio 然后使用类似于 Object Model的模型来访问服务器上的列表、网站或是其它任何东东。

那么 ,如何使用 Client Side Object Model 客户端代码获得SharePoint 网站、列表的权限情况呢,我们需要一台客户机先利用VS建一个“控制台”程序,在这个程序里,我们要进行如下的步骤:

1. 新建一个“控制台程序”, 添加Client Side Object Model客户端的DLL文件到项目的“引用”当中,

您需要添加如下2个文件:

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime.dll


这2个文件可以从SharePoint服务器中找到,方便大家,我提供一下地址(如果是SharePoint 2010,请把15换成14):

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI

因为服务器是X64的,您老如果使用X86的Win7,也没有关系照样可以使用这个64位DLL,但这个项目,一定要保证目标平台是“Any”。否则会出错。

2. 打开代码文件,前面添加引用:using Microsoft.SharePoint.Client

然后在 Main 函数,中复制,粘贴如下的代码到Main 函数中,

//如果你想获得SITE的权限列表,应该这样写
//string ObjectType = "Site", ObjectTitle = "网站名称", SiteUrl = "网站的URL";
string ObjectType = "List", ObjectTitle = "文档", SiteUrl = "http://sp2013";

ClientContext clientContext = new ClientContext(SiteUrl);
clientContext.Credentials = new NetworkCredential("administrator", "密码");
List selectedList = null;
Web selectedWeb = null;
Console.WriteLine("Object:" + ObjectType + " Name:" + ObjectTitle + " URL:" + SiteUrl);
try
{
if (ObjectType != "Site")
{
selectedList = clientContext.Web.Lists.GetByTitle(ObjectTitle);
clientContext.Load(selectedList);

}
else
{
selectedWeb = clientContext.Web;
clientContext.Load(selectedWeb);

}

clientContext.ExecuteQuery();
}
catch (Exception  wex)
{
Console.WriteLine(wex.Message);
Console.ReadLine();
return;
}
RoleAssignmentCollection ras = null;
if (ObjectType != "Site")
{
ras = selectedList.RoleAssignments;
clientContext.Load(ras);
}
else
{
ras = selectedWeb.RoleAssignments;
clientContext.Load(ras);
}

clientContext.ExecuteQuery();
Console.WriteLine("It has " + ras.Count + " role assignments");
foreach (var ra in ras)
{
clientContext.Load(ra.RoleDefinitionBindings);
clientContext.Load(ra.Member);
clientContext.ExecuteQuery();
foreach (var definition in ra.RoleDefinitionBindings)
{
clientContext.Load(definition, d => d.Name);
clientContext.ExecuteQuery();
//C#在输入中英文混合字符时,对齐会不正常,这个语句主要是给用户名添加空格的
string tmpname = ra.Member.Title.Trim() + new string(' ', 30 - Encoding.Default.GetByteCount(ra.Member.Title.Trim()));
Console.WriteLine("{0,-20}{1}{2,-15}", ra.Member.PrincipalType.ToString().Trim(), tmpname, definition.Name);
}
}

Console.ReadLine();


说明:

只要改变一下红色标记的变量值,这个代码就可以获得任意网站、列表的权限情况, 如果是网站就把 ObjectType变量写成Site,如果是列表就把ObjectType写成是List,其它的不解释了。

示例结果:



Client-Side 的原理:

1、初始化Web,

ClientContext(SiteUrl)  ,这个函数可以返回类似于OM中的SPWeb的对象, 并且不需要从SPSite中获取。

2、必在使用对象的属性代码前,加上如下语句,有了这个语句,系统才会向服务器提交HTTP查询,对象的属性才可以被使用。

clientContext.Load(变量名或是对象,linq表达式);
clientContext.ExecuteQuery();


具体可以参考MSDN的相关文章,相信您一定会很快入门:

非常好的PPT:

https://spstc-public.sharepoint.com/Lists/Sessions/Attachments/24/Client-Side%20Object%20Model%20for%20SharePoint%202013%20-%20Bleeker.pdf

非常好的中文入门教材:

http://msdn.microsoft.com/zh-cn/office/fp179912
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐