创建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类:
![clip_image002[50] clip_image002[50]](http://hi.csdn.net/attachment/201010/9/0_12865936985Kp1.gif)
把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
![clip_image002[56] clip_image002[56]](http://hi.csdn.net/attachment/201010/9/0_1286593702tQst.gif)
在工程里面创建一个名为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] clip_image002[58]](http://hi.csdn.net/attachment/201010/9/0_128659370456C3.gif)
然后在工程文件的属性页里面的“生成事件”里面增加这两行命令:
cd $(ProjectDir)
install.bat
这样就可以在每次编译后,VS2008把相关文件拷贝到Sharepoint的目录里面;
![clip_image002[60] clip_image002[60]](http://hi.csdn.net/attachment/201010/9/0_1286593706sbBs.gif)
现在编译工程文件,Install.bat文件自动把相关的文件拷贝的Sharepoint的网站目录下面:
![clip_image002[62] clip_image002[62]](http://hi.csdn.net/attachment/201010/9/0_1286593708k3Jr.gif)
打开WSS的网站的虚拟目录里面的Web.config文件:
增加数据库链接字符串的配置内容:(我这里用到SQL Server 2005的示例数据库AdventureWorks)
providerName="System.Data.SqlClient" />
然后增加Enterprise Library DLL的引用内容:
保存Web.config文件后在IE浏览器里面打开我们创建的那个Order.aspx页面,可以看到,Enterprise Library能够运行该SQL,并返回相应的数据显示在GridView控件里面:
![clip_image002[66] clip_image002[66]](http://hi.csdn.net/attachment/201010/9/0_12865937106tLD.gif)
当然,以上只是把Enterprise Library 4.1引入WSS环境的一个简单的方法,真正的业务系统,应该将工程分为业务层、表现层、数据存储层等多层的架构,然后再将系统用以上方法部署到WSS环境中去;