C# SharePoint 错误:“无法导入 Web 部件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/120928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SharePoint error: "Cannot import Web Part"
提问by
I have a web part that I've developed, and if I manually install the web part it is fine.
我有一个我开发的 Web 部件,如果我手动安装该 Web 部件就可以了。
However when I have packaged the web part following the instructions on this web site as a guide: http://www.theartofsharepoint.com/2007/05/how-to-build-solution-pack-wsp.html
但是,当我按照本网站上的说明打包 Web 部件作为指南时:http: //www.theartofsharepoint.com/2007/05/how-to-build-solution-pack-wsp.html
I get this error in the log files:
我在日志文件中收到此错误:
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)
The pertinent bit is:
相关位是:
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)
And that's accompanied by a rather terse error message: "Cannot import web part".
并且伴随着相当简洁的错误消息:“无法导入 Web 部件”。
I have checked and my .dll is registered as safe, it is in the GAC, the feature is activated, and the web parts appear in the web part library with all of the correct properties showing that the webpart files were read successfully.
我已检查并且我的 .dll 已注册为安全的,它在 GAC 中,该功能已激活,并且 Web 部件出现在 Web 部件库中,所有正确的属性表明 Web 部件文件已成功读取。
Everything appears to be in place, yet I get that error and little explanation from SharePoint of how to resolve it.
一切似乎都已就位,但我收到了那个错误,并且从 SharePoint 那里得到了关于如何解决它的一点解释。
Any help finding a solution is appreciated.
任何帮助找到解决方案表示赞赏。
采纳答案by Keith Sirmons
Figured it out.
弄清楚了。
The error message is the one from the .webpart file:
错误消息来自 .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>
The problem is that the original .webpart file was created on a 32-bit system with Visual Studio Extensions for WSS installed.
问题是原始 .webpart 文件是在安装了 Visual Studio Extensions for WSS 的 32 位系统上创建的。
However as I'm now on a 64-bit machine VSEWSS is unavailable, and I believe that results in the above GUID not being substituted as I am not using those deployment tools.
但是,由于我现在在 64 位计算机上,因此 VSEWSS 不可用,我相信这会导致上述 GUID 未被替换,因为我没有使用这些部署工具。
Replacing the GUID with the full type name works.
用完整的类型名称替换 GUID 有效。
So if you encounter the error message from your importErrorMessage node, then check that your type node in the .webpart file looks more like this (unrelated example):
因此,如果您遇到来自 importErrorMessage 节点的错误消息,请检查 .webpart 文件中的类型节点是否更像这样(不相关的示例):
<type name="TitleWP.TitleWP, TitleWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" />
This is in the format: Class, Namespace, Version, Culture, PublicKey
其格式为:Class、Namespace、Version、Culture、PublicKey
You can grab that easily from the web.config file associated with your SharePoint instance, as it will be in the safe controls list.
您可以从与 SharePoint 实例关联的 web.config 文件中轻松获取该文件,因为它将位于安全控件列表中。
回答by Keith Sirmons
Have you recycled your worker process or reset IIS?
您是否回收了您的工作进程或重置了 IIS?
回答by Smxyhd
Now I get a answer for similar problem as below:
现在我得到了类似问题的答案,如下所示:
When I try to added a new wep part to the page, then sharepoint show me a error message, tell me--Can not import my web part, this error message define in .webpart file.
当我尝试向页面添加新的 wep 部件时,sharepoint 向我显示一条错误消息,告诉我--无法导入我的 web 部件,此错误消息在 .webpart 文件中定义。
So i tried to add some ohter web parts in the page , A strange quesiton appearance, some of them can be added , some of them can not be added.
所以我尝试在页面中添加一些其他的web部件,一个奇怪的问题出现,有些可以添加,有些无法添加。
After I traced the code of my web part and anaylsis them, I found the reason:
在我跟踪我的 web part 的代码并分析它们之后,我找到了原因:
Old Code for web part ProjectInfo(my web part name) is:
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];
.....
}
Stop now, I thought that it maybe the web page init order problem. AS web page load web part control, constructrue function ProjectInfo() will be running at first. Actually, the web page havn't finish its init. by the time.
打住,我还以为是网页init命令的问题。AS 网页加载 Web 部件控件,首先会运行构造函数 ProjectInfo()。实际上,网页还没有完成它的初始化。到...的时候。
so i did a test. firstly, I put a good web in the page, it's ok . then, I try to put the web part in the page which can not be added just now. ~~ OK!! It's working ...because the page already init. finished.
所以我做了一个测试。首先,我在页面中放了一个很好的网页,没关系。然后,我尝试将 web part 放在刚刚无法添加的页面中。~~好的!!它正在工作......因为页面已经初始化。完成的。
Ok! I corrected my code:
好的!我更正了我的代码:
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];
....
}
After I test, the error message did't happed again.. LoL ~~
经过我测试,错误信息没有再出现.. LoL~~
Hope this explain will help you .
希望这个解释对你有帮助。
回答by Smxyhd
Solved mine.
解决了我的。
i was getting this error:
我收到此错误:
===========================================================================
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)
===========================================================================
"Cannot import webpart..."
“无法导入 webpart...”
The problem was: Non-matching GUIDS
问题是:不匹配的 GUIDS
Check if the guid on webpart class , and in the .xml and in .webpart class are same.
检查 webpart 类和 .xml 和 .webpart 类中的 guid 是否相同。
I was copy-pasting code from other webparts sources. ( Mutliple Document Upload Wepart on Codeplex) and forgot to fix guids.
我是从其他 webparts 来源复制粘贴代码。(Codeplex 上的多个文件上传 Wepart)忘记修复 guid。
回答by Smxyhd
I have also experienced this error when the assemblies in the GAC that my web part referenced, was signed by a different strong name key file to what I was expecting. I found this out when deciding to update these DLLs. When inserting it into the GAC I noticed that there were 2 entries for the same DLL but they had different Public Key Tokens
当我的 Web 部件引用的 GAC 中的程序集由与我期望的不同的强名称密钥文件签名时,我也遇到了此错误。我在决定更新这些 DLL 时发现了这一点。将其插入 GAC 时,我注意到同一个 DLL 有 2 个条目,但它们具有不同的公钥令牌
回答by Smxyhd
I got this error when I created a base class web part and then inherited a derived class from it. The base class was fine but the derived class failed when I tried to add it to a web page with the same error as the original post. In my case I had to add a publicmodifier to the class:
我在创建基类 Web 部件然后从它继承派生类时收到此错误。基类很好,但是当我尝试将它添加到与原始帖子相同错误的网页时,派生类失败了。就我而言,我必须向类添加一个公共修饰符:
public class DerivedWebPart : BaseWebPart
Also I added a constructor in the derived class to call the base class one - although I think you shouldn't need this really:
我还在派生类中添加了一个构造函数来调用基类 - 虽然我认为你真的不需要这个:
public DerivedWebPart() : base()
{
}
回答by Kirk Liemohn
We had this same problem and found that the constructor of our web part was being called by the WebPartImporter and within the constructor we were doing SPSecurity.RunWithElevatedPrivileges.
我们遇到了同样的问题,发现 WebPartImporter 正在调用 Web 部件的构造函数,并且在构造函数中我们正在执行 SPSecurity.RunWithElevatedPrivileges。
For some reason the WebPartImporter cannot handle this. So, we simply moved our code out of the constructor to OnInit (where it really belonged) and all is well.
出于某种原因,WebPartImporter 无法处理此问题。所以,我们只是将我们的代码从构造函数移到 OnInit(它真正属于的地方),一切都很好。
回答by Kirk Liemohn
I have seen this anomaly several times without a good resolution.
我已经多次看到这种异常情况,但没有很好的解决方案。
Check that your Assembly, Namespace, Class name is correct EVERYWHERE. This has hung me up more than once.
检查您的程序集、命名空间、类名称是否在任何地方都是正确的。这不止一次让我挂断了电话。
Make sure you have a valid SafeControls entry.
确保您有一个有效的 SafeControls 条目。
Make sure your .webpart file is valid (let SharePoint create it for you if you can)
确保您的 .webpart 文件有效(如果可以,让 SharePoint 为您创建)
If you are absolutely positive that everything is correct, well then you are stuck in a place that I have been several times. The only thing that I can come up with is that VS is compiling the assembly wrong. The ONLY fix that I have found is to create a new VS project and set it up how you need, then copy THE TEXT of your old CS files into your new CS files...do not copy the files themselves...the goal is to have everything fresh.
如果你绝对肯定一切都是正确的,那么你就会被困在我去过几次的地方。我唯一能想到的是 VS 编译的程序集是错误的。我发现的唯一解决方法是创建一个新的 VS 项目并根据需要进行设置,然后将旧 CS 文件的文本复制到新 CS 文件中...不要复制文件本身...目标就是让一切都新鲜。
This has worked for me. Good Luck.
这对我有用。祝你好运。
回答by Shannon Bray
I found that mine did not import the first time, but if I clicked 'New' and added it, it would work.
我发现我的第一次没有导入,但是如果我单击“新建”并添加它,它会起作用。
From there I grabbed a copy of the XML and saved it to my project. The web part worked great after that.
从那里我抓取了一份 XML 并将其保存到我的项目中。之后,Web 部件运行良好。
It only took me DAYS to get to this point. A lot of wasted time.
我只花了几天的时间就达到了这一点。浪费了很多时间。
回答by Ben Collins
I had a problem very similar to this, but Guids weren't the problem: my webpart didn't have the CLSCompliannt
attribute set to false. Like so:
我有一个与此非常相似的问题,但 Guids 不是问题:我的 webpart 没有将CLSCompliannt
属性设置为 false。像这样:
namespace MyNamespace
{
[CLSCompliant(false)]
[Guid("...")]
public class MyWidget : MyWebPartBaseClass
{
}
}