問題描述:sharepoint2019有modern模式和經典模式兩種,16即以前版本只有經典模式,經典模式支持js腳本,modern下面寫了js腳本調用但是起不到作用
實現功能:在document library(RegistrationId='101')下面添加一個按鈕CustomButton1,按鈕實現alert("hello,Oliver!')功能
1.在原有16的solution()下面element.xml的基礎上修改button的響應方法,將原來commandAction中"javascript:test();"改爲"="~site/_layouts/15/2019/BeforeCustomAction.aspx"
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Control Id="PromotedActions"
Sequence="91"
ControlSrc="/_ControlTemplates/15/2019/BeforeCustomAction.ascx">
</Control>
<CustomAction
Id="Ribbon.ListItem.Actions.Scripts"
Location="ScriptLink"
ScriptSrc ="/_layouts/15/2019/JS/CustomAction.js"/>
<CustomAction Id="Ribbon.CustomGroup" RegistrationId="101" RegistrationType="List"
Title="CustomButton1" Location="CommandUI.Ribbon">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Library.Settings.Controls._children">
<Button Id="Ribbon.Library.New.CustomButton1"
Command="AddFavourite"
Image32by32="/_layouts/15/images/CustomButton1_32x32.png"
LabelText="CustomButton1"
Sequence="11"
TemplateAlias="o1"
ToolTipTitle="CustomButton1"
ToolTipDescription="CustomButton1"/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="AddFavourite"
CommandAction="~site/_layouts/15/2019/BeforeCustomAction.aspx"/>
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>
2.新創建一個BeforeCustomAction.aspx文件,在PageHead下面添加需要的腳本引用和方法調用,這樣就會在點擊OliverCustomButton1的時候調用js中test()方法
<asp:content id="PageHead" contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="JS/CustomAction.js"></script>
<script type="text/javascript">
test();
</script>
</asp:content>
3.新建一個js腳本CustomAction.js,裏面加上函數方法
function test(){
alert("Hello,Oliver!");
}
4.publish該project,然後在相應網站部署上solution,並且在CA上action feature,打開相應網站就可以看到新添加的button,並且點擊響應