Tuesday, September 23, 2008

SharePoint: Deploying WebPart/Feature as SharePoint Solution Package (.wsp file) - Part 2

Link to Part 1 : Creating Skeleton.
Link to Part 2 : Creating Features/Webparts (current post)
Link to Part 3 : Creating .ddf and Manifest.xml
Link to Part 4 : Using STSADM and Centeral Admin to add and deploy solution

Once we have out skeleton ready we can start filling it with features, webparts, assemblies, pages, etc...

Lets start with creating a feature:
create similar file structure as shown in figure above (this is bcoz, ddf file maintains mapping based on this file structure) 

feature.xml:
[code]
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Id="{C719D841-1F4C-4e5c-8286-BDDD5A5C3350}"
         Hidden="FALSE"
         Description="Hands On Feature"
         Title="HandsOnFeature"
         Scope="Site">
  <ElementManifests>
    <ElementManifest Location="element.xml"/>
    <ElementManifest Location="handsOnWebpart.xml"/>
    <ElementFile  Location="me.aspx"/>
    <ElementFile  Location="handsOnWebpart.webpart"/>
  </ElementManifests>
</Feature>
[/code]

element.xml
[code]
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
   Id="HandsOnCustomActions"
   GroupId="SiteActions"
   Location="Microsoft.SharePoint.StandardMenu"
   Sequence="2000"
   Title="Hands On Custom Actions"
   Description="Getting up and going with inline code" >
    <UrlAction Url="http://www.google.com"/>
  </CustomAction>

  <Module Name="mymodule" List="101" Url="Pages">
    <File Url="me.aspx" Path="me.aspx" Type="GhostableInLibrary">      
        <AllUsersWebPart WebPartZoneID="TopZone" WebPartOrder="1">
          <![CDATA[ <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image"> <Assembly>HandsOnWsp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=35651a817c3c3a2e</Assembly> <TypeName>ClassLibrary1.handsOnWebpart</TypeName> <FrameType>None</FrameType> <Title>Hands On Webpart</Title> <iwp:ImageLink>/_layouts/images/homepage.gif</iwp:ImageLink> <iwp:AlternativeText>$Resources:core,sitelogo_wss;</iwp:AlternativeText> </WebPart>]]>        
      </AllUsersWebPart>
    </File>
  </Module>
  
</Elements>
[/code]

creating a webpart: 
in a similar fashion create webparts structure as shown in figure above:

handsOnWebPart.cs
[code]
    [Guid("70C2E501-C221-4ca3-91DB-78A4785DA79D")]
    public class handsOnWebpart : Microsoft.SharePoint.WebPartPages.WebPart 
    {
/*inherit your webpart class from 'Microsoft.SharePoint.WebPartPages.WebPart' as sharepoint fails to load webpart on aspx pages which are not derived from 'Microsoft.SharePoint.WebPartPages.WebPart'. If you recall, in our element.xml we created a module element which contains file element refering to aspx file, which further contains AllUsersWebPart element specifying webzone ID and other details. Now, if our webpart was defived from 'System.Web.UI.WebControls.WebParts.WebPart' sharepoint would throw error on feature activation*/
        public handsOnWebpart()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // TODO: add custom rendering code here.
            System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
            label.Text = "Hello World - Generate via Hands On Webpart (with DLL placed in web app bin)";
            this.Controls.Add(label);
        }
    }
[/code]

dwp file (webpart file): this file is referenced in manifest.xml
[code]
<?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="70C2E501-C221-4ca3-91DB-78A4785DA79D" />
      <importErrorMessage>Cannot import WebPart1 Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">WebPart1 Web Part</property>
        <property name="Description" type="string">WebPart1 Description</property>
      </properties>
    </data>
  </webPart>
</webParts>
[/code]

handsOnWebpart.xml: this file referenced in feature.xml
[code]
<?xml version="1.0" encoding="utf-8" ?>
<Elements Id="70C2E501-C221-4ca3-91DB-78A4785DA79D" xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="WebParts" List="113" Url="_catalogs/wp">
    <File Path="handsOnWebpart.webpart" Url="handsOnWebpart.webpart" Type="GhostableInLibrary" />
  </Module>
</Elements>
[/code]

In my next post I will discuss on how to create ddf file, manifest file, add and deploy wsp solution.

No comments: