0
votes

Warning: I'm an asp.net developer taking my first steps in SharePoint.

So, i'm writing a console application that connects to a SharePoint Server 2007 site on the same machine, but it seems that something goes wrong during the call to SPSite() constructor. Here's the simplified code:

using (SPSite siteCollection = new SPSite("http://server/AwesomeSite"))
{

  //when i set a breakpoint on this line and inspect the siteCollection object, 
  //most of it's properties (for example siteCollection.AllWebs.Names) throw an 
  //exception, which leads me to the conclusion that something went wrong during 
  //the constructor above.

  SPWebCollection site = siteCollection.AllWebs;
  SPWeb web = site[""];
  SPList list = web.Lists["AwesomeList"]; //exception occurs here
}

The SPException text:

Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))

I followed the advice of Sharepoint SPSite and checked that:

  1. The user is a server farm administrator.
  2. The user has Read and Write permissions on the content database.
  3. The user is a site collection administrator.
  4. The user has permissions to access the Windows SharePoint Services site or the SharePoint Server 2007 site through which the code iterates.

And they are all correct. What else could be causing this to happen?

5

5 Answers

4
votes

In my experience, the SPSite() constructor is highly dependent on the Alternate Access Mappings configuration of your site. Make sure that the URL you are using in the constructor appears in the list of mappings (e.g., http vs. https, machine vs. FQDN).

2
votes

You need to get more debug information.

Using Visual Studio

Try setting the debugger to break on all exceptions. Go to Debug, Exceptions and tick Common Language Runtime Exceptions. Then go to Tools, Options, Debugging and untick Enable Just My Code. Finally attach to w3wp.exe. Try running your console application now and you should find that it triggers an exception in w3wp.exe. Check the stack trace and see if that gives you more information.

Using dump files

You could also try working from a crash dump. This is admittedly significantly more hard-core but should give you the detail you are otherwise lacking. The tool ProcDump will can be attached to w3wp.exe (provided the -s switch isn't used) and will create a dump if an unhandled exception occurs. If you have trouble with ProcDump, try ADPlus which does something similar.

From the created dump file, use this KB article to set up WinDbg and get started. There is an example case of how to use WinDbg on Tess Ferrandez's blog (Strategy #2).

1
votes

Have you tried to run the code with elevated privileges?

Does the IIs have some kind of funky settings regarding authentication? (Try Windows auth. only)

1
votes

Unfortunately, there are hundreds of ways to generate this error. Just ask Google.

You might consider using SPTraceView to get a better description of the error. Here's a description of the tool and an example working an issue with it.

Good luck!

0
votes

I have similar (not equals) problem. I've solved it with this piece of code:

using( SPSite site = ConnectToSharepointAsSystem() ) {
   // now should be all ok
}

// somewhere in helper class ->
public static SPUserToken GetSystemToken(SPSite site) {
    SPUserToken token = null;
    bool tempCADE = site.CatchAccessDeniedException;
    try {
        site.CatchAccessDeniedException = false;
        token = site.SystemAccount.UserToken;
    }
    catch (UnauthorizedAccessException) {
        SPSecurity.RunWithElevatedPrivileges(() => {
            using (SPSite elevSite = new SPSite(site.ID))
                token = elevSite.SystemAccount.UserToken;
        });
    }
    finally {
        site.CatchAccessDeniedException = tempCADE;
    }
    return token;
}

public static Microsoft.SharePoint.SPSite ConnectToSharepoint() {
    string urlSharepointSite;
    var ret = ConnectToSharepoint(out urlSharepointSite);
    return ret;
}
public static Microsoft.SharePoint.SPSite ConnectToSharepoint(out string urlSharepointSite) {
    urlSharepointSite = "http://www.domain.org";
    var site = new Microsoft.SharePoint.SPSite( urlSharepointSite );
    return site;
}
public static Microsoft.SharePoint.SPSite ConnectToSharepointAsSystem() {
    string urlSharepointSite;
    Microsoft.SharePoint.SPUserToken userToken = null;
    using (var tmpSite = CSharepointNastroje.PripojitNaSharepoint( out urlSharepointSite )) {
        userToken = GetSystemToken(tmpSite);
    }
    var site = new Microsoft.SharePoint.SPSite(urlSharepointSite, userToken);
    return site;
}