Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
share|improve this question
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 Feb 2 '12 at 12:09
1  
Or maybe hes looking for a webpart to manage AD? adselfservice.codeplex.com –  Kit Menke Feb 2 '12 at 15:46
    
Yes , I want created a webpart for manage AD. –  ar.gorgin Feb 3 '12 at 5:19
add comment

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
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 Feb 3 '12 at 5:33
add comment

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

share|improve this answer
    
I can add a new user to manually? –  ar.gorgin Feb 2 '12 at 8:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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