SharePoint【Ribbon系列】– 09.如何讓使用者定義的Ribbon引用外部Javascript檔案

來源:互聯網
上載者:User

  在使用Sharepoint2010的Ribboon編程的領域內,我們必然要遇到如何編寫Ribbon內控制項的Javascript代碼。
  下面是Ribbon定義的一段常見代碼:

 <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>

  在CommandAction中潛入一個Javascript的Function,這個Funcation是在一個外部Javascript檔案中定義的。所以接下來的問題是,如何讓Ribbon的定義段知道它該使用哪一個外部Javascript檔案引用?
  好在Sharepoint中提供了這樣一個途徑,我們只需要加入下面的代碼就能解決這個問題。

<CustomAction Id=”Ribbon.Library.Actions.Scripts”
  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>

 轉載:http://www.cnblogs.com/wsdj-ITtech/archive/2012/07/12/2421014.html

  在使用Sharepoint2010的Ribboon編程的領域內,我們必然要遇到如何編寫Ribbon內控制項的Javascript代碼。
  下面是Ribbon定義的一段常見代碼:

 <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>

  在CommandAction中潛入一個Javascript的Function,這個Funcation是在一個外部Javascript檔案中定義的。所以接下來的問題是,如何讓Ribbon的定義段知道它該使用哪一個外部Javascript檔案引用?
  好在Sharepoint中提供了這樣一個途徑,我們只需要加入下面的代碼就能解決這個問題。

<CustomAction Id=”Ribbon.Library.Actions.Scripts”
  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>

 轉載:http://www.cnblogs.com/wsdj-ITtech/archive/2012/07/12/2421014.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.