Friday, September 10, 2010

SharePoint Incompatible Web Part markup detected

I was working on my Game of Life SharePoint 2010 taxonomy sample, when I suddenly started to get this error message when I tried to add my web part to a page:

“Incompatible Web Part markup detected. Use *.dwp Web Part XML instead of *.webpart Web Part XML.”

image

The problem is that the .NET framework web part and SharePoint web parts are not the same thing. If you’re deriving your web part from System.Web.UI.WebControls.WebParts.WebPart, then you are using the .NET web part. But if you’re using Microsoft.SharePoint.WebPartPages.WebPart, then it’s a SharePoint web part.

As far as I can tell, you can actually use either one inside SharePoint, but there are differences in the way that you code them. If you’re using a SharePoint web part, then you should have a .dwp file in your feature. If you’re using the .NET class, then it should be .webpart. The error about the web part markup occurs when you try to use the wrong one. The .dwp and .webpart files are both XML, but they use a different schema. So if you change from one to the other, you can’t just rename the file, you have to re-write it.

I don’t know why my project suddenly decided that it didn’t want to work. As far as I can remember, all I did was change the assembly version number, but anyway…

To resolve the issue, I had to ensure that I was consistent across my project. I want to use the .NET webpart class, so this is what the beginning of my webpart.cs file looks like. I’ve used the long format for the WebPart class to make things crystal clear:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

//Added references
using System.Xml.Serialization;

namespace GameOfLifeWebPartProject.GameOfLifeWebPart
{   
[ToolboxItemAttribute(false)]  [DefaultProperty("Text"), ToolboxData("<{0}:GameOfLifeWebPart  runat=server></{0}:GameOfLifeWebPart>"), XmlRoot(Namespace = "GameOfLifeWebPart")]
public class GameOfLifeWebPart :
System.Web.UI.WebControls.WebParts.WebPart
    {

Now that I’ve clarified which type of web part I’m using, any custom properties that I add have to be in the correct format for that type of web part. If you add custom properties using the other format, the project may compile and deploy, but the properties won’t appear in the web part property pane.

// Custom web part property for the term group name
private string m_termStoreGroupName = "Game of Life";
[System.Web.UI.WebControls.WebParts.WebBrowsable(true),
System.Web.UI.WebControls.WebParts.WebDisplayName("Term Store Group Name"),
System.Web.UI.WebControls.WebParts.WebDescription("The name of the term store group you'll use."),
System.Web.UI.WebControls.WebParts.Personalizable(
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
System.ComponentModel.Category("Game of Life Settings"),
System.ComponentModel.DefaultValue("Game of Life")]
public string TermStoreGroupName
{
     get { return m_termStoreGroupName; }
     set { m_termStoreGroupName = value; }
}

This is what the same property would have looked like if I was using .dwp and the SharePoint web part class:

private string m_termStoreGroupName = "Game of Life";
[Category("Game of Life Settings")]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Term Store Group Name")]
[Browsable(true)]
[Description("The name of the m_group you'll use in your term store.")]
[DisplayName("Term Store Group Name")]
[XmlElement(ElementName = "TermStoreGroupName")]
public string TermStoreGroupName
{
     get { return m_termStoreGroupName; }
     set { m_termStoreGroupName = value; }
}

If you decide to change from .webpart to .dwp or vice versa, you’ll need to add the new file to your project and also update the feature package. If you don’t add the new file to the feature package, your web part won’t appear in the web part gallery because it requires that XML file.

1 comment:

Luca said...

Thank you for this very usefull article!
You helped me a lot!