具有 ASP.NET Web 服务的 SharePoint API 例外
本文关键字:SharePoint API 例外 服务 ASP NET Web 具有 | 更新日期: 2023-09-27 18:34:04
我正在创建一个可以访问某些SharePoint API的Web服务,该服务将从某个WSS数据库中获取内容并创建要从中读取的CMP导出。 我已经成功地使用常规 Windows 窗体应用程序在 C# 中以编程方式执行此操作,只要该程序在 SharePoint 服务器上和服务器场管理员帐户下运行。 但是,当我尝试从我的 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-explained/
我在问题中有解决方法。 在完全遵循堆栈跟踪之后,我发现我需要在我的 IIS 网站中打开窗体身份验证,然后将HttpContext.Current
设置为 null
。