none
Error adding custom Webpart to page: WebPartFeature1: Cannot import WebPartFeature1 Web Part RRS feed

  • Question

  • Hi,

    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

    My Sharepoint Environment

    - Windows 2008 64 Bit
    - Sharepoint 2007 64 Bit latest CU w/ latest Sp

    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.

    The last thing what i think that could be is that i develop on 32 bit and the server is 64 bit.

    What do you think? Anyone knows this Problem?
    Best regards,
    Yavuz B.

    My Sharepoint Blog | http://www.sharepointmag.net
    Twitter | http://www.twitter.com/sharepointmag_t
    Monday, January 11, 2010 2:33 PM

Answers

  • Hi,

    I assume you have issue with the webpart file. When you delete the web part from gallery and reinstall that using the New (as described in step 4 above), then the webpart file is generated automatically for you. I suggest you to export the created webpart file (click the table icon right to the web part title to get the property sheet and then click the export button in the top menu) and compare that with your one using a tool. Or instead of exporting you can choose to display the XML and do the comparision "manually".

    Hope that helps.

    Peter
    • Marked as answer by SharepointMag Tuesday, January 12, 2010 1:53 PM
    Tuesday, January 12, 2010 1:33 PM

All replies

  • Has noone an idea?
    Best regards,
    Yavuz B.

    My Sharepoint Blog | http://sharepointkb10.wordpress.com
    Twitter | http://www.twitter.com/sharepointmag_t
    Tuesday, January 12, 2010 10:30 AM
  • Hi Yavouz,

    See the following thread:
    32bit webpart on 64bit Sharepoint
    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/720857c2-6380-4fae-9d49-f3d5d7600887/

    Could you post the relevant parts of web.config (SaveControls), web part xml file, and web part code?

    Peter
    Tuesday, January 12, 2010 10:42 AM
  • Thank you for your reply peter. Yes of course. I can send every codesnippet you need. But 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:

    Feature.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Feature  Id="53524872-6d26-49ea-9d57-399778e7a189"
              Title="MyWebPart"
              Description="Description for WebPart1"
              Version="12.0.0.0"
              Hidden="FALSE"
              Scope="Site"
              DefaultResourceFile="core"
              xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="elements.xml"/>
        <ElementFile Location="WebPartFeature1.webpart" />
      </ElementManifests>
    </Feature>
    



    Elements.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <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" />
        </File>
      </Module>
    </Elements>



    WebPartFeature1.webpart

    <?xml version="1.0" encoding="utf-8" ?>
    <webParts>
      <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
        <metaData>
          <type name="MyNamespace.WebPartFeature1, WSPBuilderProject1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=642caf6ec46008bf" />
          <importErrorMessage>Cannot import MyWebPart Web Part.</importErrorMessage>
        </metaData>
        <data>
          <properties>
            <property name="Title" type="string">MyWebPart</property>
            <property name="Description" type="string">Description for WebPart1</property>
          </properties>
        </data>
      </webPart>
    </webParts>
    



    WebPartFeature1.vb

    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
                Get
                    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
                    Try
    
                        MyBase.CreateChildControls()
    
                        ' Your code here...
                        Me.Controls.Add(New LiteralControl(Me.MyProperty))
                    Catch ex As Exception
                        HandleException(ex)
                    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
                    Try
                        MyBase.OnLoad(e)
    
                        ' Your code here...
                        Me.EnsureChildControls()
                    Catch ex As Exception
                        HandleException(ex)
                    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.Clear()
                Me.Controls.Add(New LiteralControl(ex.Message))
            End Sub
        End Class
    End Namespace
    



    Setup.exe.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <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="1.0.0.0"/>
        <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"/>    
      </appSettings>
    </configuration>

    Best regards,
    SharepointKB


    Best regards,
    Yavuz B.

    My Sharepoint Blog | http://sharepointkb10.wordpress.com
    Twitter | http://www.twitter.com/sharepointmag_t
    Tuesday, January 12, 2010 11:02 AM
  • 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.
    Best regards,
    Yavuz B.

    My Sharepoint Blog | http://sharepointkb10.wordpress.com
    Twitter | http://www.twitter.com/sharepointmag_t
    • Proposed as answer by Amod Goyal Thursday, May 17, 2012 3:42 PM
    Tuesday, January 12, 2010 1:05 PM
  • Hi,

    I assume you have issue with the webpart file. When you delete the web part from gallery and reinstall that using the New (as described in step 4 above), then the webpart file is generated automatically for you. I suggest you to export the created webpart file (click the table icon right to the web part title to get the property sheet and then click the export button in the top menu) and compare that with your one using a tool. Or instead of exporting you can choose to display the XML and do the comparision "manually".

    Hope that helps.

    Peter
    • Marked as answer by SharepointMag Tuesday, January 12, 2010 1:53 PM
    Tuesday, January 12, 2010 1:33 PM
  • yes thank you! when i compared both files, i found that my type name line in the webpart file was different to the one which was generated by sharepoint. so i copied it to my file and it worked fine. thank you.

    regards
    yavuz
    Best regards,
    Yavuz B.

    My Sharepoint Blog | http://sharepointkb10.wordpress.com
    Twitter | http://www.twitter.com/sharepointmag_t
    Tuesday, January 12, 2010 1:54 PM
  • Hi,

    Thank you for your feedback. It's always nice to know that I could help.

    Peter
    Tuesday, January 12, 2010 4:02 PM