Tuesday, April 8, 2008

WebPart File is not Overwritten After First Solution Deployment

We setup a nightly build process for a recent client which automagically builds and deploys a SharePoint solution.  Soon after it was implemented we realized that our (one) WebPart wasn't updating in the WebPart gallery of our target site.  In other words, the developers were checking in changes to the .webpart file in TFS, that .webpart file was being deployed to the target site, the feature was being reactivated without issue, but the .webpart file in the WebPart Gallery List remained unchanged. 

I suspect that this may have something to do with how we were doing successive deployments (using deploy/install instead of upgrade?), but I figured I'd come up with a solution to the problem within the feature itself.  Pretty simple, actually.  I wrote a piece of feature receiver code that, on feature deactivating, programmatically removes the WebPart file from the Web Part Gallery.

The Feature:

   1: <Feature
   2:   Id="7CAB976B-018D-4ec8-B1A2-354ED7645795"
   3:   Title="AIS Sample Hello World WebPart"
   4:   Description="Basic WebPart example."
   5:   Hidden="FALSE"
   6:   Scope="Site"
   7:   ImageUrl="actionsettings.gif"
   8:   ReceiverAssembly="AIS.SharePoint.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a961f5844c0e1ceb"
   9:   ReceiverClass="AIS.SharePoint.Utilities.EventReceivers.AisWebPartFeaturReceiver"
  10:   xmlns="http://schemas.microsoft.com/sharepoint/">
  11:   <ElementManifests>
  12:     <ElementManifest Location="ProvisionedFiles.xml"/>
  13:     <ElementFile Location="HelloWorld.WebPart" />
  14:   </ElementManifests>
  15:     <Properties>
  16:         <Property Key="WebPartFileName" Value="HelloWorld.WebPart"/>
  17:     </Properties>  
  18: </Feature>


The Code:



   1: public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
   2: {
   3:     try
   4:     {
   5:         SPFeatureProperty prop = properties.Feature.Properties["WebPartFileName"];
   6:         if (prop != null)
   7:         {
   8:             string filenameUpper = prop.Value.ToUpper();
   9:             SPWeb web = ((SPSite)properties.Feature.Parent).OpenWeb();
  10:             SPList webPartList = web.Lists["Web Part Gallery"];
  11:             int id = -1;
  12:             foreach (SPItem item in webPartList.Items)
  13:             {
  14:                 if (item["Name"].ToString().ToUpper() == filenameUpper)
  15:                 {
  16:                     id = item.ID;
  17:                     break;
  18:                 }
  19:             }
  20:  
  21:             if (id >= 0)
  22:             {
  23:                 webPartList.Items.DeleteItemById(id);
  24:                 System.Diagnostics.EventLog.WriteEntry(this.ToString(), "Successfully removed webpart file from webpart gallery.", System.Diagnostics.EventLogEntryType.Information);
  25:             }
  26:         }
  27:  
  28:     }
  29:     catch (Exception e)
  30:     {
  31:         System.Diagnostics.EventLog.WriteEntry(this.ToString(), "An error occurred during feature deactivation." + Environment.NewLine + e.ToString(), System.Diagnostics.EventLogEntryType.Error);
  32:     }
  33:  
  34: }

I obviously could have been a little more fancy with how I queried the list, etc, but I think this gets the point across...  If anyone has any input on items in a list not updating, please feel free to chime in.  I've seen similar behavior when deploying stylesheets, masterpages, etc, as part of a feature.


Hope this helps somebody!


UPDATE:  Props to my buddy Oskar for pointing me to the Windows Live Writer Code Snippet Plug-In.  And, uh... Do people still say 'Props'???

No comments:

Post a Comment