1

I have a list for personel.

I want add new user to AD when adding a new item in personel list.

I use this code but don't created user

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;
}

EDIT

The following worked for me:

 SPUser user = spWeb.EnsureUser(strLoginName);
 SPRoleAssignment spRoleAssignment = 
 new SPRoleAssignment(user);
3
  • 1
    We need more information to answer your question. Are you saying you want to add an item to SharePoint list and have that create a user in AD? Or are you referring to user profile synchronization?
    – SPDoctor
    Commented Feb 2, 2012 at 12:09
  • 1
    Or maybe hes looking for a webpart to manage AD? adselfservice.codeplex.com
    – Kit Menke
    Commented Feb 2, 2012 at 15:46
  • Yes , I want created a webpart for manage AD.
    – ar.gorgin
    Commented Feb 3, 2012 at 5:19

2 Answers 2

2

first you need Event Receiver to handle ItemAdded event, something like this:

class YOUR_EVENTHANDLER_CLASS_NAME : SPItemEventReceiver
    {

        public override void ItemAdded(SPItemEventProperties properties)
        {

           //Your Code to Add User To AD
        }

     }

you can read current item with this line:

SPListItem personelItem = properties.ListItem;

then you need to read personelItem fields (for username and other info you need to add to AD) and then you must write your code to Add User To Active Directory

1
  • Thanks, I need Event Receiver to handle ItemAdded event and then i want add user to AD . but i don't use this code in sharepoint.
    – ar.gorgin
    Commented Feb 3, 2012 at 5:33
-2

it seems to sync the items in the sharepoint list as new users to the AD. It is possible.

1
  • I can add a new user to manually?
    – ar.gorgin
    Commented Feb 2, 2012 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.