Wednesday, January 04, 2012

Same .webpart url in Web Part Gallery causes weirdness

I ran into an interesting issue today, and although I haven't fully tested this yet, I think I now get what is going on.

I was working on refactoring a number of web parts for a customer, they had a less experienced SharePoint dev create a bit of a mess instead of a proper WSP, and so my first task was to create a proper SharePoint project in Visual Studio and include the necessary web parts. No issues, all worked on my clean test environment.

I then proceed to deploy my shiny new WSP on the test server, and while most things work as expected, the web parts that I included are not available. The feature that they are part of is activated so I am stumped for a minute. Quickly I start to suspect that this has something to do with the old web parts, but my new ones are in a different assembly, different namespace etc etc. After some poking around in the xml, I realize that both my web parts and the old versions have the same name, and thus deploy to the same URL in the web part gallery. This can be seen in the elements.xml file for the web part.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="MyWebPart" List="113" Url="_catalogs/wp">
    <File Path=MyWebPart\MyWebPart.webpart" Url="MyWebPart.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="JoeTest" />
    </File>
  </Module>
</Elements>

A few quick tests on my local machine confirm that such URL collisions will cause problems, on my local machine it looked like web parts were being overwritten by the latest feature to deploy, but on the test server the old web parts persisted. This needs some more testing to fully understand what the intended behavior is, and whether it is something permissions related.

Nevertheless, as soon as I updated my URL deployment location, I was able to deploy the new web parts to the test server and use them on pages.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="MyWebPart" List="113" Url="_catalogs/wp">
    <File Path=MyWebPart\MyWebPart.webpart" Url="new_MyWebPart.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="JoeTest" />
    </File>
  </Module>
</Elements>

I realize that it probably isn't best practice to have web parts with the same name, etc. but trust me that I have a good reason for doing this. In any case, after some thought it makes perfect sense that SharePoint will not allow us to deploy two .webpart files to the same URL, and so a change to the URL is needed to work around this.