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) 

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Description="Hands On Feature"
    <ElementManifest Location="element.xml"/>
    <ElementManifest Location="handsOnWebpart.xml"/>
    <ElementFile  Location="me.aspx"/>
    <ElementFile  Location="handsOnWebpart.webpart"/>

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   Title="Hands On Custom Actions"
   Description="Getting up and going with inline code" >
    <UrlAction Url="http://www.google.com"/>

  <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=, 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>]]>        

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

    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()

            // 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)";

dwp file (webpart file): this file is referenced in manifest.xml
<?xml version="1.0" encoding="utf-8" ?>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
      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>
        <property name="Title" type="string">WebPart1 Web Part</property>
        <property name="Description" type="string">WebPart1 Description</property>

handsOnWebpart.xml: this file referenced in feature.xml
<?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" />

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

No comments: