Developing a simple web part (.wsp) using WSPBuilder

Before we started the development, we have to install the WSPBuilder (SharePoint WSP tool), an extensions for Visual Studio. Just download the latest version here and install it. It is pretty easy steps.

Step 1: Create a new project on visual studio and then select the project type WSPBuilder  under C# programming language (I am using Visual Studio 2008).

Step 2: This is will create the following structure in the solution explorer.

Step 3: Right click on the project (WSPBuilderStepBiStep) and then add a new Item.

Step 4: This will give the following dialog box and then select WSPbuilder and Web Part Feature template. Enter the name and click Add button (I have given WebPartStepBiStep here)

Step 5: We will get the following dialog box and asking for the scope for this Web Part. I just modified the description and select the scope and then click OK.

Step 6: The solution explorer will look like this. Under the 12 hive folder you can see the Template and Features sub folders.

Step 7: Double click  on the WebPartStepBiStep.cs file and add your custom code on the right place. I just modifed the property like this:

public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello Step Bi Step!";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }

If you wanna to add a label rather than using this LiteralControl just replace the following code

this.Controls.Add(new LiteralControl(this.MyProperty));

with this

System.Web.UI.WebControls.Label label=new System.Web.UI.WebControls.Label();
label.Text = "Hello StepBiStep!";
this.Controls.Add(label);

Step 8: After replacing the code, the CreateChildControls() method will look like this:

        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();
                    // Your code here...
                    System.Web.UI.WebControls.Label label=new System.Web.UI.WebControls.Label();
                    label.Text = "Hello StepBiStep!";
                    this.Controls.Add(label);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

Step 9: To build the project, just right click on the project name (WSPBuilderStepBiStep –> Build) and click Build.

Step 10: To build the WSP, Right click on the project name(WSPBuilderStepBiStep) –> WSPBuilder –> Build WSP

Step 11: This will create the .dll and .wsp files.


Step 12: To deploy the web part (.wsp file), right click on the project name(WSPBuilderStepBiStep) –> WSPBuilder –> Deploy


Step 13: You will get the following output, saying the .wsp file deployed successfully (done!).


Step 14: Go to the Web Part gallery and  populate the deployed Web Part on the New web part page.

Step 15: To use this Web Part in to your page, just follow these steps from my previous post. That’s all! Happy WSPBuilding..! 🙂

Please note: You have to run the Visual Studio as Administrator, otherwise you will get this error when you deploy the web part.

References:

1. http://rasor.wordpress.com/2008/10/12/wss-dev-quickstart/

Advertisements

, , , , , , , , , ,

  1. How to add a custom web part to a SharePoint 2007 « Step Bi Step
  2. Deploy and Re-deploy a web part solution package (.wsp) using STSADM command. « Step Bi Step

Please leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: