So you want to build your own web part eh?
First thing is first, I am going to cut through the crap so you know whether or not you should keep reading right now: for this tutorial to make any sort of sense, you are going to need to have
WSS 3.0 and VS/
VWD 2008 installed on your Server 2003+ machine; if you do not have
one available,
make a virtual server (Actually, this is the ideal setup
anyway). You will then need to
install
WSS 3.0 Extensions for Visual Studio 2008 on your machine (I know I
previously posted how to install WSS 3.0 Extensions on your desktop, but that
will not work for a full build). Yes it's a pain, and a lot of requirements, but SharePoint can be a bastard child sometimes.
This really is the hardest part about building new web parts.
Once all of that is out of the way, simply start a new project in VS and choose
SharePoint->Web Part. Give it
a name and a location and hit OK.
Your solution explorer should now be populated with a bunch of files and
references like this:
Now really there are only a few files you need to concern yourself with. The most important one being the
<WebPartName>/<WebPartName>.cs file, in my case it is Test/Test.cs; I
will refer to this file as the main file from now on. I say it is the most important as
that is the one that does everything from presentation to code-behind. The others are the
<WebPartName>.webpart file and possibly the AssemblyInfo.cs file, but
we will focus on the first right now.
Open up the main file and you will see something like
this:
Where you see “TODO: …” is where you are going to put your rendering code. They even provide you with the
always-friendly “Hello World” program if you just un-comment it. The Guid is automatically populated
when you make your project, so don’t mess with it. The big thing here is that 100% of
your styling and layout has to be taken care of back here. There is no aspx page to your aspx.cs
like a normal .net web page. Once
you have a handle on that, it can be treated just like any other asp.net website
you have ever made. There are some
other great interactive tools and features you can include, but that is for
another day. Right now, just use the
“Hello World” code, use my supplied code or fill it up with your own.
NOTE: Often times it is easier to get your programming logic done in a regular Visual
Studio Project as there is no way to easily test in a SharePoint Web Part
Project.
Now that you have your code finished, we can change a
few other things. Open the
<WebPartName>.webpart file and you can see some properties you can change. Customize the Title and Description if you
like.
That will change the default settings for this web part. That is all I would mess with for
right now. So in theory, you now
have a 100% working web part. Now is
the easy part. Just right click on
your project and click ‘Deploy’.
Assuming everything worked, you know have published your web part to your
portal. If it didn’t work, go fix
your bugs and come back :PGo to
your Portal->Site Actions->Site Settings->Modify All Site Settings and click on
Web Parts under Galleries and you should see your Web Part with a happy little
‘!NEW’ by it.
Now just go to a Web Part Page and put it in there
somewhere then exit edit mode. You should
now see your web part in action:
Here is my example web part you can take a look at. It shows a couple different methods of how
you can make a web part interact with itself. As I mentioned above, there is a lot you can do with web parts beyond
this, and I will hopefully be able to post some more in the near future.
UPDATE
I wrote a tutorial on how to package and deploy a web part once you make it.
Source Code: