Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Posted Monday, February 15, 2010 2:17 PM by CoreyRoth

If you are already familiar with SharePoint 2010, you already know how easy it is to build and deploy a web part now.  However, this post is for those that don’t keep up with SharePoint as some of us do and may not realize how the development experience has improved so much.  My post How to Build and Deploy a Web Part is by far the most popular post on DotNetMafia.com.  I wanted to make today’s post just as a point to show you how much less work is involved in deploying a web part.  I am going to group this post into sections in a similar manner as I did the post for the WSS3 post.

Environment

There can be entire talks about what the best way to develop is now, but we’ll start with the simplest.  Although you can install SharePoint on Windows 7 and directly develop on it, most people are going to say stick with a virtual machine and run Windows Server 2008 R2.  It’s certainly simpler to get all of the prerequisites installed if you stick with Windows Server.  The benefits to developing directly on a machine with SharePoint on it are so great now that I would recommend against remote debugging (although you still can).  The SharePoint Root (or the 12 hive as you called it) is now the 14 hive and is located at the predictable path below.

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14

Coding the Web Part

Here is where things start to change.  Instead of creating a class library and adding references to the SharePoint DLLs, we simply use one of the new included SharePoint project templates as you can see here.

WebPartIntroEmptyProject

Start by using the Empty SharePoint Project template.  Also make sure you have it set to .NET Framework 3.5 as SharePoint does not run under .NET Framework 4.0 (don’t get me started).  You’ll notice you have many different project templates to choose from.  Most of these can also be used once you create an empty project.  On the next dialogue, pick farm solution.  I’ll go into the difference between sandboxed and farm solutions, but more than likely you are going to use farm solutions every time.  You also need to specify the URL to your server.  You can change that if you want but the default value will probably work for you in this case.

WebPartIntroSolutionType

This gives us a solution that looks like this.

WebPartIntroSolutionExplorer

Now we are ready to build our new web part.  If you bring up the add new item context menu, you will see a number of choices for the types of new SharePoint Project Items (SPIs) that you can create.  We’re going to choose Web Part in this case.

WebPartIntroSPI

What is the Visual Web Part you ask?  That’s just a user control which relates directly to my second most popular post on How to Deploy a User Control.  Now we’re finally ready to add some code.  We’re just going to take our code from the WSS3 post and use it here.

using System;

using System.ComponentModel;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

 

namespace SharePointProject1.TestWebPart

{

    [ToolboxItemAttribute(false)]

    public class TestWebPart : WebPart

    {

        public TestWebPart()

        {

        }

 

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            Controls.Add(new Label() { Text = "My Test SharePoint 2010 Web Part (Hello World)!" });

        }

 

        protected override void RenderContents(HtmlTextWriter writer)

        {

            base.RenderContents(writer);

        }

    }

}

The only line of code I added here was the line to add the label and set its text.  Everything else came from the template.

Describing the Web Part

In my WSS3 post, this is where I talked about building a .webpart file.  Well, you don’t need to worry about that any more as Visual Studio creates it for you.  Here is what solution explorer looks like after you add your first web part.

WebPartIntroSolutionExplorer2

As you can see the .webpart file is already there as well as an elements.xml file for a feature to deploy the web part.  The WSS3 post went on to talk about all of the things you need to know about building a feature.  This is still good stuff to know, but its already taken care of for you.  If you want to edit the basic feature information, just open it up in solution explorer and you get a nice new interface that looks like this.

WebPartIntroFeatureEditor

I’m not going to waste space showing you the insides of the files it creates for you.  Just know it creates them for you and it saves you a ton of time.

Deploying via Solution Package

In my WSS3 post, I explained how to create a cab.ddf and manifest.xml file.  Well guest what?  That is taken care of for you now as well.  The Package.package file in the solution explorer provides another nice editor which allows you to choose with files go into the package.  You don’t have to keep track of a thing any more, it just builds the package and takes care of it for you.

WebPartIntroPackageEditor

At this point, Visual Studio has created the .webpart file, the feature, and the solution package.  However, we still need to deploy it and if we could debug it that would be even cooler right?  Take a look at our new options in the Build menu.

WebPartIntroBuildMenu

We can build and rebuild just like any other project, but notice the options for Deploy, Package, and Retract.  Those are all SharePoint functions.  In this case, I want to deploy my solution.  Choosing deploy, we see the following in the output window.

