【问题标题】:SharePoint error: "Cannot import Web Part"SharePoint 错误:“无法导入 Web 部件”
【发布时间】:2010-09-12 08:45:41
【问题描述】:

我有一个自己开发的 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 部件库中,所有正确的属性都表明 Web 部件文件已成功读取.

一切似乎都已到位,但我从 SharePoint 得到了该错误,并且几乎没有解释如何解决它。

感谢您寻求解决方案的任何帮助。

【问题讨论】:

    标签: c# sharepoint moss web-parts project-server


    【解决方案1】:

    想通了。

    错误消息来自 .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 文件是在安装了 Visual Studio Extensions for WSS 的 32 位系统上创建的。

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

    用完整的类型名称替换 GUID 有效。

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

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

    格式如下: 类、命名空间、版本、文化、公钥

    您可以从与您的 SharePoint 实例关联的 web.config 文件中轻松获取它,因为它将位于安全控件列表中。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这个答案是救命稻草!我给了 System.Net.WebPermission 的 Web 部件遇到了同样的问题。我已将 [System.Net.WebPermission(System.Security.Permissions.SecurityAction.Demand, Unrestricted = true)] 属性添加到整个类,而不是需要权限的特定方法。
      【解决方案3】:

      所有很棒的建议。我的问题既独特又愚蠢:我已将解决方案部署到第一个 Web 应用程序,但没有部署到第二个。然而,SharePoint 仍然允许我在第二个 Web 应用程序的网站集上激活该功能(不知道为什么)。这意味着第二个 Web 应用在这个 Web.config 文件中没有安全控制条目(而我正在愚蠢地检查第一个 Web.config)。

      因此,请仔细检查您是否正在查看正确的 Web 应用程序/web.config。

      【讨论】:

        【解决方案4】:

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

        当我尝试向页面添加新的 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]; 
                .....
         }
        

        现在停止,我认为它可能是网页初始化顺序问题。 AS网页加载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];
                  ....
                 }
        

        经过我的测试,错误消息没有再次出现.. 哈哈~~

        希望这个解释对你有所帮助。

        【讨论】:

          【解决方案5】:

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

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

          确保您有一个有效的 SafeControls 条目。

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

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

          这对我有用。祝你好运。

          【讨论】:

            【解决方案6】:

            您是否回收了您的工作进程或重置了 IIS?

            【讨论】:

            • 我试试看。我没有。
            【解决方案7】:

            解决了我的问题。

            我收到了这个错误:

            ===========================================================================
            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 是否相同。

            我正在从其他 webpart 来源复制粘贴代码。 (Codeplex 上的多个文档上传 Wepart)并忘记修复 guid。

            【讨论】:

              【解决方案8】:

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

              【讨论】:

                【解决方案9】:

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

                public class DerivedWebPart : BaseWebPart
                

                我还在派生类中添加了一个构造函数来调用基类之一 - 尽管我认为你真的不需要这个:

                    public DerivedWebPart() : base()
                    {
                    }
                

                【讨论】:

                  【解决方案10】:

                  我遇到了与此非常相似的问题,但 Guid 不是问题:我的 webpart 没有将 CLSCompliannt 属性设置为 false。像这样:

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

                  【讨论】:

                    【解决方案11】:

                    我发现我的第一次没有导入,但是如果我点击“新建”并添加它,它会起作用。

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

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

                    【讨论】:

                      猜你喜欢
                      • 2011-08-22
                      • 2011-05-04
                      • 2011-09-24
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-09-12
                      • 1970-01-01
                      • 2011-01-01
                      • 2011-06-24
                      相关资源
                      最近更新 更多