Thursday, June 28, 2012

Incompatible Webpart Markup - .dwp vs .webpart

SharePoint 2010 allows us to extend out of the box web parts. When I try to inherit search box webpart (one of the out of the box webparts), and try to add new web part to a SharePoint page, I got this error:
“Incompatible Web Part markup detected. Use *.dwp Web Part XML instead of *.webpart Web Part XML.”






Problem:
Both .dwp and .webpart file types are used to describe where the code is for a web part. They are XML web part descriptors deployed with the web part. The difference is .dwp was the file extension used in version 2 of SharePoint and .webpart is a new extension used in version 3.  Inside the files, the schemas are different. Microsoft.SharePoint.Portal.WebControls.SearchBoxEx is one of the older ones and therefore requires a .dwp descriptor.
Solution:
We have to create a .dwp descriptor for the webpart.
1. Add a new XML file to the web part and give it the same name but with a .dwp extension




2. Add this content to the .dwp file
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
    <Assembly>$SharePoint.Project.AssemblyFullName$,Version=1.0.0.0,Culture=Neutral,PublicToken=6476a1ad7b812768</Assembly>
  <TypeName>SharePointProject1.ExtendedWebPart.ExtendedWebPart</TypeName>
  <Title>Extended SearchBox</Title>
  <Description>Extended SearchBox WebPart</Description>
</WebPart>

It is important to note that there are two main differences in the schema compared to the .webpart file;
·         We need the assembly's public token.

3. Set the Assembly version to your assembly's version. This is usually 1.0.0.0, but you can find it in AssemblyInfo.cs in your project.
4. Replace the PublicKeyToken
5. Replace the TypeName with the type name in the .webpart file.
6. Select the .dwp file in Solution Explorer and change its Deployment Type property from NoDeployment to ElementFile. This will ensure .dwp file is deployed with the web part.














7. Verify that Step 6 adds an entry to the .spdata file.






8. Delete the .webpart file. So the final view will be like this.



9. Edit Elements.xml file and replace both instances of .webpart with .dwp
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="ExtendedWebPart" List="113" Url="_catalogs/wp">
    <File Path="ExtendedWebPart\ExtendedWebPart.dwp" Url="ExtendedWebPart.dwp" Type="GhostableInLibrary">
      <Property Name="Group" Value="My Webparts" />
    </File>
  </Module>
</Elements>

10. Build and deploy the project
This is how I extended Microsoft.SharePoint.Portal.WebControls.SearchBoxEx. For the purpose I had to reference two assemblies Microsoft.Office.Server.Search.dll, microsoft.sharepoint.portal.dll from ISAPI folder (Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI).

1 comment:

Anonymous said...

Thank you! Saved lot of time.