I have a custom list that I'm using to store requests for new sites from users. One of the columns is a Person data type and is used for indicating the new site's owner. I've got an event receiver bound to this list that creates a new site based on the user input using a custom site template. The code also creates 3 custom groups in the new site. Everything works except for one thing, adding the selected site owner to one of the new groups. The owner may or may not be the same person completing the request. I stepped thru the code in debugger and found that it's returning a User Not Found exception. I also saw that the value coming from the Person column is the display name, not the active directory ID, for example "39;#Joe Blow.
Here's the code:
using System;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using VSeWSS;
namespace SiteProvisioningHandler
{
[CLSCompliant(false)]
[Guid("67c1d345-a060-459c-b161-c7fbec702442")]
public class SiteProvisioningHandlerItemEventReceiver : SPItemEventReceiver
{
/// <summary>
/// Initializes a new instance of the Microsoft.SharePoint.SPItemEventReceiver class.
/// </summary>
public SiteProvisioningHandlerItemEventReceiver()
{
}
public override void ItemAdded(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
{
const Int32 LOCALE_ID_ENGLISH = 1033;
SPWebTemplateCollection Templates = site.GetCustomWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));
SPWebTemplate siteTemplate = Templates["Custom Site"];
SPListItem spitem = properties.ListItem;
string siteName= spitem["Title"].ToString();
string siteURL= spitem["Site URL"].ToString();
string siteDesc= spitem["Site Description"].ToString();
string siteOwner= spitem["Site Owner"].ToString();
web.Webs.Add(siteURL,siteName,siteDesc,Convert.ToUInt32(LOCALE_ID_ENGLISH), siteTemplate,false,false);
using (SPWeb newweb = site.OpenWeb(siteURL))
{
newweb.AllowUnsafeUpdates = true;
//update permissions on new site
if (!newweb.HasUniqueRoleAssignments)
{
newweb.BreakRoleInheritance(true);
}
string[] grouptypes = {"Moderators", "Contributors", "Creators" };
foreach (string gtype in grouptypes)
{
string group_name = string.Concat(siteName, " ", gtype);
SPMember defOwner = web.SiteUsers["mydomain\\system account"];
SPUser defUser = null;
if (gtype.Equals("Contributors"))
{
defUser = web.SiteUsers[" NT AUTHORITY\\authenticated users "];
}
if (gtype.Equals("Moderators"))
{
newweb.EnsureUser(siteOwner);
defUser = web.SiteUsers[siteOwner]; //this line fails with user not found
}
newweb.SiteGroups.Add(group_name, defOwner, defUser, gtype + " to this site");
SPGroup newgroup = newweb.SiteGroups[group_name];
SPRoleAssignment roleAssignment = new SPRoleAssignment(newgroup);
SPRoleDefinition RoleDefinition;
switch (gtype)
{
case "Contributors":
RoleDefinition = newweb.RoleDefinitions.GetByType(SPRoleType.Reader);
roleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
newweb.RoleAssignments.Add(roleAssignment);
newweb.Update();
break;
case "Creators":
RoleDefinition = newweb.RoleDefinitions.GetByType(SPRoleType.Contributor);
roleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
newweb.RoleAssignments.Add(roleAssignment);
newweb.Update();
break;
case "Moderators":
RoleDefinition = newweb.RoleDefinitions["Design Owner"];
roleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
newweb.RoleAssignments.Add(roleAssignment);
newweb.Update();
break;
}
}
}
spitem["Site Status"] = "Site Provisioned on " + DateTime.Now.ToString();
spitem.Update();
}
}
});
base.ItemAdded(properties);
}
}
}
I thought for sure the EnsureUser method would resolve this problem, but to no avail...possibly due to the display name being returned instead of the active directory credentials. Any thoughts on how to handle?
Thanks!