Cannot add/insert custom Webpart on Sharepoint Page: Error ...

Jan 12, 2010 at 8:34 AM


i am trying to build a custom Webpart based on the WSPBuilder Tool. My Environment is:

- Windows XP 32 bit

- VS 2008 w/ SP1

- WSPBuilder in the latest release


I startet building a custom WebPart with WSPBuilder.

Here are the Steps:

1. Start a new Project -> WSPBuilderProject

2. Add new Element "Webpart with Feature"

3. Add Solution Installer

4. Remove FarmId ... Line From Setup.exe.config File

5. Build WSP


With STSADM Addsolution and Delpoysolution - allowgacdeployment, immediate. The Webpart has the Site Scope. So i go to the Sitecollection Settings and activate my WebPart there. Its there! When i look at the Webpart List, its in the list, too. But when i now click on the WebPart ("WebPartFeature1.webpart) in the WebPart gallery i get the error:


Cannot import WebPartFeature1 Web Part. at Microsoft.SharePoint.WebPartPages.WebPartImporter.CreateWebPart(Boolean clearConnections)

at Microsoft.SharePoint.WebPartPages.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, Uri webPartPageUri, SPWeb spWeb)

at Microsoft.SharePoint.WebPartPages.WebPartPreview.get_WebPart()

at Microsoft.SharePoint.WebPartPages.WebPartPreview.CreateChildControls()

at System.Web.UI.Control.EnsureChildControls()

at System.Web.UI.Control.PreRenderRecursiveInternal()

at System.Web.UI.Control.PreRenderRecursiveInternal()

at System.Web.UI.Control.PreRenderRecursiveInternal()

at System.Web.UI.Control.PreRenderRecursiveInternal()

at System.Web.UI.Control.PreRenderRecursiveInternal()

at System.Web.UI.Control.PreRenderRecursiveInternal()

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


When i edit a Page and want to add this Webpart via the WebPart Gallery ( Its in the gallery under the group MyGroup )i get the following error:

WebPartFeature1: Cannot import WebPartFeature1 Web Part. (Its the Error defined in the .webpart File.)

I tried everything, changing the Namespace, the Classname, looking at the SafeControls in web.config, the DLL File is also in the GAC deployed with the correct version, etc.

Like explained above, i didnt changed anything of the files geenrated by the wspbuilder tool.


What do you think? Anyone knows this Problem?

Jan 12, 2010 at 10:04 AM
Edited Jan 12, 2010 at 12:18 PM

i have found a "Workaround" for my problem. It is very curious! See:

1. I install my Webpart through the SharepointInstaller which i have created with via VS2008 -> New Element -> WSPBuilder Solution Installer
2. WSPBuilder -> Create Deployment Folder
3. The WebPart is installed and i can see it under Sitesettings -> Webparts and DELETE my Webpart here
4. Click on New* -> Select my Webpart and check "Overwrite when exist"

Then it works!!!!

Whats wrong? Here my codefiles:


<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="53524872-6d26-49ea-9d57-399778e7a189"
          Description="Description for WebPart1"
    <ElementManifest Location="elements.xml"/>
    <ElementFile Location="WebPartFeature1.webpart" />


<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="">
  <Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE">
    <File Url="WebPartFeature1.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="MyGroup"></Property>
      <Property Name="QuickAddGroups" Value="MyGroup" />


<?xml version="1.0" encoding="utf-8" ?>
  <webPart xmlns="">
      <type name="MyNamespace.WebPartFeature1, WSPBuilderProject1, Version=, Culture=neutral, PublicKeyToken=642caf6ec46008bf" />
      <importErrorMessage>Cannot import MyWebPart Web Part.</importErrorMessage>
        <property name="Title" type="string">MyWebPart</property>
        <property name="Description" type="string">Description for WebPart1</property>


Imports System
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Web.UI
Imports System.Web.UI.WebControls.WebParts

Namespace MyNamespace
    <Guid("8db13781-ef6e-4ffd-8247-3ba2353fa30e")> _
    Public Class WebPartFeature1
        Inherits Microsoft.SharePoint.WebPartPages.WebPart
        Private _error As Boolean = False
        Private _myProperty As String = Nothing

        <Personalizable(PersonalizationScope.Shared)> _
        <WebBrowsable(True)> _
        <System.ComponentModel.Category("My Property Group")> _
        <WebDisplayName("MyProperty")> _
        <WebDescription("Meaningless Property")> _
        Public Property MyProperty() As String
                If _myProperty = Nothing Then
                    _myProperty = "Hello SharePoint"
                End If
                Return _myProperty
            End Get
            Set(ByVal value As String)
                _myProperty = value
            End Set
        End Property

        Public Sub New()
            Me.ExportMode = WebPartExportMode.All
        End Sub

        ''' <summary>
        ''' Create all your controls here for rendering.
        ''' Try to avoid using the RenderWebPart() method.
        ''' </summary>
        Protected Overloads Overrides Sub CreateChildControls()
            If Not _error Then


                    ' Your code here...
                    Me.Controls.Add(New LiteralControl(Me.MyProperty))
                Catch ex As Exception
                End Try
            End If
        End Sub

        ''' <summary>
        ''' Ensures that the CreateChildControls() is called before events.
        ''' Use CreateChildControls() to create your controls.
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overloads Overrides Sub OnLoad(ByVal e As EventArgs)
            If Not _error Then

                    ' Your code here...
                Catch ex As Exception
                End Try
            End If
        End Sub

        ''' <summary>
        ''' Clear all child controls and add an error message for display.
        ''' </summary>
        ''' <param name="ex"></param>
        Private Sub HandleException(ByVal ex As Exception)
            Me._error = True
            Me.Controls.Add(New LiteralControl(ex.Message))
        End Sub
    End Class
End Namespace


<?xml version="1.0" encoding="utf-8" ?>
    <add key="BannerImage" value="Default"/>
    <add key="LogoImage" value="None"/>
    <add key="EULA" value="EULA.rtf"/>
    <add key="SolutionId" value="ab6d150c-3603-46b6-a84e-6a53bcfe4684"/>
    <add key="SolutionFile" value="WSPBuilderProject1.wsp"/>
    <add key="SolutionTitle" value="WSPBuilderProject1"/>
    <add key="SolutionVersion" value=""/>
    <add key="UpgradeDescription" value="Upgrades {SolutionTitle} on all frontend web servers in the SharePoint farm."/>
    <add key="RequireDeploymentToCentralAdminWebApplication" value="true"/>
    <add key="RequireDeploymentToAllContentWebApplications" value="false"/>    
Jan 12, 2010 at 12:19 PM

Now i tried everything step by step again. When i deploy via the setup.exe my webpart i get now an error like "the webpart could not be imported. it couldnt find or it is not registered as safe".

But after deleting it from the webpart gallery and installing it via NEW* Function again with checking the overwrite checkbox everything works fine!

I dont know what it could be and why this happens. I would lilke to understand what goes wrong here. My complete code is above.

Apr 30, 2010 at 7:50 PM

I'm having this same issue only I haven't used WSP Builder...I'm creating the .wsp manually.  Any progress on your situation?