SharePoint Study Notes-sandbox solution-full trust proxy-Development Instance (2. Access full trust PR in webpart

Source: Internet
Author: User

Previous SharePoint study note --- sandbox solution -- full trust proxy -- developer instance (1. Create a full trust proxy that can access the database), which we created in SharePoint Farm solution, deploy and register a full trust proxy that can access the database. In this article, we will create a webpart in sandbox solution of SharePoint and access the database through the full trust proxy created earlier, display the access results in the created webpart.
Go to the operation steps.
1. Create and set a project
1. Open vs2010 and create an empty project empty SharePoint project named mytestsandboxaccessdbwebpart, as shown in

This project is of the sandbox type

2. In Solution Explorer, right-click the references directory and select Add reference. In the displayed add reference window, select the Browse column to view the directory where the full trust proxy class you deployed is located.
C: \ windows \ Assembly \ gac_msil \ My. Sharepoint. Sandbox

Enter the corresponding version


Select My. mongopoin. sandbox. dll class

After successful reference, you can double-click this class to see the corresponding content defined in it

 

2. Create a webpart and call full trust proxy
1. In Solution Explorer, right-click the project, select Add, add new item, and select webpart (Note: you cannot add Visual Web Part), and name it testsandboxsolutionaccessdatabasewebpart.
 
2. In Solution Explorer, double-click testdbwebpart. webpart and edit the content as follows:

Here we set its title and description.

3. Right-click feature1 and rename it databasewebpart,
 

Double-click databasewebpart to open the feature designer. In this window, set as shown in

 

4. Modify testdbwebpart. CSCodeAs follows:

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;

UsingMicrosoft. Sharepoint. usercode;
UsingMicrosoft. Sharepoint. administration;
UsingMicrosoft. Sharepoint. utilities;
UsingSystem. Data;
UsingMy. Sharepoint. sandbox;

Namespace Mytestsandboxaccessdbwebpart. testdbwebpart
{
[Toolboxitemattribute ( False )]
Public   Class Testdbwebpart: webpart
{
Private Textbox txtdbconnection =   New Textbox () {text =   @" 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 ();

PublicTestdbwebpart ()
{
# RegionProxyone

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 VoidCreatechildcontrols ()
{
Table layouttable= NewTable ();

# 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 VoidRendercontents (htmltextwriter writer)
{
Base. Rendercontents (writer );
}
}
}

In the above Code, how to obtain the corresponding value in assembly name can be found in this article:

SharePoint Study Notes --- quickly determine the Assembly name of the current project in visualstudio2010

In this webpart, we create two Textbox, one for inputting the database connection string and the other for inputting the SQL statement. A button is also defined and the Click Event of the button is attach. When the button is clicked, the background code passes the obtained parameters to the full trust proxy class, access the database content through the full trust proxy class, and pass the obtained results back to display them in the gridview control on the webpart.

 

3. Deploy and use webpart to present database data
Build and deploy this project. After successful, create a Web Part page on the test SharePoint website, add the deployed webpart to the page, enter the corresponding content in the corresponding text box of the webpart, and then click the ACCESS database button.

The result is displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.