1

I am working on Sharepoint and I am trying to save all the users from Active Directory into the Sharepoint under same group. If I run my code then I get this error:

The Web application at http://localhost/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

string groupname = txtgroupname.Text;
Uri sitePath = new Uri(@"http://localhost");
List<ADUserDetail> users = new List<ADUserDetail>();
users = BuildList(users, "LDAP://server-abc/dc=abc,dc=bb,dc=ca", groupname);
foreach (var adUser in users)
{
 using (SPSite spSite = new SPSite(sitePath.ToString()))
 {
  using (SPWeb spWeb = spSite.OpenWeb())
  {
    spWeb.AllowUnsafeUpdates = true;
    SPUser spUser = spWeb.EnsureUser(adUser.Samaccountname);
    if (spUser != null)
    {
     SPGroup spGroup = spWeb.SiteGroups[groupname];
     if (spGroup != null)
      spGroup.AddUser(spUser);
    }
   }
  }
 }

Dot Net version 4

Server is 64 bit (Any CPU)

1

Make sure that you are running your code from the server as an administrator. Also replace Localhost with the actual Web Application URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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