自定义ASP.NET WebApplication中调用SharePoint2010的对象

时间:2022-07-14 19:55:19

如果你是做SharePoint开发的话,一定不会对如下这段代码陌生:

using(SPSite oSiteCollection = new SPSite("http://Server_Name"))
{
using(SPWeb oWebsite = oSiteCollection.OpenWeb("Website_URL"))
{ }
}

你会在自定义webPart, 或者是一个控制台程序中写这段代码。那么你有没尝试过在ASP.NET中写这段代码?譬如说使用SPGridView控件的时候,与其在WebPart中调试,是不是没有在ASP.NET的Web Application中调试方便?

在SharPoint 2007的时候这段代码是可以在Web Application程序中运行的,但是在SharePoint 2010 中,你就没有那么幸运了。在第一行代码中,你得到的就是“web应用程序找不到”(我的是英文环境,错误:The Web application at 'http://Server_Name could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application)

接下来很多的人都会想到,在VS2010中,把Framework换成3.5,以及平台由X86换成“Any CPU”,但是你的问题依旧不会解决。这个方案仅仅适合控制台程序。

结论: IIS Express不完全支持64bit。

论据:http://learn.iis.net/page.aspx/901/iis-express-faq/

Q: Are 64-bit machines supported?

A: The first official release of IIS Express will support 64-bit architectures using the WoW64 subsystem. Full 64-bit support will be considered for future releases.

论证过程:

1.这段代码在调用SharePoint 2007对象的时候可以成功,但是在调用SharePoint 2010的时候失败, 二者主要的区别是后者是64bit的;

2.调用SharePoint2010对象的时候,你在控制台程序中可以成功,在web应用程序中却失败,说明SharePoint 2010的这些对象是可以在外部调用的;

3.如果你将你的web应用程序发布成IIS站点之后,代码是可以运行的。区别:VS2010调试的时候使用的是IIS Express, 而发布的时候是IIS。

解决方案:

如果你想在vs2010创建的Web Application中调试SharePoint 2010 的代码,那么你就的告诉VS2010调试这段代码的时候不要使用默认的服务器,而要使用本地的IIS. 如下图:

自定义ASP.NET WebApplication中调用SharePoint2010的对象