------ Build started: Project: SharePointProject1, Configuration: Debug Any CPU ------
  SharePointProject1 -> C:\Code\SharePointProject1\bin\Debug\SharePointProject1.dll
  Successfully created package at: C:\Code\SharePointProject1\bin\Debug\SharePointProject1.wsp
------ Deploy started: Project: SharePointProject1, Configuration: Debug Any CPU ------
Active Deployment Configuration: Default
Run Pre-Deployment Command:
  Skipping deployment step because a pre-deployment command is not specified.
Recycle IIS Application Pool:
  Recycling IIS application pool 'SharePoint - 80'...
Retract Solution:
  Skipping package retraction because no matching package on the server was found.
Add Solution:
  Adding solution 'SharePointProject1.wsp'...
  Deploying solution 'SharePointProject1.wsp'...
Activate Features:
  Activating feature 'Feature1' ...
Run Post-Deployment Command:
  Skipping deployment step because a post-deployment command is not specified.
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========

From inspecting the text of the log, you can see that Visual Studio compiled, created a package, reset my Application Pool, Added the Solution, Deployed the Solution, and activated the feature.  Let’s check SharePoint and see if it’s really there.

WebPartIntroGallery

Checking the web part gallery, we see our .webpart file.  Let’s add it to a page and see how it looks.  Edit any page and use add a web part and you will see the new interface for choosing a web part.  It puts it in the Custom group by default.

WebPartIntroAddWebPart

One you hit and add finish editing, we see the web part working correctly.

WebPartIntroWorking

You have to admit this is quite a bit easier than deploying a web part in SharePoint 3.  What if you want to debug though?  No problem.  Just set a breakpoint and choose debug from the build menu like you would any other type of project.

WebPartIntroBreakPointHit

As I mentioned earlier, if you are familiar with SharePoint 2010, this is nothing new to you.  However, my point today is for those who shied away from SharePoint in the past because the development experience was far from optimal.  Try it for yourself and you will see how easy it is to get up and running with your code.  Even with pictures this post is half the size of the WSS3 post.  That’s because it really is just that easy.  I really think Visual Studio 2010 will open the way for a new round of SharePoint developers.  Try it out today.

Comments

# SharePoint 2010 solution package deployment: sandboxed vs farm solutions) « Uscatu's blog

Pingback from  SharePoint 2010 solution package deployment: sandboxed vs farm solutions) «  Uscatu's blog

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Thursday, April 15, 2010 10:28 AM by Jasonat

Great Article.

Lets say I have created a web part on site A.  Now I wish to use the same webpart on another site B which can be on the same server, or different server.  What do you do in this case?

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Thursday, April 15, 2010 10:39 AM by CoreyRoth

@Jasonat Well, you can take the .wsp file it packages and install it on the other server using stsadm or PowerShell.  You can also just update the server reference found when you click on the project and look at its properties and point it to the URL of the other site.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Sunday, August 22, 2010 2:50 AM by BibhuK

I am learning SP 2010 (specially FAST search part) from last  few weeks , This is very helpful ! Thank you so much !

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Monday, December 6, 2010 4:02 AM by Manas Mondal

This good post for first hello world.

Thanks !!

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, December 29, 2010 2:59 AM by Janet

This blog is very helpful for me. Thank you so much:)

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, January 12, 2011 11:11 AM by Nik

Great post, thank you!

# deploy web part as a feature code « Dot Net Solutions

Monday, January 31, 2011 9:11 AM by deploy web part as a feature code « Dot Net Solutions

Pingback from  deploy web part as a feature code « Dot Net Solutions

# Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part « Dot Net Solutions

Pingback from  Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part « Dot Net Solutions

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, April 13, 2011 1:14 PM by Gerd

I have an XML file that is a settings file that is used as part of my web part. It is mapped to the layouts folder. When I update this settings file and redeploy the solution the settings do not change on the page where the web part is deployed.  I have all caching turned off including browsers. This issues is driving me batty and holding up my project. Can anyone provide any insight?

Thanks

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, April 13, 2011 2:01 PM by CoreyRoth

Unfortunately, deploying a new XML file will have no effect on anything that has already been deployed.  You'll probably have to look for a solution involving code.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Thursday, April 14, 2011 4:55 AM by zewl

