Basic Operation’s That SharePointer Needs For SharePointing By Using CSOM – Part 4

SharePoint user tasks

You can use the SharePoint .NET Framework CSOM to manage SharePoint users, groups, and user security.

Add a user to a SharePoint group

This example adds a user and some user information to a SharePoint group named Members.

C#
// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

GroupCollection siteGroups = context.Web.SiteGroups; 

// Assume that there is a "Members" group, and the ID=5. 
Group membersGroup = siteGroups.GetById(5); 

// Let's set up the new user info. 
UserCreationInformation userCreationInfo = new UserCreationInformation(); 
userCreationInfo.Email = "user@domain.com"; 
userCreationInfo.LoginName = "domain\\user"; 
userCreationInfo.Title = "Mr User"; 

// Let's add the user to the group. 
User newUser = membersGroup.Users.Add(userCreationInfo); 

context.ExecuteQuery();  

Retrieve all users in a SharePoint group

This example retrieves information about all users from a SharePoint group named Members.

C#
// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

GroupCollection siteGroups = context.Web.SiteGroups; 

// Assume that there is a "Members" group, and the ID=5. 
Group membersGroup = siteGroups.GetById(5); 
context.Load(membersGroup.Users); 
context.ExecuteQuery(); 

foreach (User member in membersGroup.Users) 
{ 
    // We have all the user info. For example, Title. 
    label1.Text = label1.Text + ", " + member.Title; 
}  

Create a role

This example creates a role that has create and manage alerts permissions.

C#
// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

BasePermissions perm = new BasePermissions(); 
perm.Set(PermissionKind.CreateAlerts); 
perm.Set(PermissionKind.ManageAlerts); 

RoleDefinitionCreationInformation creationInfo = new RoleDefinitionCreationInformation(); 
creationInfo.BasePermissions = perm; 
creationInfo.Description = "A role with create and manage alerts permission"; 
creationInfo.Name = "Alert Manager Role"; 
creationInfo.Order = 0; 
RoleDefinition rd = context.Web.RoleDefinitions.Add(creationInfo); 

context.ExecuteQuery();  

Add a user to a role

This example adds a user to a role.

C#

// Starting with ClientContext, the constructor requires a URL to the

// server running SharePoint.

ClientContext context = new ClientContext("http://SiteUrl");

// Assume that we have a SiteUser with Login user.

Principal user = context.Web.SiteUsers.GetByLoginName(@"domain\user");

// Assume that we have a RoleDefinition named "Read".

RoleDefinition readDef = context.Web.RoleDefinitions.GetByName("Read"); RoleDefinitionBindingCollection roleDefCollection = new RoleDefinitionBindingCollection(context);

roleDefCollection.Add(readDef);

RoleAssignment newRoleAssignment = context.Web.RoleAssignments.Add(user, roleDefCollection); context.ExecuteQuery();


Part 1
Part 2
Part 3

Please follow and like us:

8 thoughts on “Basic Operation’s That SharePointer Needs For SharePointing By Using CSOM – Part 4

  1. You really make it seem so easy with your presentation but I find
    this topic to be actually something which I think I would never understand.
    It seems too complex and very broad for me. I am looking forward for
    your next post, I’ll try to get the hang of it!

  2. It is really a nice and useful piece of info. I’m satisfied that you shared this helpful information with us.
    Please stay us up to date like this.

  3. I’m extremely pleased to uncover this web site.
    I want to to thank you for ones time for this particularly
    wonderful read!! I definitely savored every bit of it and I have you
    saved as a favorite to look at new stuff in your blog.

  4. Someone essentially lend a hand to make severely posts I would
    state. That is the very first time I frequented your web page
    and up to now? I amazed with the analysis you made to make this actual post incredible.
    Magnificent job!

  5. It is appropriate time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I want to suggest you few interesting things or tips.
    Maybe you could write next articles referring to this article.
    I desire to read even more things about it!

Leave a Reply

Your email address will not be published. Required fields are marked *