我如何确保我的 WebPart 被发现并注册为安全的?

How can I see to it that my WebPart is found and registered as safe?

在调试我的 WebPart 以查看其控件未显示的原因时,我将页面置于浏览器中的编辑模式以首先删除 WebPart 的所有多余实例,以便我逐步执行 CreateChildControls() 等。方法只有一次。

虽然这样做(将页面置于编辑模式),但我看到每个 WebPart 都包含以下错误消息:

Web 部件错误:无法显示或导入此页面上的 Web 部件或 Web 表单控件。找不到类型 WebFormPDFGen.WebFormPDFGenWebPart.WebFormPDFGenWebPart、WebFormPDFGen、Version=1.0.0.0、Culture=neutral、PublicKeyToken=42314blabla 或未注册为安全类型。

什么? WebFormPDFGen.WebFormPDFGenWebPart.WebFormPDFGenWebPart 没有找到?解决方案名为 WebFormPDFGen,WebPart 名为 WebFormPDFGenWebPart,.cs 和 .webpart 文件也是如此:

WebFormPDFGenWebPart.webpart 确实包含对无法找到的实体 (WebFormPDFGen.WebFormPDFGenWebPart.WebFormPDFGenWebPart) 的引用:

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="WebFormPDFGen.WebFormPDFGenWebPart.WebFormPDFGenWebPart, $SharePoint.Project.AssemblyFullName$" />
      <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">WebFormPDFGenWebPart</property>
        <property name="Description" type="string">My WebPart</property>
      </properties>
    </data>
  </webPart>
</webParts>

所以...是不是 Web 部件没有注册为安全的,正如错误消息的第二部分提到的那样?我需要做什么才能将其注册为安全的?

更新

基于演练 here:

,我使用非常基本的 WebPart 在运行时遇到了同样的问题

当我将 WebPart 添加到我的测试页面时,它呈现良好 - 它具有基于该演练中的代码的预期 "Hello, world!" 消息,它只是:

[ToolboxItemAttribute(false)]
public class XMLToPDFWebPart : WebPart
{
    private string customMessage = "Hello, world!";

    [WebBrowsable(true),
     WebDescription("Displays a custom message"),
     WebDisplayName("Display Message"),
     Personalizable(PersonalizationScope.User)]
    public string DisplayMessage
    {
        get { return customMessage; }
        set { customMessage = value; }
    }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        LiteralControl message = new LiteralControl();
        message.Text = DisplayMessage;
        Controls.Add(message);
    }

...但是当我随后在 运行 另一个 project/WebPart 时查看该页面时,它不仅不再欢快地鸣叫,"Hello, world!" 而是在我的后面吹响了雾号脸:

Web 部件错误:无法显示或导入此页面上的 Web 部件或 Web 表单控件。找不到类型 XMLToPDF.XMLToPDFWebPart.XMLToPDFWebPart、XMLToPDF、Version=1.0.0.0、Culture=neutral、PublicKeyToken=42f314BleeBlahBloo 或未注册为安全类型。

为什么它现在确定 WebPart 丢失或不安全,而以前显示它?

更新 2

在 WebPart 库中选择并单击 2 次简单 WebPart 时,我收到一条带有相关编号的错误消息。这些是日志文件中包含相关编号的行:

03/24/2015 13:33:21.37  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           Logging Correlation Data        xmnv    Medium      Name=Request (GET:https://duckbilled.platypus.poisontoe:443/_layouts/WPPrevw.aspx?ID=208)   05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.39  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           Logging Correlation Data        xmnv    Medium      Site=/  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.43  w3wp.exe (0x1980)                           0x0320  Document Management Server      Document Management             cm6w    Medium      Template Discovery/InitScriptData: cannot get user profile, no UserProfileApplicationProxy exists in the current context    05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.43  w3wp.exe (0x1980)                           0x0320  Document Management Server      Document Management             cm6w    Medium      Template Discovery/InitScriptData: cannot get user profile, no UserProfileApplicationProxy exists in the current context    05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.45  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           Monitoring                      b4ly    High        Leaving Monitored Scope (EnsureListItemsData). Execution Time=11.0956839486583  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.46  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           General                         avro    High        Relying on fallback logic in VghostPageManager::getGhostDocument() for document: 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\Template\Features\XMLToPDF_Feature1\XMLToPDFWebPart\XMLToPDFWebPart.webpart'  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.46  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           General                         avro    High        Relying on fallback logic in VghostPageManager::getGhostDocument() for document: 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\Template\Features\XMLToPDF_Feature1\XMLToPDFWebPart\XMLToPDFWebPart.webpart'  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.46  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           General                         avro    High        Relying on fallback logic in VghostPageManager::getGhostDocument() for document: 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\Template\Features\XMLToPDF_Feature1\XMLToPDFWebPart\XMLToPDFWebPart.webpart'  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.46  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           General                         avro    High        Relying on fallback logic in VghostPageManager::getGhostDocument() for document: 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\Template\Features\XMLToPDF_Feature1\XMLToPDFWebPart\XMLToPDFWebPart.webpart'  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.46  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           General                         avro    High        Relying on fallback logic in VghostPageManager::getGhostDocument() for document: 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\Template\Features\XMLToPDF_Feature1\XMLToPDFWebPart\XMLToPDFWebPart.webpart'  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.46  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           General                         avro    High        Relying on fallback logic in VghostPageManager::getGhostDocument() for document: 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\Template\Features\XMLToPDF_Feature1\XMLToPDFWebPart\XMLToPDFWebPart.webpart'  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.46  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           General                         8kh7    High        Cannot complete this action.  Please try again. 05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.46  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           General                         8e2s    Medium      Unknown SPRequest error occurred. More information: 0x80070002  05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.47  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           Runtime                         tkau    Unexpected  Microsoft.SharePoint.SPException: An error occurred when previewing the Web Part.    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.Control.PreRenderRecursiveInternal()     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)   05556c29-2d56-4093-9a6a-15ed2346f3db
03/24/2015 13:33:21.57  w3wp.exe (0x1980)                           0x0320  SharePoint Foundation           Monitoring                      b4ly    Medium      Leaving Monitored Scope (Request (GET:https://duckbilled.platypus.poisontoe:443/_layouts/WPPrevw.aspx?ID=208)). Execution Time=208.394083605598 05556c29-2d56-4093-9a6a-15ed2346f3db

请注意,如果我现在尝试将该 WebPart 添加到页面,它会告诉我,"The operation could not be completed because the item was removed from the gallery"

更新 3

清理、重建和[重新]部署似乎至少暂时修复了新的、超级简单的 WebPart:

...但我对它不会成为不受欢迎的 WebPart 的信心程度并没有完全接近珠穆朗玛峰的高度 - 也许是蒂姆山。

不过如您所见,原始 WebPart 仍然出现错误。

我建议下载 visual studio 的加载项。它被称为 CKSDev 工具

这允许您将 SharePoint 解决方案打包到 WSP 中,然后部署该包,这应该会保护您的安全控制。

CKSDEV tools

这将帮助您进行调试,并且在 SharePoint 开发方面通常很棒。他们甚至为站点模板等内容提供了一些模板,这将为您节省大量时间。

您可能需要创建一个新的 SharePoint 项目并迁移您的代码,但是一旦您让 CKSDev 开始工作,您就应该很顺利了。

另一种选择是找到您的 Web 应用程序的 web.config,并为您的解决方案抱怨的程序集添加一个安全控件,但我不会这样做,因为您在移动时会遇到同样的问题将其投入生产,当您开始使用这些文件时,很容易犯错误,这会给您带来更多痛苦。

干杯

特鲁兹