设为首页 收藏本站
查看: 622|回复: 0

[经验分享] Sharepoint学习笔记—Ribbon系列-- 2. 在Ribbon中添加新Tab

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-25 09:24:55 | 显示全部楼层 |阅读模式
  有了上面的基础,我们来看看如何向Sharepoint网站的Ribbon中添加我们定义的Tab。
  直接进入操作步骤   
  一、创建 SharePoint 项目
  要添加新选项卡,应首先创建一个空白 SharePoint 项目。如下:
   DSC0000.jpg   
  把此方案设置成Farm解决方案
  然后在此项目中分别加入新的Feature与新的空白Element如下图
DSC0001.jpg
  
  二、使用自定义操作中的功能区 XML 定义功能区自定义项
  方法是 打开 Elements.xml 文件,其内容如下:   

DSC0002.gif DSC0003.gif View Code

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
      Id="MyCustomRibbonTab"
      Location="CommandUI.Ribbon.ListView"
      RegistrationId="101"
      RegistrationType="List">
        <CommandUIExtension>
            <CommandUIDefinitions>
                <CommandUIDefinition
                  Location="Ribbon.Tabs._children">
                    <Tab
                      Id="Ribbon.CustomTabExample"
                      Title="My Custom Tab"
                      Description="This holds my custom commands!"
                      Sequence="501">
                        <Scaling
                          Id="Ribbon.CustomTabExample.Scaling">
                            <MaxSize
                              Id="Ribbon.CustomTabExample.MaxSize"
                              GroupId="Ribbon.CustomTabExample.CustomGroupExample"
                              Size="OneLargeTwoMedium"/>
                            <Scale
                              Id="Ribbon.CustomTabExample.Scaling.CustomTabScaling"
                              GroupId="Ribbon.CustomTabExample.CustomGroupExample"
                              Size="OneLargeTwoMedium" />
                        </Scaling>
                        <Groups Id="Ribbon.CustomTabExample.Groups">
                            <Group
                              Id="Ribbon.CustomTabExample.CustomGroupExample"
                              Description="This is a custom group!"
                              Title="Custom Group"
                              Sequence="52"
                              Template="Ribbon.Templates.CustomTemplateExample">
                                <Controls Id="Ribbon.CustomTabExample.CustomGroupExample.Controls">
                                    <Button
                                      Id="Ribbon.CustomTabExample.CustomGroupExample.HelloWorld"
                                      Command="CustomTabExample.HelloWorldCommand"
                                      Sequence="15"
                                      Description="Says hello to the World!"
                                      LabelText="Hello, World!"
                                      TemplateAlias="cust1"/>
                                    <Button
                                      Id="Ribbon.CustomTabExample.CustomGroupExample.GoodbyeWorld"
                                      Command="CustomTabExample.GoodbyeWorldCommand"
                                      Sequence="17"
                                      Description="Says good-bye to the World!"
                                      LabelText="Good-bye, World!"
                                      TemplateAlias="cust2"/>
                                    <Button
                                      Id="Ribbon.CustomTabExample.CustomGroupExample.LoveWorld"
                                      Command="CustomTabExample.LoveWorldCommand"
                                      Sequence="19"
                                      Description="Says I love the World!"
                                      LabelText="I love you, World!"
                                      TemplateAlias="cust3"/>
                                </Controls>
                            </Group>
                        </Groups>
                    </Tab>
                </CommandUIDefinition>
                <CommandUIDefinition Location="Ribbon.Templates._children">
                    <GroupTemplate Id="Ribbon.Templates.CustomTemplateExample">
                        <Layout
                          Title="OneLargeTwoMedium"
                          LayoutTitle="OneLargeTwoMedium">
                            <Section Alignment="Top" Type="OneRow">
                                <Row>
                                    <ControlRef DisplayMode="Large" TemplateAlias="cust1" />
                                </Row>
                            </Section>
                            <Section Alignment="Top" Type="TwoRow">
                                <Row>
                                    <ControlRef DisplayMode="Medium" TemplateAlias="cust2" />
                                </Row>
                                <Row>
                                    <ControlRef DisplayMode="Medium" TemplateAlias="cust3" />
                                </Row>
                            </Section>
                        </Layout>
                    </GroupTemplate>
                </CommandUIDefinition>
            </CommandUIDefinitions>
            <CommandUIHandlers>
                <CommandUIHandler
                  Command="CustomTabExample.HelloWorldCommand"
                  CommandAction="javascript:alert('Hello, world!');" />
                <CommandUIHandler
                  Command="CustomTabExample.GoodbyeWorldCommand"
                  CommandAction="javascript:alert('Good-bye, world!');" />
                <CommandUIHandler
                  Command="CustomTabExample.LoveWorldCommand"
                  CommandAction="javascript:alert('I love you, world!');" />
            </CommandUIHandlers>
        </CommandUIExtension>
    </CustomAction>