Great article.

What in case, I want to reference other assembly within project with webparts? Deploy is ok, but while loading the page on Sharepoint I got IO exception: System.IO.FileNotFoundException: Could not load file or assembly ' ...... ' etc.

It's suprising, that deploy doesn't include publishing referenced assemblies. Now, where to manually copy referenced assemblies? I have access to file system of server.

Thanx for reply.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Friday, April 15, 2011 9:30 AM by CoreyRoth

@Zewl Did you add the referenced assembly in the Package Editor as well?  Just referencing it in your project isn't enough.  Open your package file and click on the references tab I believe.  There you can add your other assemblies for deployment.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Tuesday, April 19, 2011 8:06 AM by Gerd

Thanks for the reply CoreyRoth. I ended up putting the XML file in the assets folder. That took care of the issue I was having. I think this is where it should have been in the first place.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, June 8, 2011 12:27 AM by Deepa

Hi,

I wish to thank you for this article. A great one at that.

Here I face a small problem. I followed all the steps exactly as in here. And the solution on deploy gives me the same message but somehow i am unable to see the webpart in the gallery and therefore no custom folder in the webpart categories. What could be the possible issue with it not showing up?

Thanks,

Deepa

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Monday, June 20, 2011 4:17 PM by CoreyRoth

@Deepa It sounds like there is an issue with your manifest.xml file.  Double check the module element in this file and verify that it's going to the correct folder and it's going to the solutions gallery.  You can verify the file by looking at an old 2007 solution such as in my previous post.

www.dotnetmafia.com/.../intro-to-sharepoint-development-how-to-build-and-deploy-a-web-part.aspx

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Sunday, July 10, 2011 8:20 PM by Dave Stuart

Wow, what a difference from WSS 3.0! Thanks for the Post!

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Thursday, September 15, 2011 4:54 PM by Paul

Thanks for the great post, Corey. I have one question: Once we create a web part, we can add that web part in the Web part gallery in the Share Point 2010. Now we can able to insert that web part on any page on the Share Point Server.

Let's say I added that web part on 5 different pages on the same Share Point server. I modified the web part for some reason and imported again in the gallery; does it supposed to update all the other pages with the same web part automatically or I have to update each page separately?

For some reason it does not work for me, please advise.

Thanks in advance

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Thursday, September 15, 2011 5:37 PM by CoreyRoth

@Paul If you update the code to the web part itself, then yes, all pages will call the new code and you will see the results.  However, if you make changes to one of the properties on the web part file, you will not see those changes necessarily on each individual deployed page.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Monday, September 19, 2011 2:08 PM by Dani

When i try and add the web part i get the following error

"A web part or web form contol on this page cannot be displayed or imported.  The type could not be found or it is not registered as safe."  Any idea how to resolve this?

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Tuesday, September 20, 2011 4:04 PM by CoreyRoth

@Dani see this post for troubleshooting techniques.

www.dotnetmafia.com/.../troubleshooting-cannot-import-web-part-error.aspx

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Tuesday, September 20, 2011 11:34 PM by Syahri Ramadhan

Sir, your blog articles are very, very  helpful...thank you so much, may GOD always bless you with prosperity and healthiness..

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Friday, October 7, 2011 8:40 AM by John

Thanks for Excellent Article. I have a question. I have a .wsp file. What are the steps to be taken while handing over .wsp file to be run on production by system administrator. And also steps to roll back if the .wsp fails.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Thursday, October 13, 2011 6:24 AM by Sam

thanks for sharing this Post......Keep posting ...

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Thursday, October 13, 2011 7:59 AM by Alan Glover

Hi Corey,  

Your post got me started a few weeks ago and I now have a collection of custom webparts that contain multiple webcontrols and database access and so on.  So thanks!

My problem now is that IE grows to immense proportions when I use my web part pages.  So clearly I should be disposing more than I am.  Simple question to start with:  your example adds a Label control.  A Label control has a dispose method.   Should you be calling it at some point and if so where?

thanks

Alan

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Thursday, October 13, 2011 6:35 PM by Sailash

Hi, this Blog is a real help for us, I have a issue with my share point web part application, every thing works fine as you said, but when i try to deploy , it fails in recyle IIS step , It says i don't have sufficient Rights, But strangly, i am Adminstrator in this machine, is there any setting i am missing ??

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Monday, October 17, 2011 9:39 PM by CoreyRoth

