0

I am working on creating a web service that can access certain SharePoint APIs that will take the content from a certain WSS database and create a CMP export to read from. I have successfully been able to do this programmatically in C# with a regular Windows Forms application, as long as the program is running on the SharePoint server and under the farm administrator account. However, the problem occurs, when I try to run the same exact code from my ASP.NET web service. The error that occurs under the web service is "Object reference not set to an instance of an object." with a System.NullReferenceException.

See the code below:

[WebMethod]
public bool SPRetreiveCMPTest()
{
    try
    {
        SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder("Data Source=sql1\\sharepoint;Initial Catalog=WSS_Content_Sales_Test;Integrated Security=SSPI");
        SPContentDatabase spcd = SPContentDatabase.CreateUnattachedContentDatabase(scsb); // <-- ERROR OCCURS HERE!!

        SPExportObject speo = new SPExportObject();
        speo.Url = "http://sharepoint/sites/sales/productmgmt/Product Management";
        speo.IncludeDescendants = SPIncludeDescendants.All;
        speo.Type = SPDeploymentObjectType.List;

        SPExportSettings spes = new SPExportSettings();
        spes.UnattachedContentDatabase = spcd;
        spes.SiteUrl = "http://sharepoint1:8080/sites/sales";
        spes.FileLocation = "C:\\users\\sp_farm\\test_restore";
        spes.LogFilePath = "C:\\users\\sp_farm\\test_restore\\test.log";
        spes.BaseFileName = "testout";
        spes.ExportObjects.Add(speo);
        spes.IncludeVersions = SPIncludeVersions.All;
        spes.LogExportObjectsTable = false;
        spes.IncludeSecurity = SPIncludeSecurity.All;

        using (SPExport spe = new SPExport(spes))
            spe.Run();
    }
    catch (Exception e) { return false; }

    return true;
}

I have made sure the application pool that the web service is running under is using the farm administrator account. Any ideas as to why this error is happening?

EDIT:

Fixed it! I started following the stack trace below and I realized it was a two-part fix.

First stack trace:

at Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest()
at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
at Microsoft.SharePoint.Administration.SPPersistedChildCollection`1.Add(T newObj, Boolean ensure)
at Microsoft.SharePoint.Administration.SPPersistedChildCollection`1.Ensure(T newObj)
at Microsoft.SharePoint.Administration.SPContentDatabase.CreateUnattachedContentDatabase(String databaseInstanceServer, String databaseName, String username, String password)
at PHSSPWebService.PHSSPWebServiceClass.SPRetreiveCMPTest() in D:\\My Documents\\Visual Studio 2008\\Projects\\PHS\\PHSSPWebService\\PHSSPWebService\\PHSSPWebService.asmx.cs:line 100"

1.) I had to enable Forms Authentication in IIS for my web services web site and a new exception (InvalidOperationException) and stack trace appeared.

Second stack trace:

at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
at Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest()
at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
at Microsoft.SharePoint.Administration.SPPersistedChildCollection`1.Add(T newObj, Boolean ensure)
at Microsoft.SharePoint.Administration.SPPersistedChildCollection`1.Ensure(T newObj)
at Microsoft.SharePoint.Administration.SPContentDatabase.CreateUnattachedContentDatabase(String databaseInstanceServer, String databaseName, String username, String password)
at PHSSPWebService.PHSSPWebServiceClass.SPRetreiveCMPTest() in D:\\My Documents\\Visual Studio 2008\\Projects\\PHS\\PHSSPWebService\\PHSSPWebService\\PHSSPWebService.asmx.cs:line 100"

2.) The fix for SPWebEnsureSPControl(HttpContext context) was to set HttpContext.Current = null at the start of my code and voila, it worked!

See here for further detail: http://johnatjornata.wordpress.com/2012/01/23/runwithelevatedprivileges-operation-is-not-valid-due-to-the-current-state-of-the-object-error-explained/

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Zac
  • 2,325
  • 3
  • 23
  • 33
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders May 11 '12 at 15:48
  • I don't believe this is a duplicate question, John. The program works fine when it runs just as a normal windows form application on the SharePoint server. The problem seems to lie in the web service and possibly the IIS Application Pool it is running under. I've made sure the app pool is running under the farm administrator and the right credentials are being used but the same error occurs in the service. – Zac May 11 '12 at 16:15
  • Now that you've posted the stack trace, I can see that this is not a duplicate. Hint: next time, post the stack trace right away. – John Saunders May 11 '12 at 19:34

1 Answers1

0

I have the fix in the question. After following the stack trace all the way through, I found I needed to turn on Forms Authentication in my IIS Web Site and then set HttpContext.Current to null.

Zac
  • 2,325
  • 3
  • 23
  • 33