Webpart can be seen as a plug-in of SharePoint and a component used to implement specific functions. This article describes how to develop and deploy a simple webpart.
1. Development
Create a project and select the SharePoint webpa template. The project name is simplecalculator.
You can see that Microsoft is automatically added to solution manager. sharePoint reference; added *. SNK ,*. CS ,*. webpart ,*. XML and other files, we will change it to a friendly File Name:
simplecalculator: using system;
using system. runtime. interopservices;
using system. web. UI. htmlcontrols;
using system. web. UI. webcontrols;
using system. web. UI. webcontrols. webparts;
using system. XML. serialization;
UsingMicrosoft. SharePoint;
UsingMicrosoft. Sharepoint. webcontrols;
UsingMicrosoft. Sharepoint. webpartpages;
Namespace Simplecalculator
{
[GUID ( " 05dd0d64-0c29-47ab-8a00-8f95d6ef67cc " )]
Public Class Calculator: system. Web. UI. webcontrols. webparts. webpart
{
Textbox txt1 = New Textbox ();
Dropdownlist ddl1 = New Dropdownlist ();
Textbox txt2 = New Textbox ();
Htmlgenericcontrol lbl1 = New Htmlgenericcontrol ( " Span " );
Textbox txt3 = New Textbox ();
Button btn1 = New Button ();
Htmlgenericcontrol lbl2 = New Htmlgenericcontrol ( " Span " );
Protected Override Void Createchildcontrols ()
{
Txt2.width = Ddl1.width = Txt1.width = 40 ;
ddl1.items. add ( " + " );
ddl1.items. add ( " - " );
ddl1.items. add ( " * " );
ddl1.items. add ( " / " );
Lbl1.innerhtml= "=";
Btn1.text= "Calculate";
Btn1.click+ = NewEventhandler (btn1_click );
Lbl2.style. Add ("Color","Red");
This . Controls. Add (txt1 );
This . Controls. Add (ddl1 );
This . Controls. Add (txt2 );
This . Controls. Add (lbl1 );
This . Controls. Add (lbl2 );
This . Controls. Add (btn1 );
}
Void Btn1_click ( Object Sender, eventargs E)
{
Try
{
Double Db1 = Double . Parse (txt1.text );
Double DB2 = Double . Parse (txt2.text );
Double Result = 0.0 ;
Switch (Ddl1.selectedvalue)
{
Case " + " :
Result = Db1 + DB2;
Break ;
case " - " :
result = db1 - DB2;
Break ;
case " * " :
result = db1 * DB2;
Break ;
case " / " :
result = db1 / DB2;
Break ;
}
lbl2.innerhtml = result. tostring ( " F2 " );
}
Catch(Exception ex)
{
Lbl2.innerhtml=Ex. message;
}
}
}
}
The following SharePoint namespaces are automatically added to the calculator file (not required in this example)
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;
Using Microsoft. Sharepoint. webpartpages; a guid, which is the only identifier of this webpart. F5 can be compiled and deployed with one click.
On the webpart list page, you can see the webpart we just deployed.
Exit the editing mode and simplecalcuator can correctly display and run the UI without modification ;-)
If the deployment is successful, let's see where your files are deployed?
As you can see, the DLL file is actually deployed in GAC. Program This is not what we want.
All the files are displayed, and a PKG folder is displayed. Set deploymenttarget of manifest. XML to webapplication. <? XML version = "1.0" encoding = "UTF-8" ?>
< Solution Solutionid = "20178fa3d-5de4-4b46-85a8-5988cc6c977d" Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Featuremanifests >
< Featuremanifest Location = "Calculator \ feature. xml" />
</ Featuremanifests >
< Assemblies >
< Assembly Location = "Simplecalculator. dll" Deploymenttarget ="Webapplication" />
</ Assemblies >
</ Solution >
After modification, the DLL file is in the bin directory of the site, but the following error occurs when you add the webpa to the page:
Assemblies that implement ASP. NET Web parts and are installed into a partially trusted location, such as the bin directory, must be compiled with the allowpartiallytrustedcallersattribute set for import to succeed.
Add attributes in the Assembly
[Assembly: allowpartiallytrustedcallersattribute ()], which is located in the system. Security namespace.
* In the webpart file, the title attribute is the name of the webpart, and the description is the description of the webpart. The displayed tooltip is the description content.
Floating remote blog: http://www.cnblogs.com/zxjay/
Author: piaoyao (Zhou Zhenxing)