以下文章转自:http://www.codeproject.com/KB/sharepoint/Adding_users_to_MOSS_2007.aspx
作者:Danimal(521)Introduction
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.
在最近的MOSS 2007项目中,我们团队需要一种统一的添加用户到我们的数据库中,然后将其添加到SharePoint站点或者组里面去。我搜集了有关的资料,发现了很多不同的方法,但是中间很多方法都不是可行的。这篇文章是关于我所采用的方法,希望它对你们能有所帮助。
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
:
这篇文章的大部分我都在列举我的代码,并且做了简要的说明了它的功能。It is pretty straight forward but there are subtle nuances(细微差别).我的方法首先查找user,如果user已经包含了site collection中,我只是将它添加到group中。否则,我先将用户添加到site中,然后再添加到group中。首先引用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;
try
{
//Open the ShrePoint site
spSite = new SPSite(strSiteURL);
spWeb = spSite.OpenWeb();
//Check to see if user exists
spReturn = spWeb.AllUsers[txbUser.Text];
}
catch(Exception)
{
}
finally
{
spWeb.Close();
spSite.Close();
}
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;
try
{
//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 =
spWeb.RoleDefinitions["Contribute"];
spRoleAssignment.RoleDefinitionBindings.Add(spSPRoleDefinition);
spWeb.RoleAssignments.Add(spRoleAssignment);
//Update site
spWeb.Update();
spReturn = spWeb.AllUsers[strLoginName];
}
catch(Exception)
{
}
finally
{
spWeb.Close();
spSite.Close();
}
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");
spGroup.Update();
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.
History
- 24 October, 2007 - Initial version