</Elements>  下面对其内容作如下说明:  


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <CustomAction
       Id="MyCustomRibbonTab"
       Location="CommandUI.Ribbon.ListView"
       RegistrationId="101"
       RegistrationType="List">
         <CommandUIExtension>
             <CommandUIDefinitions>
                 <CommandUIDefinition
                   Location="Ribbon.Tabs._children">
  在对Ribbon进行自定义时,你需要把CustomAction 元素与Ribbon的XML 一起使用。CustomAction元素的Location 属性告知 CustomAction 在何处应用自定义项(即此元素内的所有自定义内容)。
  下表对这些Location可能的值进行了说明。本例我们把Location值设置为CommandUI.Ribbon.ListView也即我们此处定义的自定义项(Tab)将在我们显示列表视图的WebPart时出现在Ribbon区域,而基其它时候我们是看不见它的。

  值
  说明
  CommandUI.Ribbon
  对于指定的 RegistrationId,自定义项出现在任何地方。
  CommandUI.Ribbon.ListView
  当存在列表视图 Web 部件时出现自定义项。
  CommandUI.Ribbon.EditForm
  自定义项出现在编辑表单上。
  CommandUI.Ribbon.NewForm
  自定义项出现在新建表单上。
  CommandUI.Ribbon.DisplayForm
  自定义项出现在显示表单上。
  关于RegistrationId属性:用于指定与此操作(CustomAction )关联的列表或项内容类型的标识符,或文件类型或编程标识符 (ProgID)。关于它们的含义请参见
  Sharepoint学习笔记&#8212;Ribbon系列-- Reference :List definitions Type and BaseType ,此处我们设置为101也即我们自定义的CustomAction与
  Document library //文档库 进行关联。也就是说当我们选择Sharepoint网站的某个DocumentLibrary List时,就会出现我们自下定义的CustomAction选项卡(Tab),也就是说,
  在CustomAction的相关属性设置中,我们解决了它显示的&#8220;时机&#8221;问题。
  RegistrationType属性: 用于给每项操作指定注册附件。可能的值包括:ContentType,FileType,List,ProgId
  


<CommandUIExtension>
             <CommandUIDefinitions>
                 <CommandUIDefinition
                   Location="Ribbon.Tabs._children">
                     <Tab
                       Id="Ribbon.CustomTabExample"
                       Title="My Custom Tab"
                       Description="This holds my custom commands!"
                       Sequence="501">
  CommandUIDefinition 元素上的 Location 定义其内部控件呈现的位置。在本示例中,您将引用服务器功能区(Ribbon)的 Tabs 集合(Ribbon.Tabs._childern)。_children 约定告知功能区将以下 XML 插入到输出中以便呈现功能区。在本例中,您将插入 Tab 元素 XML。
  Tab元素中的Sequence 属性将定义Tab相对于其他Tabs所呈现的位置。Sharepoint系统默认的Tab使用 100 的整倍数作为它们的Sequence值,因此用户自定义的Tab的Sequence 属性不应是 100 的整倍数以防止与系统的默认Sequence值出现冲突。应避免冲突,以确保对功能区 XML 进行适当处理。
  由此可见,CustomAction影响了显示的&#8220;时机&#8221;,此处CommandUIDefinition与Tab中的相关属性设置影响了显示的"位置"。
  

  

                         <Scaling
                           Id="Ribbon.CustomTabExample.Scaling">
                             <MaxSize
                               Id="Ribbon.CustomTabExample.MaxSize"
                               GroupId="Ribbon.CustomTabExample.CustomGroupExample"
                               Size="OneLargeTwoMedium"/>
                             <Scale
                               Id="Ribbon.CustomTabExample.Scaling.CustomTabScaling"
                               GroupId="Ribbon.CustomTabExample.CustomGroupExample"
                               Size="OneLargeTwoMedium" />
                         </Scaling>
  
  在创建自定义Tab时,您必须定义在添加控件时如何对Tab进行缩放。通过将 Scaling 元素与 GroupTemplate 一起使用可对此进行处理。
  MaxSize 元素对组(Group)中控件的最大大小进行定义。而 Scale 元素定义组(Group)如何在不同的情况下进行缩放。
  GroupId 属性将一个组与缩放大小相关联。
  Size 属性由稍后在GroupTemplate 中定义的 Layout 元素定义。
  


                        <Groups Id="Ribbon.CustomTabExample.Groups">
                             <Group
                               Id="Ribbon.CustomTabExample.CustomGroupExample"
                               Description="This is a custom group!"
                               Title="Custom Group"
                               Sequence="52"
                               Template="Ribbon.Templates.CustomTemplateExample">
                                 <Controls Id="Ribbon.CustomTabExample.CustomGroupExample.Controls">
                                     <Button
                                       Id="Ribbon.CustomTabExample.CustomGroupExample.HelloWorld"
                                       Command="CustomTabExample.HelloWorldCommand"
                                       Sequence="15"
                                       Description="Says hello to the World!"
                                       LabelText="Hello, World!"
                                       TemplateAlias="cust1"/>
  Groups 元素对将在选项卡上出现的组进行定义。Group 元素自身具有与其他控件相似的属性以及一个 Template 属性。
  Template 属性引用我们后面将要定义的 GroupTemplate,Sharepoin要求每一个Group都必须与某个特定的GroupTemplate项进行绑定,并推荐用户尽量使用用户自已定义的GroupTemplate.
  Controls 元素包含将在组中出现的控件。组内部的控件必须定义 TemplateAlias 和 Command 属性。与Tab类似,每个控件具有一个 Sequence 属性,用于定义这些控件将在组中出现的位置。默认控件基于 10 的整倍数,所以任何自定义控件不应使用 10 的整倍数以避免发生冲突。Command 属性由 CommandUIHandler 元素使用,并且即使在未指定 CommandUIHandler 时也要求使用此属性。TemplateAlias 属性定义控件相对于 GroupTemplate 出现的位置。
  


                 <CommandUIDefinition Location="Ribbon.Templates._children">
                     <GroupTemplate Id="Ribbon.Templates.CustomTemplateExample">
                         <Layout
                           Title="OneLargeTwoMedium"
                           LayoutTitle="OneLargeTwoMedium">
                             <Section Alignment="Top" Type="OneRow">
                                 <Row>
                                     <ControlRef DisplayMode="Large" TemplateAlias="cust1" />
                                 </Row>
                             </Section>
                             <Section Alignment="Top" Type="TwoRow">
                                 <Row>
                                     <ControlRef DisplayMode="Medium" TemplateAlias="cust2" />
                                 </Row>
                                 <Row>
                                     <ControlRef DisplayMode="Medium" TemplateAlias="cust3" />
                                 </Row>
                             </Section>
                         </Layout>
                     </GroupTemplate>
                 </CommandUIDefinition>
             </CommandUIDefinitions>
  定义组模板(GroupTemplate)时,您必须将其定义为另一个 CommandUIDefinition。CommandUIDefinition 具有 Ribbon.Templates._children 位置。这与用于组和选项卡的模式相同。
