Skip to main content

Patrick Tisseghem's Blog [MVP SharePoint]

Go Search
U2U Blog Center
U2U website
  

The Happy SharePoint Traveller

U2U Blog Center > Patrick Tisseghem's Blog [MVP SharePoint] > Posts > Building a ‘Hello World’ Web Part with the new Visual Studio Extensions for SharePoint 2007
Building a ‘Hello World’ Web Part with the new Visual Studio Extensions for SharePoint 2007

This is a small tutorial to get you started with the new SharePoint 2007 extensions for Visual Studio.NET 2005. I’ll cover here the basics for building a Web Part to get you started.

Step 1 is to install of course the extensions. They can be downloaded over here.

Next, open up Visual Studio.NET 2005 and you can create a new project selecting the SharePoint project type. Different templates are available amongst one is the template to build a Web part.


A class is created, all of the references are in place and also you have the option now to configure the feature that will make your Web part available in the site collection you are going to target. To see that feature configuration place, right-click the project and open the properties page in the designer. There is a new tab called SharePoint Solutions. Activating it shows the details of the feature. For the Hello World sample, pretty much everything is filled in and you do not have to change anything.



While you are in the properties page, activate the Debug tab and type in the URL of the site collection where you want to see your Web Part popping up.



Now return to the class representing your Web Part. The only thing we will worry about for this basic tutorial is just outputting the string Hello World:

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace HelloWorld
{
    [Guid("2ad93b4c-6f33-4942-bcc9-cbb8b2597610")]
    public class HelloWorld : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void Render(HtmlTextWriter writer)
        {
           
            writer.Write("Hello Dudes!");
        }
    }
}

You are ready to rock :). Press F5 and you’ll start the process of deploying the Web Part as a feature to the server farm.


Open up the site you are targetting and in the Site Settings page, navigate to the Site Collection Features page. You’ll notice that the Web Part is listed as a feature that is already activated.
That entails that you now can go to one of your pages and just add it to the page as one of the Web Parts.

 

Now don’t laugh! I know it is pretty simple but you can now start building more complex Web Parts. And didn’t I hear Jan talking about his plans to integrate the Smart Part in all of this?

Anyway, interesting things happened behind the scenes when you pressed F5. Basically, Visual Studio.NET created a SharePoint 2007 solution for you. You can browse to that solution in the file system. It is the HelloWorld.wsp file in the bin\debug folder of your project. This is a CAB file and you can see its contents extracted in the underlying solutions folder. Here you will find the DLL and the feature that makes the Web Part available. Remember features are not about deploying things, features are about making things in SharePoint 2007 available.

The manifest.xml actually tells the tool that installs the solution where everything needs to end up.

The DLL goes into the GAC, the registration is done as a safe control and the feature files are copied in the Features folder in the 12-hive. So what is that tool that installs the solution? Well it is good old STSADM.EXE that has now a new option called addsolution. You can verify all of this in the setup.bat found in the bin\debug folder. Now go to the SharePoint 3.0 Central Administration. In the Operations page, you find a link called Solution Management. You can see here the deployed solutions. This is the place where the admins can manage all of the deployed solutions and possibly remove them.

Good stuff from the product team! It is really going to give a boost in the productivity of many Web Part developers.

Comments

Object not found error

I am trying to build a simple webpart with the VS2005 extensions, but when I press F5 (debug) I get Object not found error. The machine I am developing on does not have Sharepoint installed, so I have copied the dll to a folder and added a reference to it. Should this work or do I need to locate the dll in a specific folder location?
at 9/03/2007 15:15

Sharepoint Site not found at URL

Any ideas how I can correct this problem?  Sharepoint is clearly running and I am developing on my Server 2003 box where it is located.  The URL is http://s2007server.  If I put thatin my browser everything is fine.  I assume something is not properly setup in my Sharepoint setup?
at 28/03/2007 18:47

Sharepoint site not found at URL

Any ideas how I can correct this problem?  Sharepoint is clearly running and I am developing on my Server 2003 box where it is located.  The URL is http://s2007server.  If I put thatin my browser everything is fine.  I assume something is not properly setup in my Sharepoint setup?
at 28/03/2007 18:48

More detail on the above problem...

The site is currently running in Farm mode, and Microsoft says debugging is not supported in Farm mode.  Is this true, and will this cause the above problem?  "Sharepoint site not found at URL"

Thanks
at 28/03/2007 22:25

Webparts - baffling

Webparts are one of the most baffling things MS have come up with in ASP.NET so far. The usefulness of webparts over normal asp.net seems rather limited, and definitely detrimental in programmer productivity.

They supply VSeWSS.exe but you can only install it on a Windows 2003 Server with VS2005 installed on it. I mean, who uses W2003Server as their desktop development OS? Very odd idea.
at 19/06/2007 5:35

Totally Rad!

If only I had my own personal copy of 2003 Server to run all this on...
at 9/08/2007 17:11

Receiving Error at the time of Compiling and Deploying.

After creating the webpart when i press F5, it gives me error and ask :

" There were deployment errors. Continue? "

