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.
OutPut :
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 Here.
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 deploymenttarget . 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.
If you wish to deploy some files like css, .js or images along with this webpart solution refer to my post below:
Deploy files to 12 hive with webPart
OutPut :
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 Here.
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
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\
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.
If you wish to deploy some files like css, .js or images along with this webpart solution refer to my post below:
Deploy files to 12 hive with webPart
This comment has been removed by a blog administrator.
ReplyDeletegreat
ReplyDeletethanks for sharing.
ReplyDeleteHi,
ReplyDeleteI have a problem that i can not reseolve it
I have a NewForm (his name :Departement)in which there are many columns and a button ,when I clicked on the button another NewForm appeared in which I found a lookupfield and when I selected an item from this lookupfield ,all the information related to this item where displayed in the first NewForm (his name :Departement) in a grid
I have not idea to do it :(
Please help me
Thanks