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

自从人类学会了使用火,烤制的方式替代了人类的消化系统部分功能,从此人类的消化系统更加简单,加速了人脑的进化;自从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个文件:

1
2
3
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

 

 

posted @   dosboy  阅读(2304)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2007-06-24 Infopath Form Service示例:如何在InfoPath表单中引用SQL SERVER 中的数据?
点击右上角即可分享
微信分享提示