直接进入操作步骤。
一、创建和设置项目 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 { ///
/// DataSet对象
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 { ///
。 6、建立(Build)并部署(Deploy)此项目。
最新评论