自定義ASP.NET WebApplication中調用SharePoint2010的對象
如果你是做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. 如下圖:

浙公網安備 33010602011771號