How to obtain permissions for SharePoint sites and lists in C # development using the client Object model clients code

Source: Internet
Author: User

Since the human society has learned to use fire, baked in a way to replace the human digestive system part of the function, since the human digestive system more simple, accelerating the evolution of the human brain; since SharePoint 2010 started with the client Side Object Model, Instead of developing a SharePoint solution on the server, we've developed a more diversified approach that accelerates the wider range of apps in SharePoint.

Now we can install visual Studio on either PC and then use a model like Object to access lists, websites, or anything else on the server.

So, how do I use the client Side Object Model to get permission to SharePoint sites and lists, we need a client to build a "console" program with VS, and in this program we'll take the following steps:

1. Create a new "console program" and add the client Side the DLL file of the Object model to the project "References",

You need to add the following 2 files:

Microsoft.SharePoint.Client.dllMicrosoft.SharePoint.Client.Runtime.dll

These 2 files can be found from the SharePoint server, convenient for everyone, I provide the address (if it is SharePoint 2010, please change 15 to 14):

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

Because the server is X64, you old if you use X86 Win7, there is no relationship can still use this 64-bit DLL, but this project, we must ensure that the target platform is "any". Otherwise, there will be an error.

2. Open the code file, before adding a reference:using Microsoft.SharePoint.Client

Then in the main function, copy, paste the following code into the main function,

     //If you want to get a list of permissions for the site, you should write this//string ObjectType = "Site", Objecttitle = "website name", SiteURL = "URL of the website";            stringObjectType ="List", Objecttitle ="Document", SiteURL ="http://sp2013"; ClientContext ClientContext=NewClientContext (SiteURL); Clientcontext.credentials=NewNetworkCredential ("Administrator", "Password"); 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(varRainchRAS) {Clientcontext.load (RA.                Roledefinitionbindings); Clientcontext.load (RA.                Member);                Clientcontext.executequery (); foreach(varDefinitioninchRA. roledefinitionbindings) {clientcontext.load (definition, D=d.name);                    Clientcontext.executequery (); //C # in the input of Chinese and English mixed characters, alignment will not be normal, this statement is mainly to add a space to the user name                    stringTmpname = ra. Member.Title.Trim () +New string(' ', --Encoding.Default.GetByteCount (RA.                    Member.Title.Trim ())); Console.WriteLine ("{0,-20}{1}{2,-15}", RA. Member.PrincipalType.ToString (). Trim (), tmpname, definition.                Name); }} console.readline ();

Description

Just change the value of the variable red tag, this code can get any site, list permissions, if the site is the objecttype variable written to site, if it is a list of objecttype written as a list, the others do not explain.

Example results:

The principle of client-side:

1. Initialize the web,

ClientContext (SiteURL)  , this function returns an object similar to SPWeb in the OM and does not need to be fetched from SPSite .

2, must use the object's property code, plus the following statement, with this statement, the system will submit to the server HTTP query, object properties can be used.

Clientcontext.load (variable name or object, LINQ expression); Clientcontext.executequery ();

Refer to the MSDN article for details, and I'm sure you'll get started soon:

Very good ppt:

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

Very good introductory materials:

http://msdn.microsoft.com/en-us/library/office/jj163886 (v=office.15). aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.