Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, December 01, 2011

Deploying WebPart as Feature in SharePoint2007



Deploying WebPart as Feature in SharePoint2007
1.       Open the server.
2.       Go to this path “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\”
3.       Create a folder with appropriate name under the above path.
4.       Now create an old feature.xml & do necessary changes.
5.       Now create another folder with the same name and copy your .webPart file into that and change the public key token in that.
6.       Create another file in the internal folder and copy an xml file with the name of the folder we have created and do the necessary changes.
7.       Do the necessary changes in that.
8.       Copy the dll in the gac.
9.       Copy safe control in web.config.
10.   Now install a feature using stsadm.
11.   Now activate a feature using stsadm.
12.   Now do iisreset.
Note:-
1.       The ID within a feature.xml should be changed.
2.       The ID inside a xml file inside the internal folder should also be changed.




Code Sample
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Data;
[assembly:AllowPartiallyTrustedCallers]
namespace SampleFeatureWebPart
{
    [Guid("28b57151-4008-42fe-9abb-c5f09f33bff2")]
    public class SampleFeatureWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        string strError = string.Empty;
        GridView dgvClients;
        public SampleFeatureWebPart()
        {
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(strError);
            try
            {
                dgvClients.RenderControl(writer);
            }
            catch (Exception ex)
            {
                writer.Write(ex.ToString());
            }
        }
        protected override void CreateChildControls()
        {
            try
            {
                dgvClients = new GridView();
                dgvClients.ID = "dgvClients";
                dgvClients.AutoGenerateColumns = false;

                BoundField bTitle = new BoundField();
                bTitle.HeaderText = "Title";
                bTitle.DataField = "Title";
                dgvClients.Columns.Add(bTitle);

                BoundField bCategory = new BoundField();
                bCategory.HeaderText = "Category";
                bCategory.DataField = "Category";
                dgvClients.Columns.Add(bCategory);

                this.Controls.Add(dgvClients);

                SPWeb currentWeb = SPControl.GetContextWeb(Context);
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "";
                SPList lstNewConcepts = currentWeb.Lists["New Comcepts Implemented"];
                SPListItemCollection myColl = lstNewConcepts.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    dgvClients.DataSource = myColl.GetDataTable();
                    dgvClients.DataBind();
                }
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
           
        }
    }
}

Procedure
Feature.xml Example
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="55598c44-ce2c-4a6b-bd2d-d03d3b1d0f42" Title="SampleFeatureWebPart" Scope="Site" Version="1.0.0.0" Hidden="FALSE" DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="SampleFeatureWebPart\SampleFeatureWebPart.xml" />
    <ElementFile Location="SampleFeatureWebPart\SampleFeatureWebPart.webpart" />
  </ElementManifests>
</Feature>

Modifications you have to do in .WebPart file which you had copied to internal folder.
<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <!--
      The following Guid is used as a reference to the web part class,
      and it will be automatically replaced with actual type name at deployment time.
      -->
      <type name="SampleFeatureWebPart.SampleFeatureWebPart,SampleFeatureWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" />
      <importErrorMessage>Cannot import SampleFeatureWebPart Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">SampleFeatureWebPart Web Part</property>
        <property name="Description" type="string">SampleFeatureWebPart Description</property>
      </properties>
    </data>
  </webPart>
</webParts>

The xml file you created in internal folder
<?xml version="1.0" encoding="utf-8"?>
<Elements Id="1114ef7b-9ca7-4d14-9047-58f333a5c6af" xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="WebParts" List="113" Url="_catalogs/wp">
    <File Path="SampleFeatureWebPart\SampleFeatureWebPart.webpart" Url="SampleFeatureWebPart.webpart" Type="GhostableInLibrary" />
  </Module>
</Elements>


Statement to install a feature via stsadm
STSADM.EXE -o installfeature -filename SampleFeatureWebPart\feature.xml –force
Statement to activate a feature via stsadm
stsadm -o activatefeature -filename SampleFeatureWebPart\feature.xml -url “http:/vlnrm:100/
Statement to deactivate a feature via stsadm
stsadm -o deactivatefeature -filename SampleFeatureWebPart\feature.xml -url "http:/vlnrm:100//"

No comments:

Post a Comment