SharePoint错误:"无法导入Web部件"

时间:2020-03-06 14:35:53  来源:igfitidea点击:

我有一个已开发的Web部件,如果手动安装该Web部件就可以了。

但是,当我按照本网站上的说明打包Web部件作为指南时:
http://www.theartofsharepoint.com/2007/05/how-to-build-solution-pack-wsp.html

我在日志文件中收到此错误:

09/23/2008 14:13:03.67  w3wp.exe (0x1B5C)                           0x1534  Windows SharePoint Services     Web Parts                       8l4d    Monitorable Error importing WebPart. Cannot import Project Filter.   
09/23/2008 14:13:03.67  w3wp.exe (0x1B5C)                           0x1534  Windows SharePoint Services     Web Parts                       89ku    High        Failed to add webpart http%253A%252F%252Fuk64p12%252FPWA%252F%255Fcatalogs%252Fwp%252FProjectFilter%252Ewebpart;Project%2520Filter.  Exception Microsoft.SharePoint.WebPartPages.WebPartPageUserException: Cannot import Project Filter.     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.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, SPWeb spWeb)     at Microsoft.SharePoint.WebPartPages.WebPartQuickAdd.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(...  
09/23/2008 14:13:03.67* w3wp.exe (0x1B5C)                           0x1534  Windows SharePoint Services     Web Parts                       89ku    High        ...String eventArgument)

相关的位是:

http%253A%252F%252Fuk64p12%252FPWA%252F%255Fcatalogs%252Fwp%252FProjectFilter%252Ewebpart;Project%2520Filter.
Exception Microsoft.SharePoint.WebPartPages.WebPartPageUserException: Cannot import Project Filter.
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.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, SPWeb spWeb)
at Microsoft.SharePoint.WebPartPages.WebPartQuickAdd.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

并且伴随着一个相当简洁的错误消息:"无法导入Web部件"。

我已经检查过,并将我的.dll注册为安全的,它已经在GAC中,该功能已激活,并且Web部件出现在Web部件库中并具有所有正确的属性,表明已成功读取WebPart文件。

一切似乎都准备就绪,但是我从SharePoint中获得了该错误和很少的解释,以了解如何解决该错误。

寻求解决方案的任何帮助将不胜感激。

解决方案

我们是否回收了工作进程或者重置IIS?

弄清楚了。

错误消息是来自.webpart文件的错误消息:

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <!--
      The following Guid is used as a reference to the web part class, 
      and it will be automatically replaced with actual type name at deployment time.
      -->
      <type name="7F8C4D34-6311-4f22-87B4-A221FA8735BA" />
      <importErrorMessage>Cannot import Project Filter.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">Project Filter</property>
        <property name="Description" type="string">Provides a list of Projects that can be used to Filter other Web Parts.</property>
      </properties>
    </data>
  </webPart>
</webParts>

问题是原始的.webpart文件是在安装了WSS的Visual Studio扩展的32位系统上创建的。

但是,由于我现在使用的是64位计算机,因此VSEWSS不可用,并且我认为由于不使用这些部署工具,因此导致上述GUID未被替换。

用全类型名称替换GUID是可行的。

因此,如果从importErrorMessage节点遇到错误消息,请检查.webpart文件中的类型节点是否看起来像这样(不相关的示例):

<type name="TitleWP.TitleWP, TitleWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" />

格式如下:
类,名称空间,版本,文化,公钥

我们可以轻松地从与SharePoint实例关联的web.config文件中获取该文件,因为它将在安全控件列表中。

现在,我得到了类似问题的答案,如下所示:

当我尝试向页面添加新的wep部件时,sharepoint向我显示一条错误消息,告诉我-无法导入我的Web部件,此错误消息在.webpart文件中定义。

所以我试图在页面中添加一些其他Web部件,一个奇怪的问题出现,其中一些可以添加,其中一些不能添加。

跟踪Web部件的代码并对其进行分析后,我发现了原因:

Web部件ProjectInfo(我的Web部件名称)的旧代码是:

namespace ProjectInfo
....
 public class ProjectInfo:System.Web.UI.WebControls.WebParts.Web.part
 {
        .....
        private SPWeb _spWeb;
        private SPList _spList;
        private string _listName = "ProjectDocs";
        ......
 }
