在使用Sharepoint2010的Ribboon编程的领域内,我们必然要遇到如何编写Ribbon内控件的Javascript代码。
下面是Ribbon定义的一段常见代码:
<
CommandUIHandlers
>
< CommandUIHandler Command =”Ribbon.MyTab.MyGroup.Button_CMD” CommandAction =”javascript:alert('RibbonJs Invoke!');' />
</ CommandUIHandlers >
< CommandUIHandler Command =”Ribbon.MyTab.MyGroup.Button_CMD” CommandAction =”javascript:alert('RibbonJs Invoke!');' />
</ CommandUIHandlers >
这段代码中的CommandAction中定义了此Ribbon控件将要执行的Javascript功能。问题是,在实际应用中,我们的Javascript远不只一个alert函数这么简单,而我们又不可能在这个Ribbon的定义中嵌入非常复杂的大量Javascript代码,我们需要做的应该像下面的代码定义段一样:
<
CommandUIHandlers
>
< CommandUIHandler Command =”Ribbon.MyTab.MyGroup.Button_CMD” CommandAction =”javascript:RibbonButtonHandler();” />
</ CommandUIHandlers >
< CommandUIHandler Command =”Ribbon.MyTab.MyGroup.Button_CMD” CommandAction =”javascript:RibbonButtonHandler();” />
</ CommandUIHandlers >
在CommandAction中潜入一个Javascript的Function,这个Funcation是在一个外部Javascript文件中定义的。所以接下来的问题是,如何让Ribbon的定义段知道它该使用哪一个外部Javascript文件引用?
好在Sharepoint中提供了这样一个途径,我们只需要加入下面的代码就能解决这个问题。
<
CustomAction
Id
=”Ribbon.Library.Actions.Scripts”
Location ="ScriptLink" ScriptSrc ="/_layouts/SharePointProject1/RibbonActions.js" />
Location ="ScriptLink" ScriptSrc ="/_layouts/SharePointProject1/RibbonActions.js" />
此代码的ScriptSrc指明了外部需要引用的Javascript文件位置以及文件名,当然我们需要在/_layouts/SharepointProjects1/目录下创建这个名为RibbonActions.js的javascript代码文件。
所以相对完整的代码应该如下 :
<
CommandUIHandlers
>
< CommandUIHandler Command =”Ribbon.MyTab.MyGroup.Button_CMD” CommandAction =”javascript:RibbonButtonHandler();” />
</ CommandUIHandlers >
< Elements xmlns =”http://schemas.microsoft.com/sharepoint/” >
< CustomAction
Id =”Ribbon.MyTab”
Title =”Ribbon Sample”
RegistrationType =”List”
RegistrationId =”100″
Location =”CommandUI.Ribbon.ListView” >
< CommandUIExtension >
< CommandUIDefinitions >… </ CommandUIDefinitions >
< CommandUIHandlers >
< CommandUIHandler Command =”Ribbon.MyTab.MyGroup.Button_CMD” CommandAction =”javascript:RibbonButtonHandler();” />
</ CommandUIHandlers >
</ CommandUIExtension >
</ CustomAction >
< CustomAction Id =”Ribbon.Library.Actions.Scripts”
Location =”ScriptLink”
ScriptSrc =”/_layouts/SharePointProject1/RibbonActions.js” />
</ Elements >
< CommandUIHandler Command =”Ribbon.MyTab.MyGroup.Button_CMD” CommandAction =”javascript:RibbonButtonHandler();” />
</ CommandUIHandlers >
< Elements xmlns =”http://schemas.microsoft.com/sharepoint/” >
< CustomAction
Id =”Ribbon.MyTab”
Title =”Ribbon Sample”
RegistrationType =”List”
RegistrationId =”100″
Location =”CommandUI.Ribbon.ListView” >
< CommandUIExtension >
< CommandUIDefinitions >… </ CommandUIDefinitions >
< CommandUIHandlers >
< CommandUIHandler Command =”Ribbon.MyTab.MyGroup.Button_CMD” CommandAction =”javascript:RibbonButtonHandler();” />
</ CommandUIHandlers >
</ CommandUIExtension >
</ CustomAction >
< CustomAction Id =”Ribbon.Library.Actions.Scripts”
Location =”ScriptLink”
ScriptSrc =”/_layouts/SharePointProject1/RibbonActions.js” />
</ Elements >