初学sharepoint 做了一个添加新账号
QuickPart是一个方便开发人员快速开发Web Part的工具。它能够将一个普通的ASP.NET用户控件包装成一个标准的Web Part,用于SharePoint中。也就是说,开发人员只需要创建出标准的ASP.NET用户控件,然后就可以使用QuickPart来将这个用户控件当做Web Part来使用。
机器上安装上QuickPart 以后,就可以自已的usercontrol 当作web part 来用
以下是添加新账号的实现
前台代码如下:


<div align="center">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="right" style="height: 22px ; padding-top:15px; color:#1987df;">
用户名:
</td>
<td style="height: 22px; padding-top:15px; color:#1987df;">
<asp:TextBox ID="txtusername" BackColor="Snow" runat="Server" BorderStyle="solid" BorderWidth="1px" BorderColor="#1987df"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" style="height: 22px; padding-top:15px; color:#1987df;">密码:</td>
<td align="left" style="height: 22px; padding-top:15px;"><asp:TextBox ID="UserPwd" runat="server" TextMode="password" BorderStyle="solid" BorderWidth="1px" BorderColor="#1987df"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" style="height: 22px; padding-top:15px; color:#1987df;">确认密码:</td>
<td align="left" style="height: 22px; padding-top:15px;"><asp:TextBox ID="surepwd" runat="server" TextMode="password" BorderStyle="solid" BorderWidth="1px" BorderColor="#1987df"></asp:TextBox></td>
</tr>
<tr >
<td colspan="2" align="center" style="height: 22px; padding-top:15px;">
<asp:Button ID="btnSubmit" runat="server" Text="添加" BorderStyle="solid" BorderWidth="1px" OnClientClick="if(!confirm('确认添加?')){return false}" BorderColor="#1987df" Width="65" BackColor="lightblue" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>

</div>
后台代码如下:
加入using System.DirectoryServices;引用


private DirectoryEntry GetDirectoryEntry()

{
DirectoryEntry entry = new DirectoryEntry("WinNT://hist-sharepoint", "用户名", "密码", AuthenticationTypes.Secure);
return entry;
}
//查找用户在域中是否存在
private bool CheckUser(string user)

{
bool sign;
DirectoryEntry entry = GetDirectoryEntry();
DirectoryEntry SearchU;
try

{
SearchU = entry.Children.Find(user, "user");
sign = false;
}
catch (Exception ex)

{
sign = true;
}
return sign;
}
//添加新用户
private bool AddNewUser(string Uname,string Upwd)

{
bool sign;
DirectoryEntry entry = GetDirectoryEntry();
try

{
DirectoryEntry newuser=entry.Children.Add(Uname, "user");//增加用户名
newuser.CommitChanges();

newuser.Invoke("SetPassword",new object[]
{Upwd} );//用户密码

newuser.Invoke("Put", new object[]
{ "FullName",Uname});

newuser.Invoke("Put", new object[]
{ "Description", "管理员" });
newuser.CommitChanges();
sign = true;
}
catch(Exception ex)

{
sign = false;
}
return sign;
}
protected void btnSubmit_Click(object sender, EventArgs e)

{

string user = this.txtusername.Text.ToString().Trim();
string strpwd = this.UserPwd.Text.ToString();
string newpsd = this.surepwd.Text.Trim().ToString();
if (user.Trim() == "")

{
Page.ClientScript.RegisterStartupScript(GetType(), "", Getjs("用户名不能为空"));
return;
}
if (strpwd.Trim() == "")

{
Page.ClientScript.RegisterStartupScript(GetType(), "", Getjs("用户密码不能为空"));
return;
}
if (newpsd.Trim() == "")

{

Page.ClientScript.RegisterStartupScript(GetType(), "", Getjs("用户确认密码不能为空"));
return;
}
if (newpsd != strpwd)

{
Page.ClientScript.RegisterStartupScript(GetType(), "", Getjs("两次输入密码不一致"));
return;
}


if (!CheckUser(user))

{
Page.ClientScript.RegisterStartupScript(GetType(), "", Getjs("该用户已存在"));
this.txtusername.Text = "";
return;
}
if (AddNewUser(user, strpwd))

{
Page.ClientScript.RegisterStartupScript(GetType(), "", Getjs("添加成功"));
this.txtusername.Text = "";
}
else

{
Page.ClientScript.RegisterStartupScript(GetType(), "", Getjs("发生错误"));
this.txtusername.Text = "";
}
}
private string Getjs(string str)

{
str = str.Replace("\"", "\\\"");
str = str.Replace("\r\n", "\\n");

string js = "<script language='javascript'>alert(\"" + str + "\");</script>";
return js;


}
posted @ 2007-08-09 12:09
江闯 阅读(110)
评论(0) 编辑 收藏 所属分类:
SharePoint