GroupTemplate 元素包含一个 Layout 元素,后者又包含 Section 或 OverflowSection 元素。Layout 元素具有一个 Title 属性,可用于 MaxSize 和 Scale 元素上的 Size 属性。
Section 元素具有两个属性。Alignment 属性对以下 Row 元素中的控件所处的位置进行定义。Type 属性定义将在对应的部分显示的行数。一个 Section 最多具有三个 Row 元素。
Row 元素包含一个或多个 ControlRef 元素。每个 ControlRef 元素定义一个控件在功能区的显示方式。DisplayMode 属性包含以下值(注:并非所有的控件都提供所有的 DisplayMode 值)。

  值
  说明
  Small
  显示为无标签文本的小图标。
  Medium
  显示为带有标签文本的 16 x 16 的图标。
  Large
  显示为带有标签文本的 32 x 32 的图标。
  Text
  仅显示为文本。
还可以有一个 OverflowSection 元素而不是 Section 元素。此元素定义一个区域,在此区域中,不必使用 Row 元素即可显示多个控件。所有的控件都将显示为由 DisplayMode 属性定义的相同大小。DividerAfter 和 DividerBefore 属性定义在显示溢出部分时分隔线将出现的位置。
向默认功能区位置添加控件时,您应该考虑组模板和缩放。向默认位置添加控件可改变组的显示。大多数默认组模板包含将随自定义控件一起增长的溢出部分。在更高级的方案中,您可以重写缩放来根据设计需要显示控件。


             <CommandUIHandlers>
                 <CommandUIHandler
                   Command="CustomTabExample.HelloWorldCommand"
                   CommandAction="javascript:alert('Hello, world!');" />
                 <CommandUIHandler
                   Command="CustomTabExample.GoodbyeWorldCommand"
                   CommandAction="javascript:alert('Good-bye, world!');" />
                 <CommandUIHandler
                   Command="CustomTabExample.LoveWorldCommand"
                   CommandAction="javascript:alert('I love you, world!');" />
             </CommandUIHandlers>
   CommandUIHandlers 元素包含所有 CommandUIHandler 元素。CommandUIHandler 元素定义功能区上的控件如何响应某个操作。Command 属性是与随控件定义的 Command 属性一起使用的命令的唯一名称。CommandAction 属性包含可对控件执行的操作。此操作可以是 ECMAScript(JavaScript、JScript)、URL 或 UrlAction 元素中以前包含的任意内容。
  
  三、部署自定义项
    按 F5。Visual Studio 2010 中的 SharePoint 开发工具将自动构建和部署功能。
    转到网站或子网站中的文档库。
   单击&#8220;My Custom Tab&#8221;选项卡,查看&#8220;自定义组&#8221;,然后单击&#8220;Hello, World&#8221;、&#8220;Good-bye, World&#8221;或&#8220;I Love You, World&#8221;按钮。
  
DSC0004.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118484-1-1.html 上篇帖子: Sharepoint学习笔记—习题系列--70-573习题解析 下篇帖子: SharePoint 2010 列表字段之KPI
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表