【问题标题】:Cannot add custom web part to page using the JS OM无法使用 JS OM 将自定义 Web 部件添加到页面
【发布时间】:2010-07-14 02:58:16
【问题描述】:

我正在尝试使用 SharePoint 2010 提供的新 Javascipt OM 添加自定义 Web 部件(这是我自己编写的)。我的解决方案也在沙箱中运行。

我的内容编辑器 Web 部件可以正常工作(基于示例代码),但是当我将内容编辑器 Web 部件定义替换为我自己的 Web 部件定义时,我收到以下错误:

---------------------------
Message from webpage
---------------------------
Request failed. The operation could not be completed because the Web Part is not on this page.
undefined
---------------------------
OK   
---------------------------

我测试过的东西:

  1. 我可以成功地将我的 .webpart 定义上传到 Web 部件库,并且 Web 部件工作正常。
  2. 我可以成功地将 Web 部件添加到我正在尝试使用“上传 Web 部件”功能以编程方式添加它的页面。

这是我用来添加 Web 部件的代码:

function addWebPart(serverRelativeUrl) {
//var webPartXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
//webPartXml = webPartXml + "<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.microsoft.com/WebPart/v2\">  <Title>Content Editor</Title>  <FrameType>Default</FrameType>  <Description>Allows authors to enter rich text content.</Description>  <IsIncluded>true</IsIncluded>  <ZoneID>Main</ZoneID>  <PartOrder>0</PartOrder>  <FrameState>Normal</FrameState>  <Height />  <Width />  <AllowRemove>true</AllowRemove>  <AllowZoneChange>true</AllowZoneChange>  <AllowMinimize>true</AllowMinimize>  <AllowConnect>true</AllowConnect>  <AllowEdit>true</AllowEdit>  <AllowHide>true</AllowHide>  <IsVisible>true</IsVisible>  <DetailLink />  <HelpLink />  <HelpMode>Modeless</HelpMode>  <Dir>Default</Dir>  <PartImageSmall />  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>  <PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>  <IsIncludedFilter />  <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>  <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>  <ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />  <Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />  <PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>";

var webPartXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
       "<webParts><webPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\">" +
       "<metaData>" + 
       "<type name=\"Codezeven.Social.Views.WebParts.ItemViewTrackerWebPart, Codezeven.Social.Views, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c56ab109c2917056\" />" +
       "<importErrorMessage>Cannot import this Web Part.</importErrorMessage>" +
       "<Solution SolutionId=\"f5984dff-f7c0-4108-8c08-671bbb9b5df0\" xmlns=\"http://schemas.microsoft.com/sharepoint/\" />" +
       "</metaData>" +
       "<data><properties>" +
       "<property name=\"Title\" type=\"string\">ItemViewTrackerWebPart</property>" +
       "</properties></data>" + 
       "</webPart></webParts>";

clientContext = new SP.ClientContext.get_current();
var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);

var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);

var oWebPartDefinition = limitedWebPartManager.importWebPart(webPartXml); this.oWebPart = oWebPartDefinition.get_webPart();

limitedWebPartManager.addWebPart(oWebPart, 'Main', 0);

clientContext.load(oWebPart);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));}

function onQuerySucceeded() {

    alert('Web Part added: ' + oWebPart.get_title());
}
function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

【问题讨论】:

  • 你有没有让这个工作?我遇到了同样的错误消息。

标签: javascript sharepoint-2010


【解决方案1】:

不确定你是否解决了这个问题,但我认为你有一个不必要的

clientContext.load(oWebPart);

就在 executeQueryAsync 之前。

如果您使用的是 Firebug/Fiddler/IE9 工具栏,请注意网络流量,并且来自 ClientQuery 的 POST 结果有一些有用的错误消息。

【讨论】:

    猜你喜欢
    • 2011-07-21
    • 2021-10-08
    • 2011-06-13
    • 2010-12-25
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多