如何在WSS中使用Microsoft Enterprise Library

創建Sharepoint空項目:

clip_image002[7]

創建了一個空的項目文件:

clip_image002[9]

添加引用:

clip_image002

選擇要引用的DLL:

clip_image002[16]

添加引用的DLL:

clip_image002[18]

繼續添加Sharepoint.dll的引用:

clip_image002[20]

下載Microsoft的Enterprise Library 4.1版本的Hands On Labs的安裝文件,因爲 Hands On Labs的DLL文件是強簽名的,

下載地址是:http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=ab3f2168-fea1-4fc2-b40c-7867d99d4b6a

而Enterprise Library 4.1的源碼編譯出來的DLL不是強簽名的。要想在Sharepoint裏面使用Enterprise Library的DLL,這些DLL必須是強簽名的才行;

clip_image002[22]

用默認的選項安裝了Enterprise Library for .NET Framework 3.5(v4.1) Hands On Labs後,安裝包會把相關的DLL拷貝到以下目錄:

C:/Enterprise Library 4.1 HOL/Lib

clip_image002[24]

繼續給工程文件添加Enterprise Library 4.1的這兩個DLL:

clip_image002[26]

添加了Enterprise Library的DLL後的工程文件:

clip_image002[28]

用右鍵查看Microsoft.Practices.EnterpriseLibray.Data這個DLL的屬性,可以看到這個DLL是強簽名的:

clip_image002[30]

接着在工程裏面按照以下目錄創建存放我們要創建的*.aspx文件的文件夾:

clip_image002[32]

選擇新建文件菜單:

clip_image002[34]

選擇以下文件類型:

clip_image002[36]

創建後的aspx文件:

clip_image002[38]

按VS2008工具欄上面的保存按鈕,以Orders.aspx命名保存這個文件到工程裏面的EnLibWss目錄:

clip_image002[40]

然後把Orders.aspx文件添加到工程裏面來:

clip_image002[42]

clip_image002[44]

在VS2008裏面打開這個Orders.aspx文件:

clip_image002[46]

 

把默認的內容換成以下代碼,主要是要使Orders.aspx這個頁面要使用到application.master這個母版頁:





   
   
   


    Enterprise Library 4.1 & Sharepoint       


    Enterprise Library 4.1 & Sharepoint

clip_image002[48]

給Orders.aspx創建CodeBehind類:

clip_image002[50]

clip_image002[52]

把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;

 

            }

 

        } 

 

    }

 

}

clip_image002[54]

 

分別把引用裏面的Microsoft.Practices.EnterpriseLibrary.Common.dll和Microsoft.Practices.EnterpriseLibrary.Data.dll這兩個DLL的“複製本地”屬性改爲:True

clip_image002[56]

在工程裏面創建一個名爲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%

clip_image002[58]

然後在工程文件的屬性頁裏面的“生成事件”裏面增加這兩行命令:

cd $(ProjectDir)

install.bat

這樣就可以在每次編譯後,VS2008把相關文件拷貝到Sharepoint的目錄裏面;

clip_image002[60]

現在編譯工程文件,Install.bat文件自動把相關的文件拷貝的Sharepoint的網站目錄下面:

clip_image002[62]

clip_image002[64]

打開WSS的網站的虛擬目錄裏面的Web.config文件:

image

增加數據庫鏈接字符串的配置內容:(我這裏用到SQL Server 2005的示例數據庫AdventureWorks)


          providerName="System.Data.SqlClient" />

image

然後增加Enterprise Library DLL的引用內容:

 

image

保存Web.config文件後在IE瀏覽器裏面打開我們創建的那個Order.aspx頁面,可以看到,Enterprise Library能夠運行該SQL,並返回相應的數據顯示在GridView控件裏面:

clip_image002[66]

當然,以上只是把Enterprise Library 4.1引入WSS環境的一個簡單的方法,真正的業務系統,應該將工程分爲業務層、表現層、數據存儲層等多層的架構,然後再將系統用以上方法部署到WSS環境中去;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章