Adding Users to MOSS 2007 (SharePoint) Sites and Groups

2023-10-08 09:18

本文主要是介绍Adding Users to MOSS 2007 (SharePoint) Sites and Groups,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

以下文章转自: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.

Collapse
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

License

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

 

这篇关于Adding Users to MOSS 2007 (SharePoint) Sites and Groups的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/164259

相关文章

获取Excel文档的版本(2003或者2007)

因工作需要解析excel文档,用poi插件来进行处理,但是2003版本之前的和2007版本之后的解析方式不一样,开始,我们是以后缀名来区分的(2003之前是xls,2007之后是xlsx),后来发现,如果一个2003文档的后缀名被改成xlsx或反之,解析都会出现一些莫名其妙的问题,所以根据文档内容来判断版本是非常必要的。于是在网上找了很久终于找到一个切实可行的方法,代码如下 public s

webservice的安全机制1---users.lst

本节摘要:本节主要介绍webservice的安全机制。   1.引言   俗话说,安全第一;   那么,我们软件中是否有安全,安全是否重要呢?   sure,软件开发中当然也有安全,安全显得尤为重要。   本节,我们将一起来看看webservice的安全问题。   之前在前几节中写了webservice开发相关的知识,接下来几节我会写webservice安全相关的问题。   闲扯一

clang: error: no such file or directory: '/Users/bigxin/Desktop/not file xxx

这样的问题,解决方法有以下几种: 第一:一般多人开发的时候会出现文件缺失的问题,遇到这个问题就根据提示找到对应的地址,然后会发现这个文件名字是红色的把红色的文件删除,然后重新addfile,然后把缺失的文件拷贝过来就可以了。。第二:如果第一种方法不行的话,可以移步到 TARGETS —-> Build Phases ——> Compile Sources(编译源) 里面来找,看有没有失效的文件

Windows用户取消共享文件夹密码方法(Method for Windows Users to Cancel Shared Folder Password)

Windows用户取消访问共享文件夹密码方法 💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:Linux运维老纪的首页,持续学习,不断总结,共同进步,活到老学到老 导航剑指大厂系列:全面总结 运维核心技术:系统基础、数据库、网路技术、系统安全、自动化运维、容器技术、监

DL/T645-2007_Part2(参变量数据标识编码表)

数据类型分为7类:电能量、最大需量及发生时间、变量、事件记录、参变量、冻结量、负荷记录。 数据标识数据格式 数据 长度  (字节)单位功能数据项名称DI₃DI₂DI₁DI₀读写04 00 01 01020304050607YYMMDDWWhhmm55NNNNXXXXYYNM00hhmmYYNMDDhhmm  4  3  1  2  5  5年月日星期时分秒分分毫秒年月日时分年月日时分日期及星期(其

九度考研真题 浙大 2007-浙大1023:EXCEL排序 排序

//1023:EXCEL排序 #include<iostream>  #include<algorithm> #include<string.h> using namespace std; struct stu{ char nu[10]; char name[10]; int score; }stud[100100]; bool cmp1(stu A,

九度考研真题 浙大 2007-3浙大游船出租 结构体

//游船出租 #include<iostream>  #include<algorithm> using namespace std; struct node{ int num; char action; char time[10]; double t; bool operator <(const node&A)const{

安装jmeter的梯度压测线程组(Custom Thread Groups)的插件

1、打开:Install :: JMeter-Plugins.org 2、进入主页后点击下面图片的链接进行安装 3、将安装包放入apache-jmeter-5.4.1    >   lib       >    ext  里面 4、打开Jmeter 点击下面的【Plugins Manager】 5、进入 【Plugins Manager】后选择【Avaliable Plug

DL/T645-2007_Part2(变量数据标识编码表)

数据类型分为7类:电能量、最大需量及发生时间、变量、事件记录、参变量、冻结量、负荷记录。 数据标识数据格式数据长度字节单位功能数据项名称 DI₃ DI₂ DI₁DI₀读写 02  01  01 02 03 FF  00 XXX,X2 V十A相电压B相电压C相电压电压数据块 02  02  01 02 03 FF  00 XXX.XXX3 AA相电流B相电流C相电流电流数据块 02  03  00

HDU1669 Jamie's Contact Groups (二分+二分图的多重匹配+一对多的匹配)

多重匹配:一对多的二分图的多重匹配。二分图的多重匹配算法的实现类似于匈牙利算法,对于集合X中的元素xi,找到一个与其相连的元素yi后,检查匈牙利算法的两个条件是否成立,若yi未被匹配,则将 xi,yi匹配。否则,如果与yi匹配的元素已经达到上限,那么在所有与yi匹配的元素中选择一个元素,检查是否能找到一条增广路径,如果能,则让出位置,让xi与yi匹配。 match[i][j]表示X集合中的