add users to sharepoint programmaticallyI am trying to add users to sharepoint 2007 programmatically. I have set up my site to use forms authentication using the default aspsqlmembershipprovider. However, I want to be able to add a user to my database and to a sharepoint with a defined role. User are added to the database, no problem, but not to sharepoint. Hope someone can help me. My code is as follows:<br><br> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>      MembershipCreateStatus status;<br>            MembershipUser user = Membership.CreateUser(username, password, email, &quot;Not used&quot;, &quot;Not used&quot;, true, out status);<br><br>            if(status.ToString().ToLower().Equals(&quot;success&quot;))<br>            {<br>                           SPWeb portalweb = SPContext.Current.Web;<br><br>            if(portalweb != null) {<br>                SPWeb site = SPContext.Current.Site.RootWeb;<br>                SPRoleDefinitionCollection roleDefinitions = site.RoleDefinitions;<br>                SPRoleAssignmentCollection roleAssignments = site.RoleAssignments;<br><br>                SPRoleAssignment roleAssignment =<br>                        new SPRoleAssignment(username, email, username, &quot;myNotes&quot;);<br><br>                SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;<br><br>                roleDefBindings.Add(roleDefinitions[&quot;Contribute&quot;]);<br><br>                roleAssignments.Add(roleAssignment);<br>            }</p></div></div><br><br>With this code I get the following error message:<br><br>The user does not exist or is not unique. <br>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br><br>Exception Details: System.Runtime.InteropServices.COMException: The user does not exist or is not unique.<br><br>Source Error: <br><br>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  <br><br>Stack Trace: <br><br><br>[COMException (0x81020054): The user does not exist or is not unique.]<br>   Microsoft.SharePoint.Library.SPRequestInternalClass.EnsureUserExists(String bstrUrl, String bstrLogin, String bstrEmail, String bstrName, String bstrNotes, Boolean bIsRole, Boolean bSendEmail, Boolean bForceAdd, Byte[]&amp; ppsaSystemId, Boolean bImportDeleted, Int32&amp; plUserId) +0<br>   Microsoft.SharePoint.Library.SPRequest.EnsureUserExists(String bstrUrl, String bstrLogin, String bstrEmail, String bstrName, String bstrNotes, Boolean bIsRole, Boolean bSendEmail, Boolean bForceAdd, Byte[]&amp; ppsaSystemId, Boolean bImportDeleted, Int32&amp; plUserId) +145<br><br>[SPException: The user does not exist or is not unique.]<br>   Microsoft.SharePoint.Library.SPRequest.EnsureUserExists(String bstrUrl, String bstrLogin, String bstrEmail, String bstrName, String bstrNotes, Boolean bIsRole, Boolean bSendEmail, Boolean bForceAdd, Byte[]&amp; ppsaSystemId, Boolean bImportDeleted, Int32&amp; plUserId) +186<br>   Microsoft.SharePoint.SPRoleAssignmentCollection.Add(SPRoleAssignment roleAssignment) +410<br>   Netcompany.Forsikringsguiden.Backend.Agents.UserManager.CreateUser(String username, String password, String email) +373<br>   Netcompany.Forsikringsguiden.UI.Administration.Users.CreateUser.Submit_Click(Object sender, EventArgs e) +92<br>   System.EventHandler.Invoke(Object sender, EventArgs e) +0<br>   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105<br>   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107<br>   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7<br>   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11<br>   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33<br>   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102<br><br> <br><br><br><br><br><br>            <br><br><br>© 2009 Microsoft Corporation. All rights reserved.Mon, 25 May 2009 05:47:23 Zdb46ab8f-9594-4878-9bab-a18fe3fba959http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#db46ab8f-9594-4878-9bab-a18fe3fba959http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#db46ab8f-9594-4878-9bab-a18fe3fba959Thomas Moellnitzhttp://social.msdn.microsoft.com/Profile/en-US/?user=Thomas%20Moellnitzadd users to sharepoint programmaticallyI am trying to add users to sharepoint 2007 programmatically. I have set up my site to use forms authentication using the default aspsqlmembershipprovider. However, I want to be able to add a user to my database and to a sharepoint with a defined role. User are added to the database, no problem, but not to sharepoint. Hope someone can help me. My code is as follows:<br><br> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>      MembershipCreateStatus status;<br>            MembershipUser user = Membership.CreateUser(username, password, email, &quot;Not used&quot;, &quot;Not used&quot;, true, out status);<br><br>            if(status.ToString().ToLower().Equals(&quot;success&quot;))<br>            {<br>                           SPWeb portalweb = SPContext.Current.Web;<br><br>            if(portalweb != null) {<br>                SPWeb site = SPContext.Current.Site.RootWeb;<br>                SPRoleDefinitionCollection roleDefinitions = site.RoleDefinitions;<br>                SPRoleAssignmentCollection roleAssignments = site.RoleAssignments;<br><br>                SPRoleAssignment roleAssignment =<br>                        new SPRoleAssignment(username, email, username, &quot;myNotes&quot;);<br><br>                SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;<br><br>                roleDefBindings.Add(roleDefinitions[&quot;Contribute&quot;]);<br><br>                roleAssignments.Add(roleAssignment);<br>            }</p></div></div><br><br>With this code I get the following error message:<br><br>The user does not exist or is not unique. <br>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br><br>Exception Details: System.Runtime.InteropServices.COMException: The user does not exist or is not unique.<br><br>Source Error: <br><br>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  <br><br>Stack Trace: <br><br><br>[COMException (0x81020054): The user does not exist or is not unique.]<br>   Microsoft.SharePoint.Library.SPRequestInternalClass.EnsureUserExists(String bstrUrl, String bstrLogin, String bstrEmail, String bstrName, String bstrNotes, Boolean bIsRole, Boolean bSendEmail, Boolean bForceAdd, Byte[]&amp; ppsaSystemId, Boolean bImportDeleted, Int32&amp; plUserId) +0<br>   Microsoft.SharePoint.Library.SPRequest.EnsureUserExists(String bstrUrl, String bstrLogin, String bstrEmail, String bstrName, String bstrNotes, Boolean bIsRole, Boolean bSendEmail, Boolean bForceAdd, Byte[]&amp; ppsaSystemId, Boolean bImportDeleted, Int32&amp; plUserId) +145<br><br>[SPException: The user does not exist or is not unique.]<br>   Microsoft.SharePoint.Library.SPRequest.EnsureUserExists(String bstrUrl, String bstrLogin, String bstrEmail, String bstrName, String bstrNotes, Boolean bIsRole, Boolean bSendEmail, Boolean bForceAdd, Byte[]&amp; ppsaSystemId, Boolean bImportDeleted, Int32&amp; plUserId) +186<br>   Microsoft.SharePoint.SPRoleAssignmentCollection.Add(SPRoleAssignment roleAssignment) +410<br>   Netcompany.Forsikringsguiden.Backend.Agents.UserManager.CreateUser(String username, String password, String email) +373<br>   Netcompany.Forsikringsguiden.UI.Administration.Users.CreateUser.Submit_Click(Object sender, EventArgs e) +92<br>   System.EventHandler.Invoke(Object sender, EventArgs e) +0<br>   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105<br>   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107<br>   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7<br>   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11<br>   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33<br>   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102<br><br> <br><br><br><br><br><br>            <br><br><br>Tue, 19 Jun 2007 12:47:58 Z2008-03-01T04:39:04Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#1c48d7a8-776b-4923-82ac-1f7509c404b9http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#1c48d7a8-776b-4923-82ac-1f7509c404b9James Peckhamhttp://social.msdn.microsoft.com/Profile/en-US/?user=James%20Peckhamadd users to sharepoint programmatically<p>if using forms authentication and asp role/member provider, can you not use the asp role/member methods to add new users?</p> <p> </p> <p>That method you're using, i think the way it works is it takes an existing asp.net or AD account and adds it into the sharepoint users. in your case no such user exists so it can't be added to sharepoint.</p> <p> </p> <p>I think first you'd need to call the asp.net methods to add the user to sql/asp provider then call the sharepoint methods to import from sql/asp provider.... </p> <p> </p> <p>these are just my assumptions based on the error you're receiving.</p>Tue, 19 Jun 2007 19:52:08 Z2007-06-19T19:52:08Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#c530b3dc-240e-4f2a-92b9-3e38f5eb7a3ehttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#c530b3dc-240e-4f2a-92b9-3e38f5eb7a3eCurtis Ruppe _MicroStaff IT_http://social.msdn.microsoft.com/Profile/en-US/?user=Curtis%20Ruppe%20_MicroStaff%20IT_add users to sharepoint programmaticallyWhenever you create an aspnetdb based user account the username really isn't the typical username.  It becomes aspnet:username (or something similar).  Play around with adding a database-based user account to a SharePoint group manually, and then logging in as them to see their SharePoint UserInfo page (My Settings).  Hope this helps.Sun, 24 Jun 2007 16:33:37 Z2007-06-24T16:33:37Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#b4b0171d-0c3d-4a0e-834b-60078b984372http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#b4b0171d-0c3d-4a0e-834b-60078b984372DavidCrabbehttp://social.msdn.microsoft.com/Profile/en-US/?user=DavidCrabbeadd users to sharepoint programmatically<span style="font-family:Tahoma,Helvetica,Sans-Serif">I could be way off the mark with what you're trying to do, but I've added user accounts to SharePoint site groups using the following (where groupMembers is an ArrayList of SPUserInfo objects with details populates from</span><span style="font-family:Tahoma,Helvetica,Sans-Serif"> the user's AD entry</span><span style="font-family:Tahoma,Helvetica,Sans-Serif">):</span><br><br><span style="font-family:Courier New,Courier,Monospace">site.SiteUsers.AddCollection((SPUserInfo[])groupMembers.ToArray(typeof(SPUserInfo)));</span><br style="font-family:Courier New,Courier,Monospace"><span style="font-family:Courier New,Courier,Monospace">// add each user to the specified site group</span><br style="font-family:Courier New,Courier,Monospace"><span style="font-family:Courier New,Courier,Monospace">foreach (SPUserInfo member in groupMembers)</span><br style="font-family:Courier New,Courier,Monospace"><span style="font-family:Courier New,Courier,Monospace">{</span><br style="font-family:Courier New,Courier,Monospace"><span style="font-family:Courier New,Courier,Monospace"> site.SiteGroups[edSiteGroup.Text].AddUser(site.SiteUsers[member.LoginName]);</span><br style="font-family:Courier New,Courier,Monospace"><span style="font-family:Courier New,Courier,Monospace">}</span><br><br><span style="font-family:Tahoma,Helvetica,Sans-Serif">This depends upon the SharePoint groups exsiting at the site level (in the above code, the group name is taken from a form control: &quot;edSiteGroup&quot;).<br><br>Hope this helps,<br>David.<br><br><br style="font-family:Tahoma,Helvetica,Sans-Serif"></span><br style="font-family:Tahoma,Helvetica,Sans-Serif">Mon, 25 Jun 2007 04:14:21 Z2007-06-25T04:14:21Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#89539561-9614-4643-aede-9572413fd7bdhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#89539561-9614-4643-aede-9572413fd7bdAlexander Demurahttp://social.msdn.microsoft.com/Profile/en-US/?user=Alexander%20Demuraadd users to sharepoint programmatically<p>MembershipUser you create is not yet a sharepoint user, so you can't assign roles to it. Try the following:</p> <p> </p> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>SPWeb site = SPContext.Current.Site.RootWeb;</p> <p dir=ltr style="margin-right:0px">SPUser spUser = site.EnsureUser(username); //this creates an actual sharepoint user</p> <p dir=ltr style="margin-right:0px">//if (spUser == null) throw something to be sure;</p> <p dir=ltr style="margin-right:0px">SPRoleAssignment roleAssignment =<br>                        new SPRoleAssignment(spUser);  //use another constructor</p></div></div> <p>  </p> <p dir=ltr style="margin-right:0px">I faced this problem when I needed to add new user to a default visitors group, and that helped. I can provide you with some additional code if you need.</p> <p dir=ltr style="margin-right:0px"> </p> <p dir=ltr style="margin-right:0px">Good luck!</p>Wed, 27 Jun 2007 17:30:51 Z2007-06-27T17:30:51Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#d533e4da-5129-4bce-a2f2-65500fc7490chttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#d533e4da-5129-4bce-a2f2-65500fc7490crae bartonhttp://social.msdn.microsoft.com/Profile/en-US/?user=rae%20bartonadd users to sharepoint programmaticallyAlexander,<br><br>I have exactly same issue. I've configure my site as form-based authentication site. Now, I want to add users in a default visitor group upon their registration. Could you please shed some lights as to how to do so step-by-step? I've been looking for some guides for many days without such luck. If you're kind enough to send me some codes/instruction, my email address raekbarton@hotmail.com. Any help would be greatly appreciated.<br><br>Rae<br>Fri, 29 Jun 2007 17:10:00 Z2007-06-29T17:10:00Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#d0c74ec6-f947-4031-b110-f3f3c857b3efhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#d0c74ec6-f947-4031-b110-f3f3c857b3efAlexander Demurahttp://social.msdn.microsoft.com/Profile/en-US/?user=Alexander%20Demuraadd users to sharepoint programmatically<p><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2>Rae,</font></font></span></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2> </font></font></span></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2>As far it seems that you have already configured a membership provider for your sharepoint site, and you have forms authentication working. </font></font></span></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif" size=2> </font></span></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif" size=2>If not, please read here - </font><a title="http://weblog.vb-tech.com/nick/archive/2006/06/14/1617.aspx" href="http://weblog.vb-tech.com/nick/archive/2006/06/14/1617.aspx"><font face="Verdana, Geneva, Arial, Sans-serif" size=2>http://weblog.vb-tech.com/nick/archive/2006/06/14/1617.aspx</font></a></span></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"></span><font face="Verdana, Geneva, Arial, Sans-serif" size=2> </font></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif" size=2>So, then you all need is to create a user and add it to a default group, that's say &quot;VISITORS&quot;.</font></span></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"></span><font face="Verdana, Geneva, Arial, Sans-serif" size=2> </font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2></font><span lang=EN-US style="font-size:9.5pt;color:black"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%"><font face="Verdana, Geneva, Arial, Sans-serif" size=2>Code Snippet</font></span></div> <p><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2><span lang=EN-US style="font-size:9.5pt;color:black">//1)</span> Create membership user</font></font></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif" size=2>MembershipUser membershipUser = <font color="#2b91af">Membership</font>.CreateUser(login, password, email);  </font></span></p> <p><span lang=EN-US style="font-size:9.5pt;color:black"></span><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif" size=2>//now you have it</font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2> </font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2>//2) Create sharepoint user</font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2>SPWeb spWeb = <font color="#2b91af">SPControl</font>.GetContextWeb(<font color="#2b91af">HttpContext</font>.Current);</font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2>//get your site instance  </font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2>SPUser spUser = spWeb.EnsureUser(login);  </font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2>//now you have sharepoint user</font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2> </font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2>//3) Add sharepoint user to a desired group</font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2>SPGroup spGroup = spWeb.SiteGroups[<font color="#2b91af">&quot;VISITORS&quot;</font>];  </font></font></p> <p><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2>//get your group</font></p></font><font face="Verdana, Geneva, Arial, Sans-serif" size=2> <p>spGroup.AddUser(spUser);  </p> <p>//that should do the work</p></font></span></div></div> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2> </font></p></span> <p><font face="Verdana, Geneva, Arial, Sans-serif" size=2>That's all! </font><font face="Verdana, Geneva, Arial, Sans-serif" size=2>Of course some error-checking is nessesary.</font></p> <p><font face=Verdana size=2>If you have some questions, please write with some concrete questions.</font></p>Mon, 02 Jul 2007 14:47:58 Z2008-03-01T04:39:04Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#e8f60db0-35a3-4981-9032-9c9e25c32809http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#e8f60db0-35a3-4981-9032-9c9e25c32809zhljy711http://social.msdn.microsoft.com/Profile/en-US/?user=zhljy711add users to sharepoint programmaticallyHi Alexander,<br><br>I'm facing the same issue. My website has been configured as forms-based authentication website. By default, I'm not allowing anonymous access to my website. But I'll allow a visitor from internet can register his own accounts and put it into a specific group automatically. <br><br>Now there is no problems to create an asp.net user. But when I try to put this new user into a sp group, the web is redirected to the login page. This happens when it reaches the code <span><span class=txt4 id="_ctl0_MainContent__ctl0_PostForm_ReplyBody"><span lang=EN-US style="font-size:9.5pt;color:black"><font face="Verdana, Geneva, Arial, Sans-serif" size=2>spWeb.EnsureUser(login). I think there might be an authorization issue. I've been working on this for a whole week but no luck either.  Can you give me some suggestions on that? Thanks.<br><br></font></span></span></span>Wed, 04 Jul 2007 14:51:31 Z2007-07-04T14:51:31Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#94bbb05c-1249-4618-ab03-e6fa8781934ahttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#94bbb05c-1249-4618-ab03-e6fa8781934aCurtis Ruppe _MicroStaff IT_http://social.msdn.microsoft.com/Profile/en-US/?user=Curtis%20Ruppe%20_MicroStaff%20IT_add users to sharepoint programmaticallyUnless you want to mess with Impersonation context, you will not be able to do what you are looking to do.  The web object is under the context of the current user (anonymous), so in effect, if the user does not have the appropriate set of rights, they will be prompted to log in as a user who does.  Your web part / page might be able to call a web service with alternate credentials to perform the tasks you wish to do.Wed, 04 Jul 2007 15:12:15 Z2007-07-04T15:12:15Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#775f632c-ed38-4ff0-82b9-d662e7b85ba5http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#775f632c-ed38-4ff0-82b9-d662e7b85ba5Alexander Demurahttp://social.msdn.microsoft.com/Profile/en-US/?user=Alexander%20Demuraadd users to sharepoint programmatically<p><font size=2>Try running this code with higher privileges:</font></p> <p> </p><font size=2> <p>Microsoft.SharePoint.</font><font color="#2b91af" size=2>SPSecurity</font><font size=2>.RunWithElevatedPrivileges(</font><font color="#0000ff" size=2>delegate</p></font><font size=2> <p>{</p> <p>spWeb.EnsureUser(login);</p> <p>});</p> <p> </p> <p>You may need to add the following string to your web.config file.</p> <p>&lt;identity impersonate=&quot;true&quot; /&gt;</p> <p> </p> <p>Hope that will help you, </p> <p>good luck!</font></p>Thu, 05 Jul 2007 14:18:58 Z2007-07-05T14:18:58Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#36843904-0257-4f38-b914-59a8831c5a86http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#36843904-0257-4f38-b914-59a8831c5a86myfriedmindhttp://social.msdn.microsoft.com/Profile/en-US/?user=myfriedmindadd users to sharepoint programmatically<br>Remember, if you are using FBA, to prefix the Membership Provider Name before the &quot;username&quot; - ex<br><br>string _usernameWithProvider = String.Format(&quot;{0}:{1}&quot;, System.Web.Security.Membership.Provider.Name, username);<br><br><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">SPRoleAssignment roleAssignment =<br>                        new SPRoleAssignment(_usernameWithProvider, email, username, &quot;myNotes&quot;);<br><br>Also note that the LoginName and DisplayName are different due to the necc prefixing of the Provider.Name<br><br>Also, also note that you may need to have the AllowSafeUpdates=true on either Web, Site, or both (haven't tested this)<br><br>Also, also, also note &lt;g&gt; that the NTLM with throw an error on Provider.Name, so use for FBA<br><br>Also, also, also, also note (last one, I promise) that you should specify System.Web.Security.Membership.Provider, since MOSS has its own Membership.Provider which can cause confusion<br><br>good luck!<br>papabear<br></span></span>Mon, 25 Feb 2008 19:48:51 Z2008-02-25T19:48:51Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#e1d50e4a-f794-4a5f-9417-615f0c3dc0f2http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#e1d50e4a-f794-4a5f-9417-615f0c3dc0f2Erinc Arikanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Erinc%20Arikanadd users to sharepoint programmatically<p align=left><font face=Arial size=2>Nice post</font></p> <p align=left> </p> <p align=left>Also for anyone who is having problems with EnsureUser, has to add one user to site by using sharepoint add user functionality and should check account name. Generally it is in the form of groupname:username, so instead of trying:</p> <p align=left> </p> <p align=left>spWeb.EnsureUser(username);</p> <p align=left> </p> <p align=left>Try:</p> <p align=left> </p> <p align=left>spWeb.EnsureUser(&quot;whatevergroupnameitis:&quot;+username);</p> <p> </p> <p>That worked in my case.</p> <p> </p> <p>Good luck.</p> <p> </p> <p>Erinc Arikan</p>Thu, 13 Mar 2008 15:27:32 Z2008-03-13T15:27:32Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#6a284399-5420-48a8-8d12-bc956651afa7http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#6a284399-5420-48a8-8d12-bc956651afa7neeraja_godrej.comhttp://social.msdn.microsoft.com/Profile/en-US/?user=neeraja_godrej.comadd users to sharepoint programmatically<p align=left>Hey zhijy</p> <p> </p> <p align=left> </p> <p align=left>I m facing similar kind of issue.I am using Form authentication to access the site.</p> <p align=left>You have mentioned that an anonymous user can register himself and then put it into a specific group.Can you please explain the way this can be done??Can we do this in a single step(Registration and group allocation)??</p> <p align=left> </p> <p align=left>Thanks,</p> <p align=left>Neeraj</p> <p align=left> </p> <p align=left> </p> <p align=left> </p>Wed, 26 Mar 2008 11:26:47 Z2008-03-26T11:26:47Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#978b5c53-ddd1-424a-9e74-cfde9687b0f3http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#978b5c53-ddd1-424a-9e74-cfde9687b0f3khaled abrasshttp://social.msdn.microsoft.com/Profile/en-US/?user=khaled%20abrassadd users to sharepoint programmaticallyto solve this problem : The user does not exist or is not unique. <p align=left><font face=Arial size=2>and u r using forms authentications , u can use this code to add the user ( after adding it in membership):</font></p> <p align=left> </p> <p align=left>input : userName, fullName, groupName,email,website name</p> <p align=left> </p><font color="#0000ff" size=2> <p>using</font><font size=2> (</font><font color="#008080" size=2>SPSite</font><font size=2> site = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>SPSite</font><font size=2>(webSite))</p> <p>{</p> <p></font><font color="#0000ff" size=2>using</font><font size=2> (</font><font color="#008080" size=2>SPWeb</font><font size=2> web = site.OpenWeb())</p> <p>{</p> <p align=left>userName = </font><font color="#800000" size=2>&quot;scemembershipsqlprovider:&quot;</font><font size=2> + userName;</p> <p>web.SiteUsers.Add(userName, mail, fullName, </font><font color="#800000" size=2>&quot;Automatically Added User&quot;</font><font size=2>);</p> <p>web.Groups[&quot;GroupName&quot;].AddUser(userName, mail, fullName, </font><font color="#800000" size=2>&quot;Automatically Added User&quot;</font><font size=2>);</p> <p></p> <p>}</p> <p>}</p> <p align=left> </p> <p align=left>so u  should add ur membership provider before the login name...</p> <p> </p> <p align=left></font> </p>Tue, 15 Apr 2008 11:07:16 Z2008-04-15T11:07:16Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#dd9c6b68-5a48-4d4d-a91b-9ddaf699dabbhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#dd9c6b68-5a48-4d4d-a91b-9ddaf699dabbawadhhttp://social.msdn.microsoft.com/Profile/en-US/?user=awadhadd users to sharepoint programmatically<p>Hi to All,</p> <p align=left>   I m too facing exactly the same problem and  I tried all the posts on this subject in this thread with EXACTLY the same steps , but still its not doing the things with following observations..</p> <p align=left>1. FBA user is created by anonymous user.</p> <p align=left>2. But anonymous user is not able to add this newly created account to any SP groups and <em>this i think is due to fact that anonymous user doesn't hv ant rights to add any user directly to any SharePoint Group.</em></p> <p align=left><em></em> </p> <p align=left>So, CAN ANYONE PLZ TELL IF THIS REALLY POSSIBLE OR NOT , AND IF POSSIBLE PLZ POST WITH EXACT STEPS.</p> <p align=left> </p> <p align=left>Thanks in Advance,</p> <p align=left>awadh</p> <p align=left> </p> <p align=left> </p> <p align=left> </p> <p align=left><font face=Arial size=2></font> </p>Fri, 02 May 2008 12:16:55 Z2008-05-02T12:16:55Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#caa4f7ea-5adf-46ad-8885-46e9a288fb9bhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#caa4f7ea-5adf-46ad-8885-46e9a288fb9bawadhhttp://social.msdn.microsoft.com/Profile/en-US/?user=awadhadd users to sharepoint programmatically<p align=left><font face=Arial size=2></font> </p> <p>Hi <font color="#013da4">Alexander<strong> ,</strong></font></p> <p align=left><strong><font color="#013da4">   i tried exactly the same steps with proper FBA settings, still its not doing the things.</font></strong></p> <p align=left><strong><font color="#013da4">Can u plz help me to get out of this problem ?</font></strong></p>Fri, 02 May 2008 12:19:45 Z2008-05-02T12:19:45Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#fcdab465-df43-4131-bb21-d614c1efa94ahttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#fcdab465-df43-4131-bb21-d614c1efa94axor_freecityhttp://social.msdn.microsoft.com/Profile/en-US/?user=xor_freecityadd users to sharepoint programmaticallyI suggest you add a asp.net role to the sharepoint group when the site is setup. At runtime, you can simply add membership users to this role when the user is registered. This is much easier to program and don't need elevated privileges.<br>Sat, 20 Dec 2008 15:46:16 Z2008-12-20T15:46:16Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#e80d15e1-7cdb-403d-98d6-6f97ceb653c0http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#e80d15e1-7cdb-403d-98d6-6f97ceb653c0任国强http://social.msdn.microsoft.com/Profile/en-US/?user=%u4efb%u56fd%u5f3aadd users to sharepoint programmatically<p>the best solution is <br><font size=2><span class=RadEWrongWord>step1</span>.    RunWithElevatedPrivileges when <span class=RadEWrongWord>ensureuser</span> and <span class=RadEWrongWord>adduser</span><br><span class=RadEWrongWord>step2</span>.    you should <br>        a. You can update data for a single site or site collection by adding a page directive and <font color="#0033cc"><span class=RadEWrongWord>FormDigest</span></font> control to the page that makes the request.<br>        <br>        b. Include a <b><span class=RadEWrongWord>FormDigest</span></b> control within the form as follows:</font></p><hr class="sig">任国强Tue, 03 Feb 2009 13:14:53 Z2009-02-03T13:14:53Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#993dfad3-8f8b-4fff-9f14-423603e742f1http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#993dfad3-8f8b-4fff-9f14-423603e742f1任国强http://social.msdn.microsoft.com/Profile/en-US/?user=%u4efb%u56fd%u5f3aadd users to sharepoint programmatically<a href="http://msdn.microsoft.com/en-us/library/ms472879.aspx">http://msdn.microsoft.com/en-us/library/ms472879.aspx</a><hr class="sig">任国强Tue, 03 Feb 2009 13:16:05 Z2009-02-03T13:16:05Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#e77255d0-399c-4cc3-b262-5ce45f0928b7http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#e77255d0-399c-4cc3-b262-5ce45f0928b7Amreesh Sharmahttp://social.msdn.microsoft.com/Profile/en-US/?user=Amreesh%20Sharmaadd users to sharepoint programmatically<p>Hi All,</p> <p>I have a publishing site in sharepoint. i configured forms authentication for this site using asp.net web application. it wass successfull. i added these roles in the SharePoint Groups and gave appropriate Approve permission to the group. i created different users and assigned them the roles in asp.net web application. Now  if i edit a page and start a workflow(serial workflow) as an administrative user and then i login as a normal user under the role which i have added in the sharepoint group, i am not able to see the &quot;Approve&quot; &quot;Reject&quot; and &quot;View Page Tasks&quot; on the home page. I tried by configuring the Forms Authentication for SSP and MySite also which i think not needed in this scenario. </p> <p>If i add a user uder this role in the sharePoint Group then he is able to see all the Approve , Reject and 'View page taks&quot; buttons but not when i add a role in the SharePoint Group. Please give the solution immediately. its very urgent. Any help regarding this will be highly appreciated.</p> <p>i checked the WorkFlow task list permissions and permissions of the workflow. but i am not able to find the solution after so many attempts. please suggest appropriate ideas and solutions...</p> <p>Thanks and Regards<br>Amreesh Sharma</p>Fri, 27 Feb 2009 07:29:09 Z2009-02-27T07:29:09Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#f32570ff-425e-4585-8d13-1dc111dbc872http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959#f32570ff-425e-4585-8d13-1dc111dbc872Sridebi Koradahttp://social.msdn.microsoft.com/Profile/en-US/?user=Sridebi%20Koradaadd users to sharepoint programmaticallyHi All,<br/>   I have a sharepoint site which is form authenticated. In my 'Create User' form I would like to add user to membership database, if it is successful I add that user to user information list.<br/><br/>When I was trying with the code above it did not work for me. at last I could able to get the solution. <br/><br/><strong>The username in the membership database is not a AliasName in sharepoint.<br/><br/></strong>The login name in sharepoint is 'name of membershipProvider':'Username.<br/><br/>For example if<br/>User Name is : Sridebi.Korada<br/>MembershipProviderName is : MembershipProvider<br/><br/>then the login Name for sharepoint will be : MembershipProvider:Sridebi.Korada<br/><br/>The code snippet for adding new user will be as follows<br/><br/><span style="font-size:x-small"><font size=2> <p> </p> </font></span> <p><span style="font-size:x-small;color:#2b91af">SPSite</span><span style="font-size:x-small"> site = </span><span style="font-size:x-small;color:#2b91af">SPContext</span><span style="font-size:x-small">.Current.Site;<font size=2> <p> </p> </font></span></p> <p><span style="font-size:x-small;color:#2b91af">SPWeb</span><span style="font-size:x-small"> web = site.RootWeb;<font size=2> <p> </p> </font></span></p> <p><span style="font-size:x-small;color:#2b91af">SPGroup</span><span style="font-size:x-small"> addUserGroup = web.Groups[</span><span style="font-size:x-small;color:#a31515">&quot;ReadiNow Members&quot;</span><span style="font-size:x-small">];</span></p> <span style="font-size:x-small"><font size=2> <p>addUserGroup.AddUser(</p> </font></span> <p><span style="font-size:x-small;color:#0000ff">string</span><span style="font-size:x-small">.Format(</span><span style="font-size:x-small;color:#a31515">&quot;MembershipProvider:Sridebi.Korada&quot;,</span><span style="font-size:x-small"><span style="color:#a31515">&quot;SKorada.abc.com&quot;</span>, <span style="color:#a31515">&quot;Sridebi.Korada&quot;</span>, </span><span style="font-size:x-small;color:#a31515">&quot;&quot;</span><span style="font-size:x-small">);<br/></span></p> <br/><br/>Try this code if you fail to add user to UI list in sharepoint site if it is form authenticated.<br/><br/><br/>Thanks and Regards<br/>Sridebi Korada<br/><br/>Mon, 25 May 2009 05:43:34 Z2009-05-25T05:43:34Z