码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-06-25 18:48:00      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   ext   

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

 

示例结果:

bubuko.com,布布扣

 

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/en-us/library/office/jj163886(v=office.15).aspx

 

 

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

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

标签:style   class   blog   code   http   ext   

原文地址:http://www.cnblogs.com/dosboy/p/3805912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!