public ProjectInfo()
 {
        .....
            _spWeb = SPContext.Current.Web;
            //It show me a error here when i trace the code
            _spList = _spWeb.Lists[_listName]; 
        .....
 }

现在停止,我认为这可能是网页初始化顺序问题。作为Web网页加载Web部件控件,构造函数ProjectInfo()将首先运行。实际上,该网页尚未完成其初始化。到...的时候。

所以我做了一个测试。首先,我在网页上放了一个好的网页,没关系。然后,我尝试将Web部件放置在当前无法添加的页面中。 ~~好!它正在工作...因为该页面已经初始化。完成的。

好的!我更正了我的代码:

namespace ProjectInfo
    ....
     public class ProjectInfo:System.Web.UI.WebControls.WebParts.Web.part
     {
            .....
            private SPWeb _spWeb;
            private SPList _spList;
            private string _listName = "ProjectDocs";
            ......
     }
    public ProjectInfo()
     {
            .....
                //Remove code in constructure function.
                //_spWeb = SPContext.Current.Web;
                //It show me a error here when i trace the code
                //_spList = _spWeb.Lists[_listName]; 
            .....
     }

   protected override void CreateChildControls()
        {
         ....
           base.CreateChildControls();

            _spWeb = SPContext.Current.Web;
            _spList = _spWeb.Lists[_listName];
          ....
         }

经过我的测试,错误消息没有再次出现。
大声笑~~

希望这个解释能对我们有所帮助。

解决了我的问题。

我收到此错误:

===========================================================================
Error importing WebPart. Cannot import ........ Web Part.
Failed to add webpart 
Exception Microsoft.SharePoint.WebPartPages.WebPartPageUserException: Cannot import ... 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.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, SPWeb spWeb)     at Microsoft.SharePoint.WebPartPages.WebPartQuickAdd.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)   
===========================================================================

"无法导入Webpart ..."

问题是:不匹配的GUIDS

检查webpart类以及.xml和.webpart类中的guid是否相同。

我正在从其他Webparts来源复制粘贴代码。 (在Codeplex上有多个文档上载Wepart),却忘记修复向导。

当我的Web部件引用的GAC中的程序集由与我期望的不同的强名称密钥文件签名时,我也遇到了此错误。
我决定更新这些DLL时发现了这一点。将其插入GAC时,我注意到同一DLL有2个条目,但它们具有不同的公钥令牌

创建基类Web部件,然后从中继承派生类时,出现此错误。基类很好,但是当我尝试将其添加到网页时,派生类失败,并且错误与原始帖子相同。就我而言,我必须在该类中添加一个公共修饰符:

public class DerivedWebPart : BaseWebPart

另外,我在派生类中添加了一个构造函数来调用基类,尽管我认为我们实际上不需要这样:

public DerivedWebPart() : base()
    {
    }

我们遇到了同样的问题,发现Web部件的构造函数被WebPartImporter调用,并且在该构造函数中,我们正在执行SPSecurity.RunWithElevatedPrivileges。

由于某些原因,WebPartImporter无法处理此问题。因此,我们只是将代码从构造函数中移到了OnInit(它真正所属的地方),一切都很好。

我已经多次看到这种异常情况,而没有得到很好的解决方案。

检查程序集,命名空间,类名称在任何地方都是正确的。这使我不止一次挂断电话。

确保我们具有有效的SafeControls条目。

确保.webpart文件有效(如果可以,请让SharePoint为我们创建文件)

如果我们绝对肯定一切正确,那么我们就陷在了我去过几次的地方。我唯一能想到的是VS错误地编译了程序集。我发现的唯一解决方法是创建一个新的VS项目并按照需要进行设置,然后将旧CS文件的文本复制到新CS文件中...不要自己复制文件...目标是要让一切都新鲜。

这对我有用。祝你好运。

我发现我的不是第一次导入,但是如果单击"新建"并将其添加,它将可以正常工作。

从那里,我获取了XML的副本并将其保存到我的项目中。之后,Web部件运行良好。

我只花了几天的时间就到了这一点。浪费了很多时间。

我有一个与此非常相似的问题,但Guid并不是问题:我的Webpart的CLSCompliannt属性未设置为false。像这样:

namespace MyNamespace
{

    [CLSCompliant(false)]
    [Guid("...")]
    public class MyWidget : MyWebPartBaseClass
    {

    }
}