Sharepoint学习笔记---Sandbox Solution-- Full Trust Proxy--开发实例之(2、在Webpart中访问Full Trust Proxy)...
上一篇Sharepoint學習筆記---Sandbox Solution-- Full Trust Proxy--開發實例之(1、創建一個能訪問DataBase的Full Trust Proxy), 我們在Sharepoint的Farm Solution中創建,部署并注冊了一個能訪問數據庫的Full trust Proxy,這一篇我們將在Sharepoint的Sandbox Solution中創建一個Webpart,并通過前面創建的Full trust proxy訪問數據庫,把訪問結果顯示在我們創建的Webpart中。
進入操作步驟。
一、創建和設置項目
? 1、打開VS2010,創建一個空項目Empty SharePoint Project,命名為MyTestSandBoxAccessDBWebPart,如下圖
??
? 此項目為Sandbox類型的
? 2、在Solution Explorer中,右擊References目錄,選擇Add reference,在跳出的Add Reference窗口,選擇Browse欄,瀏覽到你部署的Full trust Proxy類所在目錄
C:\Windows\assembly\GAC_MSIL\My.Sharepoint.SandBox
進入對應的版本
選擇My.Sharepoin.SandBox.dll類
引用成功后,可以雙擊此類,看到它里面定義的對應內容
?
二、創建Webpart并調用Full Trust Proxy
1、在Solution Explorer中,右擊項目,選擇Add,添加New Item,選擇Webpart(注意:不能選擇添加Visual Web Part),并命名為TestSandboxSolutionAccessDataBaseWebPart
?
2、在Solution Explorer中,雙擊TestDBWebpart.webpart,編輯內容如下:
此處我們設置了它的Title和Description.
3、右擊Feature1,重命名為DataBaseWebPart,
?
然后雙擊DataBaseWebPart,打開Feature Designer,在此窗口中設置如下圖
?
4、修改TestDBWebpart.cs代碼如下:
using?System;using?System.ComponentModel;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?Microsoft.SharePoint;
using?Microsoft.SharePoint.WebControls;
using?Microsoft.SharePoint.UserCode;
using?Microsoft.SharePoint.Administration;
using?Microsoft.SharePoint.Utilities;
using?System.Data;
using?My.Sharepoint.SandBox;
namespace?MyTestSandBoxAccessDBWebPart.TestDBWebpart
{
????[ToolboxItemAttribute(false)]
????public?class?TestDBWebpart?:?WebPart
????{
????????private?TextBox?txtDBConnection?=?new?TextBox()?{?Text?=?@"Data?Source=Data?Source=serverDB;Initial?Catalog=MyDb;User?ID=MyApp;Password=mypwd"?};
????????private?TextBox?txtSQLCommand?=?new?TextBox()?{?Text?=?@"SELECT?TOP(100)*?FROM?dbo.myTable"?};
????????private?Label?results?=?new?Label();
????????private?Label?lblFileContent?=?new?Label()?{?Text?=?"Input?DBConnectionStr?:"?};
????????private?Label?lblFileName?=?new?Label()?{?Text?=?"Input?SQL?Command?:"?};
????????private?Button?btnAccessDatabase?=?new?Button()?{?Text?=?"Access?Database"?};
????????private?GridView?gv?=?new?GridView();
????????public?TestDBWebpart()
????????{
????????????#region??ProxyOne
????????????string?assemblyName?=?"My.Sharepoint.SandBox,?Version=1.0.0.0,?Culture=neutral,?PublicKeyToken=9f460c3b7a15fdf1";
????????????string?typeName?=?"My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo.TestProxyCode.SQLProxyExecute";
????????????try
????????????{
????????????????btnAccessDatabase.Click?+=?(object?sender,?EventArgs?e)?=>
????????????????{
????????????????????string?DBConnectionString?=?txtDBConnection.Text.ToString().Trim();
????????????????????string?SQLCommandStr?=?txtSQLCommand.Text.ToString().Trim();
????????????????????DataTable?dt?=?((DataSet)SPUtility.ExecuteRegisteredProxyOperation(assemblyName,?typeName,
????????????????????????new?My.Sharepoint.SandBox.MyTestSandBoxAccessDBInfo.TestProxyCode.SQLProxyArgs(DBConnectionString,?SQLCommandStr))).Tables[0];
????????????????????gv.DataSource?=?dt;
????????????????????gv.DataBind();
????????????????};
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????results.Text?=?ex.ToString();
????????????}
????????????#endregion
????????}
????????protected?override?void?CreateChildControls()
????????{
????????????Table?layoutTable?=?new?Table();
????????????#region?Create?Table
????????????//Input?File?Content?Row
????????????layoutTable.Rows.Add(new?TableRow());
????????????layoutTable.Rows[0].Cells.Add(new?TableCell());
????????????layoutTable.Rows[0].Cells.Add(new?TableCell());
????????????//Input?File?Location?Row
????????????layoutTable.Rows.Add(new?TableRow());
????????????layoutTable.Rows[1].Cells.Add(new?TableCell());
????????????layoutTable.Rows[1].Cells.Add(new?TableCell());
????????????//Create?File?button?Row
????????????layoutTable.Rows.Add(new?TableRow());
????????????layoutTable.Rows[2].Cells.Add(new?TableCell()?{?ColumnSpan?=?2?});
????????????//Show?Result?Row
????????????layoutTable.Rows.Add(new?TableRow());
????????????layoutTable.Rows[3].Cells.Add(new?TableCell()?{?ColumnSpan?=?2?});
????????????txtDBConnection.Width?=?400;
????????????txtSQLCommand.Width?=?400;
????????????layoutTable.Rows[0].Cells[0].Controls.Add(lblFileContent);
????????????layoutTable.Rows[0].Cells[1].Controls.Add(txtDBConnection);
????????????layoutTable.Rows[1].Cells[0].Controls.Add(lblFileName);
????????????layoutTable.Rows[1].Cells[1].Controls.Add(txtSQLCommand);
????????????layoutTable.Rows[2].Cells[0].Controls.Add(btnAccessDatabase);
????????????layoutTable.Rows[3].Cells[0].Controls.Add(results);
????????????#endregion
????????????this.Controls.Add(layoutTable);
????????????this.Controls.Add(gv);
????????????base.CreateChildControls();
????????}
????????protected?override?void?RenderContents(HtmlTextWriter?writer)
????????{
????????????base.RenderContents(writer);
????????}
????}
}
?在上面的代碼中,如何取得Assembly Name中的相應值,可參見此文 :
Sharepoint學習筆記--- 快速確定VisualStudio2010當前Project的assembly name
?在此Webpart中,我們創建了兩個TextBox,一個用于輸入數據庫連接字符串,一個用于輸入SQL語句。同時還定義了一個按鈕,并Attach了此按鈕的Click事件,當點擊此按鈕后,后臺代碼會通過取得的參數,傳遞給Full Trust Proxy類,通過Full Trust Proxy類去訪問數據庫內容,然后把取得的結果傳遞回來,顯示在Webpart上的GridView控件中。
?
三、部署并使用Webpart呈現DataBase數據庫數據
?Build并部署此項目。成功后,在測試Sharepoint網站上創建一個Web Part Page,在此Page上加入部署的Webpart,然后在此Webpart的相應Text框中輸入相應內容再點擊Access Database按鈕
可看到結果
總結
以上是生活随笔為你收集整理的Sharepoint学习笔记---Sandbox Solution-- Full Trust Proxy--开发实例之(2、在Webpart中访问Full Trust Proxy)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows无线网卡如何设置
- 下一篇: win7如何获取管理员最高权限