The steps to create a simple Web Part that prints "Hello World!" to the screen.
1. In Visual Studio, create a new class library
2. Add a reference to System.Web.
3. Create a class that inherits from the WebPart class in System.Web.UI.WebControls.WebParts. 4. In order to control the rendering of the Web Part one overrides the Render method. An HtmlTextWriter instance is passed into the Render method and can then be used to write markup to the page output.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace ExampleWebPartLibrary
{
public class MyWebPart : WebPart
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("Hello World!");
}
}
}
5. OPTIONAL Add the code statements using System.Security; and [assembly: AllowPartiallyTrustedCallers] to our AssemblyInfo.cs file.
6. Specify proper Assembly Name and Namespace properties for the class library project by opening the project property page
7. OPTIONAL Specify the Signing parameters in the project property page
8. Build the assembly frequent
9. Copy the newly created assembly from bin\Release or bin\Debug folder and paste it in the bin directory of the sharepoint web application OR In our class library properties, set the build output path to the bin directory of our SharePoint development web application.
10. OPTIONAL copy the assembly into C:\Windows\Assembly (GAC), REight click select properies to find its public key
11. In the web.config file of the sharepoint web application, add the assembly as a safe control.
Namespace="ExampleWebPartLibrary"
TypeName="*" Safe="True" AllowRemoteDesigner="True" />
12. We can now add the web part library to your SharePoint site. To do this, log in as an administrator and go to the settings page. Click on "Web Part Gallery" under the "Galleries" section. Click "New" and we should see our web part in a list. Select our web part and click "populate gallery".
13. We can give your web part a friendly title and control its permissions using the edit button in the web part gallery. I have chosen to call the Web Part "My Web Part".
14. Finally we can add the web part to a web part page and we should see the web part's hard coded "Hello World!" message.
Adding ASP.NET controls in our web part
1. Repeat the steps 1-3 of first example.
2. Controls should be declared as member variables and then initialised and configured within an override of the CreateChildControls method. I usually create a separate method to setup each control and then invoke that method from CreateChildControls. It is important to add the created controls to the list of Web Part controls. If a control is not added to this list then it's events won't be raised on post back.
private Button btnDemo;
private void createBtnDemo()
{
btnDemo = new Button();
btnDemo.Text = "Click me";
btnDemo.Click += new EventHandler(btnDemo_Click);
// This line of code is required to trigger the click eventControls.Add(btnDemo);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
createBtnDemo();
createLblHelloWorld();
}
3. The controls are rendered inside the Render method override. This method can be used to layout the web part it's controls. The HtmlTextWriter has many useful methods to help out, including WriteBreak() which writes a "
".
protected override void Render(HtmlTextWriter writer)
{
writer.WriteBreak();
lblHelloWorld.RenderControl(writer);
writer.WriteBreak();
writer.WriteBreak();
btnDemo.RenderControl(writer);
}
4. Perform the steps from 5 (of first example) to deploy the web part
Accessing SharePoint data in our web part
1. Repeat the steps 1-3 of first example.
2. Reference Microsoft.SharePoint.dll by browsing to the directory "[Program Files]\Common Files\Microsoft Shared\web server extensions\12\ISAPI".
3. To take care of the code access security for our web part by setting the target SharePoint site's trust level to "Full". This level of trust is generally acceptable in an Intranet environment. Find the following in our site's web.config file:
change this to:
4. Write some code to access SharePoint data. Here I show a simple web part that displays the name of the presently logged in user:
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
namespace ExampleWebPartLibrary
{
public class HelloUser : WebPart
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
// Get a contextual reference to the current SPWeb
SPWeb currentWeb = SPContext.Current.Web;
// Write a message to the current user
writer.Write("Hello " + currentWeb.CurrentUser.LoginName);
}
}
}
Note : SPContext.Current.Web is the starting point for accessing SharePoint data. For example we can use it to access data in lists and document libraries.
5. Perform the steps from 5 (of first example) to deploy the web part
No comments:
Post a Comment