I am not a developer, but here I am getting ready to make my third blog post and I find myself including code. Maybe I should have chosen to move my blog under MSDN instead of Technet.
The reality is that a good IT guy needs a bit of coding in his bag of tricks to get stuff done. Where I spent time in the past with cryptic command lines and batch files I now find myself rooting through SDKs and writing powershell scripts. Grow or die right?
So I have my handy dandy SharePoint server (who doesn't) and I wanted to show some data, but I could not make it look right using OOB(out of box) webparts, list views, and HTML tricks. I guess it is time to build a webpart. Unfortunately this is where the blogs and documentation left me bleary eyed. I spent more time figuring out strong-names than I did with the cool stuff of outputting code.
So here is my howto based on the simplest way I have found to start playing int he wonderfull world of WebParts
Writing the Web Part
At this point I am going to make some assumptions.
Now that the assumptions are out of the way we can get down to business.
Imports System.Web.UI.WebControls.WebParts
Public Class HelloWorld : Inherits WebPart
Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.RenderContents(writer)
End Sub
writer.write("Hello World!")
At this point our code page should look like the output below.
Public Class Hello : Inherits WebPart
writer.Write("Hello World!")
End Class
Deploying the Web part
There are numerous ways to deploy a web part. We can use the GAC and strong names, but I want this to be as easy as possible so we are just going to deploy directly to our website.
<SafeControl Assembly="HelloWorld" Namespace="HelloWorld" TypeName="*" />
Testing the Web Part
At this point the web part is deployed, but not enabled. To use the web part we have to do the following:
You should now be able to browse to the home page of the site and add our new Hello web part to the page from All Web Parts -> Miscellaneous. Cool huh?
Changing the Code
But wait, there is more! Let's say we want to get fancy.
writer.write("Hello " & Context.User.Identy.Name)
Without having to re-deploy your web part has been updated!
This is the easiest way I have found to write SharePoint web parts. It cuts out all the bloat from the templates and allows for lighting fast testing.