1

How do I create a new user account in Active Directory from SharePoint 2013 programmatically?

3
  • 1
    What would the source of the user be, AD? OAuth? Feb 26, 2014 at 13:55
  • AD. Sorry for not specifying this. Would it be possible to create an user account that does not exist in AD?
    – Pantani
    Feb 26, 2014 at 14:00
  • 2
    It would, but it would need high permissions in the AD from the user executing the code (be that elevated or not). Appart from that, it would be just like any C# code: stackoverflow.com/questions/1298449/… Feb 26, 2014 at 14:02

2 Answers 2

2

It would, but it would need high permissions in the AD from the user executing the code (be that elevated or not), meaning that the user must have privileges to add users in AD.

Appart from that, it would be just like any C# code, you can follow this post for some sample code: https://stackoverflow.com/questions/1298449/create-active-directory-user-in-net-c:

using System;
using System.DirectoryServices;

namespace test {
   class Program {
      static void Main(string[] args) {
        try {
            string path = "LDAP://OU=x,DC=y,DC=com";
            string username = "johndoe";

            using (DirectoryEntry ou = new DirectoryEntry(path)) {
               int NORMAL_ACCOUNT = 0x200;
               int PWD_NOTREQD = 0x20;
               DirectoryEntry user = ou.Children.Add("CN=" + username, "user");
               user.Properties["sAMAccountName"].Value = username;
               user.Properties["userAccountControl"].Value = NORMAL_ACCOUNT | PWD_NOTREQD;
               user.CommitChanges();
            }
         } 
         catch (Exception exc) {
             Console.WriteLine(exc.Message);
         }
      }
   }
}
0

You can use something like:

SPUser newUser = SPContext.Current.Web.EnsureUser(@"domain\logonname");
4
  • -1: This code isn't about creating user in AD Feb 26, 2014 at 14:42
  • @VadimGremyachev To be fare the post was very vague when Nadeem added this answer :) Feb 26, 2014 at 14:44
  • As mentioned by @RobertLindgren, this is what the post looked when I added the answer: How programmatically create a new user in Sharepoint 2013 How do I create a new user account in SharePoint 2013 programmatically? VadimGremyachev, you can check the Post Edits. Feb 26, 2014 at 14:49
  • ah, my bad, i was thinking the author wants to find out how to create user in AD Feb 26, 2014 at 14:51

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.