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/