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:

27 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!

  6. My developer is trying to persuade me to move to .net
    from PHP. I have always disliked the idea because of the
    expenses. But he’s tryiong none the less. I’ve been using WordPress on a variety of websites for
    about a year and am nervous about switching to another platform.
    I have heard very good things about blogengine.net.
    Is there a way I can transfer all my wordpress posts into
    it? Any help would be really appreciated!

  7. Fantastic beat ! I would like to apprentice while you amend your site, how can i subscribe for a blog site?
    The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered
    bright clear idea

  8. Pretty nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed browsing your blog posts.

    In any case I will be subscribing to your rss feed
    and I hope you write again very soon!

  9. Excellent beat ! I wish to apprentice while you amend your web site, how can i subscribe for
    a blog website? The account aided me a acceptable deal. I had been a
    little bit acquainted of this your broadcast
    offered bright clear idea

  10. Hello! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really enjoy your content.

    Please let me know. Thank you

  11. Hey there! I’m at work browsing your blog from my new iphone
    4! Just wanted to say I love reading your blog and look forward to all
    your posts! Carry on the excellent work!

  12. Hello! Someone in my Myspace group shared this website with us so I came to give it a look.
    I’m definitely loving the information. I’m bookmarking and will be tweeting this to my
    followers! Superb blog and brilliant style and design.

  13. Great goods from you, man. I’ve bear in mind your
    stuff previous to and you’re just too great. I actually like what you have obtained here, certainly like
    what you’re stating and the best way through which you
    are saying it. You make it enjoyable and you
    still take care of to stay it sensible. I can’t wait to learn much more from you.
    That is actually a great web site.

  14. whoah this blog is fantastic i love reading your posts.
    Keep up the great work! You know, lots of persons
    are searching round for this info, you can help them greatly.

  15. Hello There. I found your weblog the use of msn.
    This is a very well written article. I’ll make sure to bookmark it and come back to learn more of your useful info.
    Thanks for the post. I’ll certainly comeback.

Leave a Reply

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