设为首页 收藏本站
查看: 931|回复: 0

[经验分享] Sharepoint学习笔记---Sandbox Solution-- Full Trust Proxy--开发实例之(1、创建一个能访问DataBase的Fu

[复制链接]

尚未签到

发表于 2015-9-25 13:02:34 | 显示全部楼层 |阅读模式
  在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/
DSC0000.png

DSC0001.png
  

  2、在Solution Explorer中,选择项目名并点击鼠标右键,在弹出菜单中选择Properties,从而打开项目设置窗口
  2.1在此窗口中选择Application栏,设置Default namespace和Assembly name两项,分别为My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo和My.Sharepoint.SandBox,如下图
   DSC0002.png
2.2在此窗口中选择Sharepoint栏,设置Post-deployment CommandLine内容:


  net stop SPUsercodeV4
  net start SPUsercodeV4  它的主要目的是保证我们在每一次修改此full trust proxy并形成和发布其新的版本时,自动重新启动Microsoft SharePoint Foundation Sandboxed Code service,以使新的改动在Sharepoint网站中生效。
  

DSC0003.png
3、在Solution Explorer中,双击并打开Assembly.info,在此文件底部加入

  [assembly: AllowPartiallyTrustedCallers]
   DSC0004.png
二、创建Full Trust Proxy 相关类在项目中添加一个新的目录TestProxyCode,我们将在此目录下添加我们的Full Trust Proxy相关类。
1、创建数据库访问类:在目录TestProxyCode添加一个新类,命名为SQLDBAccess.cs,此类的代码如下:

DSC0005.gif DSC0006.gif 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,如下图
DSC0007.png
3、双击Registration.feature,打开其设置窗口,设置它的Title和Descript DSC0008.png ion。

  
4、右击Registration,添加一个Event Receiver

DSC0009.png
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)
        //{
        //}
    }
}  
DSC00010.png
6、建立(Build)并部署(Deploy)此项目。
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118722-1-1.html 上篇帖子: 使用VISUAL STUDIO 2008 BETA2 创建 SHAREPOINT 工作流 下篇帖子: SharePoint开发笔记-为SharePoint2010客户端对象模型创建Silverlight应用程序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表