either press yes or no, both r same and the error msg is :

"Object reference not set to an instance of an object."

i tried alot but could nt find ny solution, plz reply me with ur ideas.

thanks in advance.
at 17/08/2007 16:47

Error at deployment

I'm having the same types of issues. It seems that the files are all created, but only under the build folder, in my documents/visual studio etc in my case. running the setup.bat deploys it correctly, though I haven't tried debugging yet. Most likely changing the build settings for the compiler would cause everything to work correctly. I'll repost here if I figure something more out. Good luck.
at 4/09/2007 23:47

Error at deployment

I'm having the same types of issues. It seems that the files are all created, but only under the build folder, in my documents/visual studio etc in my case. running the setup.bat deploys it correctly, though I haven't tried debugging yet. Most likely changing the build settings for the compiler would cause everything to work correctly. I'll repost here if I figure something more out. Good luck.
at 4/09/2007 23:52

Solution for 'Object reference not set .....'

Hi Everyone,
 
   You can solve the problem by going to the bin folder & running firstly setup.bat /uninstall & then setup.bat /install. It worked for me. Let me know if it works.

Regards,
Praveen
at 24/09/2007 5:39

Object reference

Running setup.bat doesn't work
Look carefully, the old solution is deleted than the error happens when it tries to create the new solution.xml file
at 29/10/2007 8:19

Re: Building a ‘Hello World’ Web Part with the new Visual Studio Extensions for SharePoint 2007

Check out a possible solution to the "object reference not set to the instance of an object" at my wiki site I created.

http://wsswiki.com/Visual_Studio_Extensions_For_WSS_3.0

I also created a utility (downloadable from there) that will help track down the feature causing the issue.
at 30/10/2007 4:59

Building a web part to use a database

I'm sort of new to all this. Is there a tutorial on how to build a web part that uses a backend sql server database? For example, how to add a field for data entry and then write the content to a table on SQL Server. I then want to read the data back in at some point. Any reference materials would be helpful. Thanks.
at 17/01/2008 22:40

Mr

Can u please tell me why the SharePoint solution tag is not present in my Visual Studio project property page? Ive only have up to Signing tag only. Im using VS 2005 and got the VS SharePoint extention too.
at 26/03/2008 6:35

Have to build on the server?

This is the most ludicrous thing I have ever heard of - that you have to install VS2005 and VSeWSS on the freaking server in order to build a web part!!! Absolutely outrageous and brain dead and inconsiderate on Microsoft's part!
at 30/04/2008 8:23

Have to build on the server (Extensions)?

Brain dead, I agree, and now I have a team of 20 developers, all wanting to use the Sharepoint extensions...
at 4/07/2008 15:43

It works for me..

thanks very much
at 7/07/2008 17:09

Tab called SharePoint Solutions

If you don't have a tab called SharePoint Solutions.
Create a new solution and take the second webpart projet
at 23/07/2008 21:41

How to install on a workstation...

I agree, ridiculous that they expect development to be done on 03 server.  Here's a good article that will show you a registry hack that will allow you to install the tools on a workstation:

http://fernandof.wordpress.com/2008/02/11/how-to-install-the-sharepoint-2007-vs-2005-extensions-on-a-workstation/
at 31/12/2008 16:24

Regarding GUID

In the code,it is written: [Guid("2ad93b4c-6f33-4942-bcc9-cbb8b2597610")].

Can you please tell me from where did you retrieve this GUID?
at 10/02/2009 12:34

Error on deploying - hello world

Hi,

I am Admin on this box (w2k3) and have sharepoint 2007 + visual studio extension for wss installed. I am building this hello world web-part so that it get deployed on my sharepoint site http://machinename:6147 I have given the same path in visual studio debug.

It compiled fine but I am getting deployment error on F5,

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.SharePoint.Tools.Reflection.ReflectionUtility.InvokeMethod(MethodBase method, Object instance, Object[] parameters)
   at Microsoft.SharePoint.Tools.Reflection.ReflectionUtility.InvokeMethod(Type type, String methodName, Object instance, Object[] parameters)
   at Microsoft.SharePoint.Tools.SharePointProxies.SPProxy.InvokeMethod(String name, Object[] parameters)
   at Microsoft.SharePoint.Tools.SharePointProxies.SPSolutionProxy.DeployLocal(Boolean globalInstallWPPackDlls, Collection`1 webApplications, Boolean force)
   at Microsoft.SharePoint.Tools.SharePointProxies.SPSolutionFacade.AddSolution(String url, String solutionFilePath, Boolean solutionContainsWebApplicationResource)
   at VSeWSS.Server.Services.SPService.AddSolution(String url, String solutionFilePath, Boolean solutionContainsWebApplicationResource)

Also the error file is placed in
C:\Documents and Settings\Default User\Application Data\Microsoft\VSeWSS 1.3

Why this error and why default user?

Please throw some light.

thanks
JD
at 29/04/2009 22:46
Captcha

Enter the code shown above: *

(Note: If you cannot read the numbers in the above
image, reload the page to generate a new one.)