in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

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

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 said:

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

April 8, 2010 8:01 AM
 

Jasonat said:

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?

April 15, 2010 10:28 AM
 

CoreyRoth said:

@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.

April 15, 2010 10:39 AM
 

BibhuK said:

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

August 22, 2010 2:50 AM
 

Manas Mondal said:

This good post for first hello world.

Thanks !!

December 6, 2010 4:02 AM
 

Janet said:

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

December 29, 2010 2:59 AM
 

Nik said:

Great post, thank you!

January 12, 2011 11:11 AM
 

deploy web part as a feature code « Dot Net Solutions said:

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

January 31, 2011 9:11 AM
 

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

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

February 9, 2011 9:19 AM
 

Gerd said:

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

April 13, 2011 1:14 PM
 

CoreyRoth said:

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.

April 13, 2011 2:01 PM
 

zewl said:

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.

April 14, 2011 4:55 AM
 

CoreyRoth said:

@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.

April 15, 2011 9:30 AM
 

Gerd said:

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.

April 19, 2011 8:06 AM
 

Deepa said:

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

June 8, 2011 12:27 AM
 

CoreyRoth said:

@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

June 20, 2011 4:17 PM
 

Dave Stuart said:

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

July 10, 2011 8:20 PM
 

Paul said:

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

September 15, 2011 4:54 PM
 

CoreyRoth said:

@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.

September 15, 2011 5:37 PM
 

Dani said:

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?

September 19, 2011 2:08 PM
 

CoreyRoth said:

@Dani see this post for troubleshooting techniques.

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

September 20, 2011 4:04 PM
 

Syahri Ramadhan said:

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

September 20, 2011 11:34 PM
 

John said:

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.

October 7, 2011 8:40 AM
 

Sam said:

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

October 13, 2011 6:24 AM
 

Alan Glover said:

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

October 13, 2011 7:59 AM
 

Sailash said:

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 ??

October 13, 2011 6:35 PM
 

CoreyRoth said:

@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

October 17, 2011 9:39 PM
 

CoreyRoth said:

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

October 17, 2011 9:48 PM
 

CoreyRoth said:

@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.

October 17, 2011 9:50 PM
 

Alan Glover said:

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

October 26, 2011 3:01 AM
 

Harshal said:

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

November 9, 2011 9:49 AM
 

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

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

November 12, 2011 3:21 AM
 

CoreyRoth said:

@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.

December 7, 2011 8:24 PM
 

pearl said:

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

May 30, 2012 6:46 AM
 

CoreyRoth said:

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

May 30, 2012 8:08 AM
 

Nick Johnson said:

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.....

October 23, 2012 2:17 AM
 

Need help in share point 2010 | Jptab said:

Pingback from  Need help in share point 2010 | Jptab

November 9, 2013 3:52 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an Applications Architect at Infusion specializing in ECM and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems