創建Sharepoint空項目:
創建了一個空的項目文件:
添加引用:
選擇要引用的DLL:
添加引用的DLL:
繼續添加Sharepoint.dll的引用:
下載Microsoft的Enterprise Library 4.1版本的Hands On Labs的安裝文件,因爲 Hands On Labs的DLL文件是強簽名的,
而Enterprise Library 4.1的源碼編譯出來的DLL不是強簽名的。要想在Sharepoint裏面使用Enterprise Library的DLL,這些DLL必須是強簽名的才行;
用默認的選項安裝了Enterprise Library for .NET Framework 3.5(v4.1) Hands On Labs後,安裝包會把相關的DLL拷貝到以下目錄:
C:/Enterprise Library 4.1 HOL/Lib
繼續給工程文件添加Enterprise Library 4.1的這兩個DLL:
添加了Enterprise Library的DLL後的工程文件:
用右鍵查看Microsoft.Practices.EnterpriseLibray.Data這個DLL的屬性,可以看到這個DLL是強簽名的:
接着在工程裏面按照以下目錄創建存放我們要創建的*.aspx文件的文件夾:
選擇新建文件菜單:
選擇以下文件類型:
創建後的aspx文件:
按VS2008工具欄上面的保存按鈕,以Orders.aspx命名保存這個文件到工程裏面的EnLibWss目錄:
然後把Orders.aspx文件添加到工程裏面來:
在VS2008裏面打開這個Orders.aspx文件:
把默認的內容換成以下代碼,主要是要使Orders.aspx這個頁面要使用到application.master這個母版頁:
Enterprise Library 4.1 & Sharepoint
Enterprise Library 4.1 & Sharepoint
給Orders.aspx創建CodeBehind類:
把Orders.cs文件的內容改爲如下,使Orders類繼承:LayoutsPageBase 類;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace EnLibWss
{
public class Orders : LayoutsPageBase
{
protected GridView gvOrders;
protected TextBox tbMsg;
protected override void OnLoad(EventArgs e)
{
try
{
Database db = DatabaseFactory.CreateDatabase("MyConnectionString");
DataSet dsCustomer = db.ExecuteDataSet(CommandType.Text, " SELECT TOP 10 * FROM Sales.SalesOrderHeader ");
gvOrders.DataSource = dsCustomer.Tables[0].DefaultView;
gvOrders.DataBind();
}
catch (Exception ex)
{
tbMsg.Text = ex.Message;
}
}
}
}
分別把引用裏面的Microsoft.Practices.EnterpriseLibrary.Common.dll和Microsoft.Practices.EnterpriseLibrary.Data.dll這兩個DLL的“複製本地”屬性改爲:True
在工程裏面創建一個名爲intall.bat的批處理文件,並添加以下內容到install.bat文件裏面,保存:
@SET TEMPLATEDIR="c:/program files/common files/microsoft shared/web server extensions/12/Template"
@SET STSADM="c:/program files/common files/microsoft shared/web server extensions/12/bin/stsadm"
@SET GACUTIL="C:/Program Files/Microsoft SDKs/Windows/v6.0A/Bin/gacutil.exe"
IISRESET
REM cscript c:/windows/system32/iisapp.vbs /a "SharePointDefaultAppPool" /r
Echo Installing EnLibWss.dll in GAC
%GACUTIL% -if bin/debug/EnLibWss.dll
Echo Installing Microsoft.Practices.EnterpriseLibrary.Common.dll in GAC
%GACUTIL% -if bin/debug/Microsoft.Practices.EnterpriseLibrary.Common.dll
Echo Installing Microsoft.Practices.EnterpriseLibrary.Data.dll in GAC
%GACUTIL% -if bin/debug/Microsoft.Practices.EnterpriseLibrary.Data.dll
Echo Copying files
xcopy /e /y TEMPLATE/* %TEMPLATEDIR%
然後在工程文件的屬性頁裏面的“生成事件”裏面增加這兩行命令:
cd $(ProjectDir)
install.bat
這樣就可以在每次編譯後,VS2008把相關文件拷貝到Sharepoint的目錄裏面;
現在編譯工程文件,Install.bat文件自動把相關的文件拷貝的Sharepoint的網站目錄下面:
打開WSS的網站的虛擬目錄裏面的Web.config文件:
增加數據庫鏈接字符串的配置內容:(我這裏用到SQL Server 2005的示例數據庫AdventureWorks)
providerName="System.Data.SqlClient" />
然後增加Enterprise Library DLL的引用內容:
保存Web.config文件後在IE瀏覽器裏面打開我們創建的那個Order.aspx頁面,可以看到,Enterprise Library能夠運行該SQL,並返回相應的數據顯示在GridView控件裏面:
當然,以上只是把Enterprise Library 4.1引入WSS環境的一個簡單的方法,真正的業務系統,應該將工程分爲業務層、表現層、數據存儲層等多層的架構,然後再將系統用以上方法部署到WSS環境中去;