在SharePoint【学习笔记】--【Sandbox Solution】-- Full Trust Proxy--开发步骤一文,我们讲述了开发和部署Sandbox Solution的Full Trust Proxy的基本步骤,在这里,我们采用另一种方式来开发和部署一个能访问数据库的Full Trust Proxy,由于内容比较多,所以分割成两个部分,本部分主要讲如何开发这个Full Trust Proxy,而下一部分则讲如何在Webpart中调用它来展示所取得的数据库数据。
直接进入操作步骤。
一、创建和设置项目
1、在Vs2010中新建一个Empty SharePoint Project,命名为: MyTestSandBoxAccessDBInfo,由于是开发Full Trust Proxy,所以此项目要基于Farm开发,
我们用于测试的Sharpoint网站网址是 http://sd1-sp1dev:5000/sites/Develop/
。
2、在Solution Explorer中,选择项目名并点击鼠标右键,在弹出菜单中选择Properties,从而打开项目设置窗口
2.1在此窗口中选择Application栏,设置Default namespace和Assembly name两项,分别为My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo和My.Sharepoint.SandBox,如下图
。
2.2在此窗口中选择Sharepoint栏,设置Post-deployment CommandLine内容:
net stop SPUsercodeV4
net start SPUsercodeV4
它的主要目的是保证我们在每一次修改此full trust proxy并形成和发布其新的版本时,自动重新启动Microsoft SharePoint Foundation Sandboxed Code service,以使新的改动在Sharepoint网站中生效。
。
3、在Solution Explorer中,双击并打开Assembly.info,在此文件底部加入
[assembly: AllowPartiallyTrustedCallers]
。
二、创建Full Trust Proxy 相关类
在项目中添加一个新的目录TestProxyCode,我们将在此目录下添加我们的Full Trust Proxy相关类。
1、创建数据库访问类:在目录TestProxyCode添加一个新类,命名为SQLDBAccess.cs,此类的代码如下:
View Code
using System;
using System.Data;
using System.Data.SqlClient;
namespace My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo.TestProxyCode
{
/// <summary>
/// 通用数据库类 SQLDBAccess
/// </summary>
public class SQLDBAccess
{
private string ConnStr = null;
private const int TIME_OUT = 180;
#region connected flag
//connected is a flag to make sure if have connect to Database
//when exception happed, perhaps close connect mannual is better
private bool connected = false;
#endregion
#region 构造函数
public SQLDBAccess()
{
//ConnStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MySQL"].ToString();
ConnStr = "Data Source=serverDB;Initial Catalog=MyDb;User ID=MyApp;Password=mypwd";
}
public SQLDBAccess(string Str)
{
try
{
ConnStr = Str;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 公用函数
#region 返回connection对象
/// <summary>
/// 返回connection对象
/// </summary>
/// <returns></returns>
public SqlConnection ReturnConn()
{
SqlConnection Conn = new SqlConnection(ConnStr);
Conn.Open();
return Conn;
}
#endregion
#region 释放connection对象
public void Dispose(SqlConnection Conn)
{
if (Conn != null)
{
Conn.Close();
Conn.Dispose();
}
GC.Collect();
}
#endregion
#endregion
#region 运行SQL语句相关
#region 运行SQL语句,返回DataSet对象 --SQL, Ds
/// <summary>
/// 运行SQL语句,返回DataSet对象
/// </summary>
/// <param name="procName">SQL语句</param>
/// <param name="prams">DataSet对象</param>
public DataSet RunProc(string SQL, DataSet Ds)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da;
//Da = CreateDa(SQL, Conn);
Da = new SqlDataAdapter(SQL, Conn);
try
{
Da.Fill(Ds);
}
catch (Exception Err)
{
throw Err;
}
Dispose(Conn);
return Ds;
}
#endregion
#endregion
}
}
2、创建Full trust proxy的Operation类:在目录TestProxyCode添加一个新类,命名为SQLProxyExecute.cs,此类的代码如下:
View Code
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.UserCode;
using System.Data;
namespace My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo.TestProxyCode
{
[Microsoft.SharePoint.Security.SharePointPermission(System.Security.Permissions.SecurityAction.LinkDemand, ObjectModel = true)]
public class SQLProxyExecute : SPProxyOperation
{
public override object Execute(SPProxyOperationArgs args)
{
if (args != null)
{
SQLProxyArgs ftArgs = args as SQLProxyArgs;
SQLDBAccess dba = new SQLDBAccess(ftArgs.connectionString);
DataSet ds = new DataSet();
return dba.RunProc(ftArgs.sqlCommand, ds);
}
else return null;
}
}
}
3、创建Full trust proxy的Argument参数类:在目录TestProxyCode添加一个新类,命名为SQLProxyArgs.cs,此类的代码如下:
View Code
using System;
using Microsoft.SharePoint.UserCode;
namespace My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo.TestProxyCode
{
[Serializable]
[Microsoft.SharePoint.Security.SharePointPermission(System.Security.Permissions.SecurityAction.LinkDemand, ObjectModel = true)]
public class SQLProxyArgs : SPProxyOperationArgs
{
public string connectionString { get; set; }
public string sqlCommand { get; set; }
public SQLProxyArgs(string ConnectionString, string SqlCommand)
{
this.connectionString = ConnectionString;
this.sqlCommand = SqlCommand;
}
}
}
三、注册Full Trust Proxy到sandboxed code service
1、在Solution Explorer中,右击Feature目录,选择添加新Feature.
2、右击Feature1目录,选择重命名,重命名为:Registration,如下图
。
3、双击Registration.feature,打开其设置窗口,设置它的Title和Description。
4、右击Registration,添加一个Event Receiver
。
5、修改此Event Receiver的后台代码如下:
View Code
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.UserCode;
using Microsoft.SharePoint.Administration;
namespace My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo.Features.Registration
{
/// <summary>
/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should not be modified.
/// </remarks>
[Guid("f64dd849-567d-497a-bd56-9e92ea33f9ce")]
public class RegistrationEventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
Type type = typeof(My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo.TestProxyCode.SQLProxyExecute);
SPProxyOperationType proxyOperationType = new SPProxyOperationType(type.Assembly.FullName, type.FullName);
SPUserCodeService userCodeService = SPUserCodeService.Local;
userCodeService.ProxyOperationTypes.Add(proxyOperationType);
userCodeService.Update();
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
Type type = typeof(My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo.TestProxyCode.SQLProxyExecute);
SPProxyOperationType proxyOperationType = new SPProxyOperationType(type.Assembly.FullName, type.FullName);
SPUserCodeService userCodeService = SPUserCodeService.Local;
userCodeService.ProxyOperationTypes.Remove(proxyOperationType);
userCodeService.Update();
}
// Uncomment the method below to handle the event raised after a feature has been installed.
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised before a feature is uninstalled.
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised when a feature is upgrading.
//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
}
}
。
6、建立(Build)并部署(Deploy)此项目。转载:http://www.cnblogs.com/wsdj-ITtech/archive/2011/09/15/2177790.html