我正在尝试创建自定义功能区,并在我的项目中创建了一个 JavaScript 文件。

enter image description here

我发现我可以使用的问题 <CustomAction> 标签来导入 JavaScript 文件。

<CustomAction Id="Ribbon.Library.Actions.Scripts"
 Location ="ScriptLink" ScriptSrc="/_layouts/MyJSFile.js" />

现在我的 Elements.xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction Id="Ribbon.Library.Actions.Scripts" Location="ScriptLink"
                ScriptSrc="/_layouts/CustomRibbonButton/JavaScript1.js" />

    <CustomAction
    Id="Ribbon.Library.Actions.AddAButton"
    Location="CommandUI.Ribbon"
    RegistrationId="101" RegistrationType="List"
    Title="Add a Ribbon Button">
        <CommandUIExtension>
            <CommandUIDefinitions>
                <CommandUIDefinition
          Location="Ribbon.Library.Share.Controls._children">
                    <Button Id="Ribbon.Library.Share.NewRibbonButton"
            Command="NewRibbonButtonCommand"
                    Image16by16="_layouts/15/images/placeholder16x16.png"
                    Image32by32="_layouts/15/images/placeholder32x32.png"
                                    LabelText="Hello World"
            TemplateAlias="o2" />
                </CommandUIDefinition>
            </CommandUIDefinitions>
            <CommandUIHandlers>
                <CommandUIHandler
          Command="NewRibbonButtonCommand" 
          CommandAction="javascript:getFiles()" />
            </CommandUIHandlers>
        </CommandUIExtension>
    </CustomAction>
</Elements>

当我在 IE 开发者工具的“网络”选项卡中加载我的页面时,我可以看到 404 错误,表明无法找到路径 /_layouts/customribbonbutton/javascript1.js. 。我更新了我的 <CustomAction> 标记为此:

<CustomAction Id="Ribbon.Library.Actions.Scripts" Location="ScriptLink"
                ScriptSrc="/_layouts/JavaScript1.js" />

但我仍然遇到相同的 404 错误,尽管路径不同。

我在这里做错了什么?

有帮助吗?

解决方案

您需要将 15 附加到 _layouts 即 /_layouts/15 以便在您处理 SP 2013 时获取该文件。

更新

将 javascript 文件放在项目中的 CustomRibbonButton 文件夹下并使用 /_layouts/15/CustomRibbonButton/JavaScript1.js. 。SharePoint 2013 的配置单元文件夹为 15。MOSS 过去为 12,SP 2010 为 14。这需要明确使用,因为 SP 2013 还支持基于 2010 的解决方案,如果您在代码中未提及 15,那么它将在 14 hive 中查找文件。

其他提示

在之间添加以下内容 <CommandUIExtension> 打开和关闭标签

<CommandUIHandler
                    Command="NewRibbonButtonCommand"
                    CommandAction="javascript:Convertion();"
                    EnabledScript="javascript:enable();"/>
许可以下: CC-BY-SA归因
scroll top