0

I'm working on a console application in VS2012 installed on my SP2013 dev server. The app is called from a command line and sets up a new FBA account using info provided by the user. When it reaches the code snippet to put the new account in a SharePoint group, I get the error

"The web application at http://sub.company.com/ could not be found. Verify that you have typed the URL correctly. If the ..."

Here's what I've checked:

  • The console app is configured for .Net 4, any CPU.
  • Both the dev and production servers are running the same build version of SharePoint 2013.
  • I've confirmed that the URL is correct and that I can access the site in the dev server's browser (IE11).
  • I've also confirmed that the AAM settings are correct for this web application. Our sites are all on port 80 and use host headers, so I can't try server:port directly.

The only thing I've been able to find so far is several search results that state your application has to be on the same server farm as the web application. That seems kind of ridiculous, though, since staging the app properly requires you to test everything exactly as it is in production. So, I have two questions:

  1. Is it true that your app has to be on the same farm as the web app context you're trying to open in your code?
  2. If not and it's a problem with my code, what is the proper way to do what I'm attempting here?

Here's the code snippet in question:

Console.WriteLine("Updating account properties...");

string mySite = "http://sub.company.com/";

using (SPSite elevatedSite = new SPSite(mySite))
   {
      using (SPWeb elevatedWeb = elevatedSite.OpenWeb())
      {
         // Add new account to security group on app site
         SPUser myUser = elevatedSite.RootWeb.EnsureUser("i:0#.f|CustomMembershipProvider|" + newUsername);
         SPGroup myGroup = elevatedSite.RootWeb.Groups["My SharePoint Group"];
         myGroup.AddUser(myUser);
         myGroup.Update();
      }
   }
2

Change

The console app is configured for .Net 4, any CPU.

to

The console app is configured for .Net 4, X64.

Also, as Amal said, run the console as administrator in the same box as you have SharePoint installed.

Also, Run with Elevated Priviledges in a console does nothing. RWEP only removes the Impersonation and goes back to the Account that is running the process, in the console application, there is no Impersonation. In this case, the account running the console should have access to SharePoint_Config database and any content database/service application you plan to access/modify through the API.

  • Hmm - good to know about RWEP. The EXE will be called from an AutoCAD extension developed by someone else, so I assume it will be running under the account of each individual user, which wouldn't have rights to perform the operation on the site. I'll look into a different way of doing it then. Thanks! – Omegacron Jan 20 '15 at 15:06
  • don't forget that the Microsoft.SharePoint.dll adds the pre-requisite of the code being run on the SP box. So unless you have Autocad installed in your sharepoint server, it's no good. Have you considered creating a web service to receive the information and having hte web service do the things the extension wants done in SP? – Choggo Jan 20 '15 at 18:32
1

If you are using Server Side Code then your application should run from the Same FARM.

Else you should use Client Side Object Model.

1

Related: SharePoint object model: connect to remote server?

  1. Is it true that your app has to be on the same farm as the web app context you're trying to open in your code?

Yes it is true as Amal and the link above said.

  1. If not and it's a problem with my code, what is the proper way to do what I'm attempting here?

You can use CSOM, or alternatively, add proper logging to the code and copy the .exe file from output of the console application to the production. You can also use Visual Studio remote debugging (only if that is an option in production!)

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.