绑定类型为People and group
private void BindOwner()
{
using (SPSite site = new SPSite(siteurl))
{
using (SPWeb web = site.OpenWeb())
{
DataTable ownerTable = Make_Table();
foreach (SPUser user in web.SiteGroups["Owner"].Users)
{
if (user.Name != "System Account")
{
DataRow row = ownerTable.NewRow();
row["Name"] = user.Name;
row["LoginName"] = user.LoginName;
ownerTable.Rows.Add(row);
}
}
DropDownListOwner.DataSource = ownerTable;
DropDownListOwner.DataTextField = "Name";
DropDownListOwner.DataValueField = "LoginName";
DropDownListOwner.DataBind();
}
}
}
private DataTable Make_Table()
{
DataTable nameTable = new DataTable("Names");
nameTable.Columns.Add("Name");
nameTable.Columns.Add("LoginName");
return nameTable;
}
插入时需注意的:
using (SPSite site = new SPSite(siteurl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["AuditSystem"];
item["Owner"] = web.SiteUsers[DropDownListOwner.SelectedValue];
item.Update();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}