Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:13:27+00:00 2026-05-29T09:13:27+00:00

I want add user in ActiveDirectory. I use this code private SPUser CreateUser(string strLoginName,

  • 0

I want add user in ActiveDirectory.

I use this code

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

when spWeb.RoleAssignments.Add(spRoleAssignment);
error :”Error:The user does not exist or is not unique “

EDIT

The following worked for me:

 SPUser user = spWeb.EnsureUser(strLoginName);
 SPRoleAssignment spRoleAssignment = 
 new SPRoleAssignment(user);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:13:28+00:00Added an answer on May 29, 2026 at 9:13 am

    Your code will only grant Contribute access to an existing Active Directory user.

    If you really want to create a new Active Directory user, see Create Active Directory user in .NET (C#).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add a user in SQL Server 2008 so I can use
I want to add my user control in InstantiateIn like this: public void InstantiateIn(Control
How do I programmatically add user permissions to a list in Sharepoint? I want
I have dbml with single table users i want add partial class for User
I want to add a property to my User model that returns the number
I want to add text to speech user guided instruction to my applications. Where
In my ASP.NET MVC application I want a user to add a value into
I have a list of string values that I want add to a hashtable
I have 2 Lists: List<User> List<UserStats> So I want to add a property Count
I'm using Google Engine App with Python. I want to add custom user authentication.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.