We can add users to a SharePoint site using object model.
One possible way:
SPRoleAssignment MyRoleAssign = new SPRoleAssignment(”domain/alias”, “email address”, “User Name”, “Description”);
SPRoleDefinition MyRoleDef = newSubWeb.RoleDefinitions["Contribute"];
MyRoleAssign.RoleDefinitionBindings.Add(MyRoleDef);
site.RoleAssignments.Add(MyRoleAssign);
Second is:
SPGroup AddUserGroup = site.Groups["Group name"];
AddUserGroup.AddUser(”domain/alias”, “email address”, “User Name”, “Description”);
To add bulk of users in one go, following code is preferred:
SPUserInfo[] AddUsers = new SPUserInfo[1];
AddUsers[0].Email = “email address”;
AddUsers[0].LoginName = “domain\login”;
AddUsers[0].Name = “name”;
AddUsers[0].Notes = “notes”;
site.Roles["Contributor"].Users.AddCollection(AddUsers);
hyze said,
April 15, 2008 @ 1:50 am
Hai FarhanFaiz,
Can I know how to use that object model and code for the test. Actually i always found the sample code like this but dont know where to start and how. Can you help me how to do that in visual studio..
Appreciate your help
fyi: i’m not .net programmer but want to know the result of some code above.
farhanfaiz said,
April 15, 2008 @ 3:45 pm
hyze,
I will try to explain the whole process step-by-step:
1- Open VS and select “Console Application” as new project.
2- Add reference of “SharePoint”. For details check this link (http://farhanfaiz.wordpress.com/2007/12/31/creating-adding-an-event-handler/ )
3- Completed Code is as under:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
namespace AddUsers
{
class AddUserClass
{
static void Main(string[] args)
{
SPSite siteCollection = new SPSite(”Site URL”);
SPWeb site = siteCollection.OpenWeb();
SPGroup AddUserGroup = site.Groups["Group Name"];
AddUserGroup.AddUser(”domain/alias”, “email address”, “Name”, “Description”);
OR
SPRoleAssignment MyRoleAssign = new SPRoleAssignment(”domain/alias”, “email address”, “Name”, “Description”);
SPRoleDefinition MyRoleDef = newSubWeb.RoleDefinitions["Contribute"];
MyRoleAssign.RoleDefinitionBindings.Add(MyRoleDef);
site.RoleAssignments.Add(MyRoleAssign);
site.Dispose();
siteCollection.Dispose();
}
}
}
4- Run the code and check it in the site.
In case you require more assistance of any of the above steps, do let me know.
hyze said,
April 16, 2008 @ 1:53 am
thanks bro for the explanation. it’s work and easy. things is, i need to start coding. really appreciate your help
Rudy said,
April 23, 2008 @ 7:19 am
Hai FarhanFaiz,
Thanks for the code, what if we have to create a Group through code other than adding users to an already existing Group?
Pls explain
farhanfaiz said,
April 24, 2008 @ 5:37 pm
Rudy,
Try the following line, hopefully it will work:
site.SiteGroups.Add(….)
john said,
May 19, 2008 @ 12:22 am
Thank you for your site. I have found here much useful information…
Melissa said,
May 19, 2008 @ 12:46 am
I thank the Lord for giving us the gift of brilliant preachers!o
Dan said,
May 21, 2008 @ 6:24 pm
s prazdnikov vas
Melissa said,
May 24, 2008 @ 6:51 am
Looking for information and found it at this great site…e
chanaka said,
May 29, 2008 @ 5:33 am
i wnt to get data from the SPSITE, but when i trying to access in progarmaticaly it gives “the system administrator may need to add a new request URL mapping to the intended application”, can you help me please!!!
farhanfaiz said,
May 29, 2008 @ 3:37 pm
Chanka,
Any details of the specific data that you want to extract?
Regards
Trent said,
June 11, 2008 @ 3:16 pm
Looked everywhere for this code great post! Have a question though…
When i add the user i then need to update other attributes associated with that user.
this is my code
Dim AddUserGroup As SPGroup = newSubWeb.Groups(”Users”
AddUserGroup.AddUser(Account, Email, loginName, Description)
dim newUser as spuser = newSubWeb.AllUsers(loginName)
‘update details of spuser
updateUserDetailsSP(newUser)
——-
I get an error back to say ‘User cannot be found.’ but the user is being added. I think it may be trying to find the user before the whole process has been completed.
Unfortunately the add method doesn’t return the user eg.
dim newUser as spuser = AddUserGroup.AddUser(Account, Email, loginName, Description)
Do you know if there is another way?
Thanks again
farhanfaiz said,
June 11, 2008 @ 10:59 pm
Trent,
I am a bit lost. What are the attributes that you want to update? Can you specify anyone.
Regards,
Farhan
Trent said,
June 12, 2008 @ 2:45 am
All data is stored for each user in a list called ‘User Information List’ on the root site. You can setup extra metadata on that list like any other list.
Say i had this
dim newUser as spuser = newSubWeb.AllUsers(loginName)
dim userID as string=newUser.Id
UserID is now the row in which i can update that User Information List with any data i want eg. Job Title, Phone, Branch etc.
farhanfaiz said,
June 12, 2008 @ 5:34 pm
Trent,
What I can say is that try to generate list of all the users in the site and see if code can fetch the recently added user or not.
do let me know the result.
Trent said,
June 13, 2008 @ 7:15 am
It will find the user within the full list but there is about a 30 second delay after first inserting the user. Just thought you might have known a way to return the user at point of inserting the new user. Thanks anyways.
farhanfaiz said,
June 13, 2008 @ 8:14 pm
Trent,
It may be that you have tried the following work around already but that worked for me in case of list update.
1 - Try different vesion of update if exist.
2- Use AllowUnsafeUpdate
Regards,