@John when you are ready to go, your system administrator can install the solution package using PowerShell.  Just give your admin a copy of the .wsp file.  I have more details in the post below.

www.dotnetmafia.com/.../adding-and-deploying-solutions-with-powershell-in-sharepoint-2010.aspx

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Monday, October 17, 2011 9:48 PM by CoreyRoth

@Alan No, you shouldn't have to dispose of the control.  You may try disabling ViewState on controls that do not require it.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Monday, October 17, 2011 9:50 PM by CoreyRoth

@Sailash This is a common issue.  If I remember right the cause of the issue is not having sufficient permissions in the SQL Server content databases for the given web application.  I think you can resolve it by granting db_owner, but I could be remembering incorrectly.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, October 26, 2011 3:01 AM by Alan Glover

Tim Boothby solved my memory problem its a bug with Sharepoint 2010 when using IE8

I can give you slightly more specific informaiton. The problem is associated with an IE addin NameCtrl Class which is a part of SharePoint that handles presence information. You can workaround this issue either by disabling just this addon in IE or by turning off presence information at the SharePoint site level. To do this go to Central Administration -> Application Management -> Manage Web Applications -> Select your site -> General Settings -> Enable additional actions and Online Status for members -> No

More information in this thread -

social.msdn.microsoft.com/.../87ced99d-8a1a-42b1-9513-1ffa59c8fd8d

Tim

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, November 9, 2011 9:49 AM by Harshal

I read your post and it was very helpful but I am trying something else.

I have a solution built in VS 2010 and I have choosen the deploy solution directly to the sharepoint server. Now the issue is I have made few changes and the build was also successful, I want to deploy the solution to the Sharepoint server I am afraid it might crash the site as it is a production environment.

Please guide me in this.

Thanks in advance.

Harshal

# Br??kdyr caf??mad i Vestjylland og ??vrige nyheder « Dagbog « IHD Blog

Pingback from  Br??kdyr caf??mad i Vestjylland og ??vrige nyheder « Dagbog « IHD Blog

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, December 7, 2011 8:24 PM by CoreyRoth

@Harshal you should do your development on a different server (other than production) if possible.  Once you are ready to deploy to production, you can use the .wsp package that Visual Studio produces to deploy your code to the new server.  Since you are in production now, you should only make change during shceduled downtime as deploying the solution will cause a bit of downtime.

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, May 30, 2012 6:46 AM by pearl

Hi all,

  IS it possible to deploy a website which is created using Central Administration , just like an example of intranet portal...using site collection , pages and lists and customise the pages contents as required..

I have created the site but now how do i deploy i m nt understanding plz help

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, May 30, 2012 8:08 AM by CoreyRoth

@Pearl so you have create a site on one server and want to move it to another one now?

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Tuesday, October 23, 2012 2:17 AM by Nick Johnson

To build and deploy a web part is an inmportant part of sharepoint development.

This article teach you this in a very simple and easy to understand way.....

# Need help in share point 2010 | Jptab

Saturday, November 9, 2013 3:52 AM by Need help in share point 2010 | Jptab

Pingback from  Need help in share point 2010 | Jptab

# How do we call a slight that is in a webpart category file, from a usercontrol in SharePoint 2007? | Zepp

Pingback from  How do we call a slight that is in a webpart category file, from a usercontrol in SharePoint 2007? | Zepp

# Sharepoint 2010 Development Environment Set-up | Ziccardi

Thursday, November 20, 2014 3:05 AM by Sharepoint 2010 Development Environment Set-up | Ziccardi

Pingback from  Sharepoint 2010 Development Environment Set-up | Ziccardi

# re: Intro to SharePoint 2010 Development: How to Build and Deploy a Web Part

Wednesday, January 21, 2015 12:05 PM by Punit

Super article...friends I want to appear for Developing MS SharePoint server 2013. I need ur suggestions and sample questions for preparing

# How To Deploy Sharepoint Web Parts | Adsense

Tuesday, August 11, 2015 5:26 PM by How To Deploy Sharepoint Web Parts | Adsense

Pingback from  How To Deploy Sharepoint Web Parts | Adsense

Leave a Comment

(required)
(required)
(optional)
(required)