Part 1: Build Web Part Assembly
Posted by Steve Pietrek on June 6, 2007
This is Part 1 of a series on creating web parts and deploying them to SharePoint using a VS 2005 Solution Project. There are 6 parts in the series.
1. Open Visual Studio .NET 2005
2. Select File>New>Project from the menu
3. In the New Project dialog, select the Visual C# Project Type
4. Under Templates, select Class Library
5. Name the project HelloWorldWebPart
6. Check the “Create directory for solution” option
7. Name the Solution HelloWorldWebPart
8. Click OK
9. Right-click on Class1.cs and select Rename
10. Rename the file to HelloWorldWebPart.cs
11. Right-click on the HelloWorldWebPart and select Add Reference
12. Add the following reference to the project:
- System.Web
13. Add the following using statements to the HelloWorldWebPart.cs file:
- System.Web.UI
- System.Web.UI.WebControls
- System.Web.UI.WebControls.WebParts
- System.ComponentModel
14. Change the HelloWorldWebPart.cs file’s namespace to HelloWorld.Web
15. Add the following code to the file:
public class HelloWorldWebPart : WebPart
{
protected LiteralControl _info;
private string _employeeName;
[Personalizable(PersonalizationScope.User), WebBrowsable(true), Category("Data")]
public string EmployeeName {
get { return _employeeName; }
set { _employeeName = value; }
}
protected override void CreateChildControls() {
this.Controls.Clear();
_info = new LiteralControl(_employeeName);
Controls.Add(_info);
this.ChildControlsCreated = true;
}
protected override void RenderContents(HtmlTextWriter writer) {
writer.AddAttribute(HtmlTextWriterAttribute.Width, “100%”);
writer.AddAttribute(HtmlTextWriterAttribute.Border, “0″);
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_info.RenderControl(writer);
writer.RenderEndTag(); // td
writer.RenderEndTag(); // tr
writer.RenderEndTag();
}
16. Build the HelloWorldWebPart project and fix any errors.



links for 2007-06-07 » mhinze.com said
[...] Part 1: Build Web Part Assembly « Steve Pietrek’s SharePoint Stuff (tags: sharepoint webparts moss2007) [...]