我正在创建一个 Web 服务,该服务可以访问某些 SharePoint API,这些 API 将从某个 WSS 数据库中获取内容并创建一个 CMP 导出以供读取。只要该程序在 SharePoint 服务器上和场管理员帐户下运行,我就可以在 C# 中使用常规 Windows 窗体应用程序成功地以编程方式执行此操作。但是,当我尝试从我的 ASP.NET Web 服务运行相同的代码时,就会出现问题。Web 服务下出现的错误是“对象引用未设置为对象的实例”。带有 System.NullReferenceException。
请看下面的代码:
[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;
}
我已确保运行 Web 服务的应用程序池正在使用场管理员帐户。关于为什么会发生此错误的任何想法?
编辑:
解决它!我开始关注下面的堆栈跟踪,我意识到这是一个由两部分组成的修复。
第一个堆栈跟踪:
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.) 我必须在 IIS 中为我的 Web 服务网站启用表单身份验证,并且出现了一个新异常 (InvalidOperationException) 和堆栈跟踪。
第二个堆栈跟踪:
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.)解决方法SPWebEnsureSPControl(HttpContext context)
是HttpContext.Current = null
在我的代码开始时设置,瞧,它起作用了!
有关详细信息,请参见此处:http: //johnatjornata.wordpress.com/2012/01/23/runwithelevatedprivileges-operation-is-not-valid-due-to-the-current-state-of-the-object-error-解释/