Search This Blog

Wednesday, April 6, 2011

Deploy Webpart as Solution Package and Feature

In this example we will create a SharePoint WebPart using VseWss, which will display data from a SharePoint list in a gridview control. After creating the webpart we will deploy the solution file i.e. .wsp file in our SharePoint site using stsadm.


To begin with you need to install the Visual studio extensions for Windows SharePoint Services 3.0 (VseWss) for your appropriate Visual Studio version.
You can download the latest one from this link.

Output:


Next thing that you have to keep in mind is that you are developing this solution on the server or machine which has WSS 3.0 installed in it.
Lastly, a Cutsom list named "Clients" should exist in your SharePoint site. Since I am using a Custom list called "Clients" and also a Custom column "Clients" in that list in my webpart.

Lets begin with the steps
1. Create a SharePoint WebPart Project. See the Screen below.

2. Now in your soluiotn explorer you would see a folder named "WebPart1". Letse just rename it to "MyCustomWebPart". Make sure you also rename all the files in the folder. Also, don't forget to change the url path in MyCustomWebPart.xml. So, we have somthing like below .

3. Now lets write some code in our MyCustomWebPart.cs . In this webpart we will create a Gridview control and will bind it to one of the columns in the SharePoint list. We have also used RunWithElevatedPrivileges so that the code runs under Application Pools identity and should not cause errors for the end users. The list we have used is called "Clients" and the Column in the list is also called "Clients".

Code :

namespace MyCustomWebPart
{
[Guid("f66be37b-91f9-4e99-97dc-0e30f6eb44ff")]
public class MyCustomWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
GridView myGrid;

public MyCustomWebPart()
{
}

protected override void CreateChildControls()
{
SPDataSource SPDataSource1 = new SPDataSource();
try
{
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
//Using RunWithElevatedPrivileges

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(mySite.ID))
{
using (SPWeb web = siteCollection.OpenWeb(myWeb.ID))
{
myGrid = new GridView();
myGrid.AutoGenerateColumns = false;

BoundField ClientName = new BoundField();
ClientName.HeaderText = "Our Clients";
ClientName.DataField = "Clients";
myGrid.Columns.Add(ClientName);

SPList List = myWeb.Lists["Clients"];
SPDataSource1.List = List;


myGrid.DataSource = SPDataSource1;
myGrid.DataBind();

Controls.Add(myGrid);

base.CreateChildControls();
}
}
});
}
catch { }
}
}
}

In above Code we have also used SPDatasource which will define the Source for Gridview control as SharePoint list.

Now lets just build the project. Please Note that building the project will not create a .wsp or solution package . To create a Solution file or .wsp file we will have to virtually Deploy the project. So, to do that just right click on the Project "MyCustomWebPart" and click on Depoly. See the screen below.



You will likely get an error at this point. Since, we have not specified the deployment target. Just ignore the error and grab the .wsp file. To see awhat all files are created click on "Show all Files" button on top the solution explorer.


Now you will see your .wsp file along with files like feature.xml and manifest.xml files.


Deployment : At this point we have two options for deploying this wsp or solution package in our sharePoint site.
1. Deploy using Visual Studio - This is quick and easy deployment. Just right click on you project and goto Properties. Goto the "Debug" tab and enter you SharePoint site's url . Save it and again hit the Deploy button. See the screen below.



2. We will deploy the wsp file using stsadm . To do this open a command prompt and navigate to the below path
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN.
Open this path in your windows explorer and drap and drop your wsp in here. See Screen below.



Lets write the command to deploy the webpart.



In this case you will just add the solution package in SharePoint's Solution store. You would then need to goto CentralAdmin->Operations->Solution Management and deploy the webpart. Just click on the webpart title in Solution management screen and you will see the below. Click on Deploy Solution and Choose the webapplictaion.


After you deploy : After yo deploy the webpart you will need to add it into webpart gallery. To do this Open your site and goto "SiteActions->Site Settings ->Modify All Site Settings ->WebParts. Click on New and select the webpart from the Screen. See below



 Now you are all set to add the webpart in your webpart page.

4 comments:

  1. After checking out a handful of the blog articles on your web page, I truly like your technique of
    blogging. I book-marked it to my bookmark site list and will
    be checking back in the near future. Take a look at my web site
    too and let me know how you feel.

    My web site; cash advance in an hour

    ReplyDelete
  2. Have you ever thought about adding a little bit more than just your articles?

    I mean, what you say is fundamental and everything.
    But just imagine if you added some great graphics or video clips to give your posts
    more, "pop"! Your content is excellent but with pics
    and clips, this site could definitely be one of the best in its niche.
    Excellent blog!

    My blog ... related web site

    ReplyDelete
  3. My partner and I stumbled over here from a different web address and thought
    I may as well check things out. I like what I see so now i am following you.
    Look forward to looking over your web page repeatedly.



    Feel free to visit my website: Orange County Catering

    ReplyDelete
  4. Hi there, I discovered your website via Google at the same
    time as looking for a similar topic, your web site got here up, it appears to be like good.
    I have bookmarked it in my google bookmarks.
    Hi there, simply became aware of your weblog thru Google, and found that it is really informative.
    I am gonna be careful for brussels. I will be grateful should you proceed
    this in future. Lots of people shall be benefited out of your writing.

    Cheers!

    Also visit my site Mortgage loans

    ReplyDelete