Click here to Skip to main content
13,288,520 members (84,333 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 24 Oct 2007

Adding Users to MOSS 2007 (SharePoint) Sites and Groups

, 24 Oct 2007
Rate this:
Please Sign up or sign in to vote.
This article describes a simple approach to adding users to MOSS 2007 (SharePoint)


On a recent project with MOSS 2007, our team was required to create a unified user creation wizard that added user information to our database and then added the new user to a site/group in SharePoint. I dug around and found several sources that showed different ways of doing this task however I found that there is "more to the story". This article is about the approach I took, I hope it helps.

Using the Code

For the most part I am just going to list code fragments that I used in this project and talk briefly on what it is doing. It is pretty straight forward but there are subtle nuances. The approach I took was to first look for the user. If the user was already in the site collection then I just had to add her or him to the group. Otherwise I would have to add the user first to the site and then to the group. To start, you will need a reference to Microsoft.SharePoint:

using Microsoft.SharePoint;

The first function tries to return a user. You want to look first to make sure that the user is not currently in the site collection. In my code, if GetSPUser returns null then I know I have to create the user. You might want to handle exceptions differently. I have removed our exception handling routines for clarity.

private SPUser GetSPUser(string strLoginName, string strSiteURL)
    SPUser spReturn = null;
    SPSite spSite = null;
    SPWeb spWeb = null;
        //Open the ShrePoint site
        spSite = new SPSite(strSiteURL);
        spWeb = spSite.OpenWeb();
        //Check to see if user exists
        spReturn = spWeb.AllUsers[txbUser.Text];

    return spReturn;

Now if GetSPUser returns null then we have to create the user in the site collection. What's important here is the SPRoleAssignment and SPRoleDefinition. These should be set to create the user correctly. You have to tell SharePoint what access level the new user has. In this sample I am just using Contribute. You may want to make that a parameter or use a different level. Notice that I am not calling spWeb.Users.Add() as this is not needed. When you add the role definition, it will add the user as well.

private SPUser CreateUser(string strLoginName, string strEMail, 
    string strName, string strNotes, string strSiteURL)
    SPUser spReturn = null;
    SPSite spSite = null;
    SPWeb spWeb = null;

        //Open the SharePoint site
        spSite     = new SPSite(strSiteURL);
        spWeb     = spSite.OpenWeb();

        //Assign role and add user to site
        SPRoleAssignment spRoleAssignment = 
            new SPRoleAssignment(strLoginName, strEMail, strName, strNotes);
        //Using Contribute, might need high access
        SPRoleDefinition spSPRoleDefinition = 

        //Update site
        spReturn = spWeb.AllUsers[strLoginName];

    return spReturn;

Putting It All Together

Here is the code from the main function. Once again, open the site, get or create the user, then add to the group.

//Open Site
SPSite spSite = new SPSite(strSiteURL);
SPWeb spWeb = spSite.OpenWeb();

//Get or create user
SPUser spUser = GetSPUser(strLoginName, strSiteURL);

if(spUser == null)
    spUser = CreateUser(strLoginName, strEMail, strName, strNotes, strSiteURL);

//Open group
SPGroup spGroup = spWeb.SiteGroups[strGroup];

//Add and update group with new user
spGroup.AddUser(spUser.LoginName, spUser.Email, spUser.Name, 
    "Added by UserControl");

Points of Interest

In general, SharePoint MOSS 2007 is a very powerful platform but some things do not seem to work the way they should. I hope this helps anyone trying to automate the adding of users to sites or groups.


  • 24 October, 2007 - Initial version


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Dan Biscup is a MCAD working as a consultant. More to come....

You may also be interested in...


Comments and Discussions

GeneralNice Article Pin
Prabhu_Pappu21-Mar-13 1:30
memberPrabhu_Pappu21-Mar-13 1:30 
QuestionNice article Pin
asifiqbaljh29-Jan-13 1:05
memberasifiqbaljh29-Jan-13 1:05 
AnswerHere's all the code, that works. I need to change for final purpose, but you will be able to see missing pieces from orignal posting. Pin
J Keith Montz28-Oct-11 16:58
memberJ Keith Montz28-Oct-11 16:58 
GeneralUser cannot be found in GetSPUser() method Pin
jsnelders30-Dec-09 13:12
memberjsnelders30-Dec-09 13:12 
GeneralError on trying to use this code Pin
afilho10-Nov-09 9:52
memberafilho10-Nov-09 9:52 
QuestionGet All Users from Each Groups Pin
anwarul28-Jan-09 20:26
memberanwarul28-Jan-09 20:26 
Generalroleassignments... user does not exist or is not unique Pin
liamsheik7-Mar-08 10:47
memberliamsheik7-Mar-08 10:47 
GeneralRe: roleassignments... user does not exist or is not unique Pin
Mrogr7-Jun-10 1:29
memberMrogr7-Jun-10 1:29 
GeneralCustomize List Webpart Pin
sanong5-Nov-07 23:01
membersanong5-Nov-07 23:01 
GeneralA very nice Article Pin
Hemant.Kamalakar31-Oct-07 21:13
memberHemant.Kamalakar31-Oct-07 21:13 
GeneralGreat Article !! Pin
Chirag R Darji25-Oct-07 22:36
memberChirag R Darji25-Oct-07 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 24 Oct 2007
Article Copyright 2007 by Danimal(521)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid