2

There is a custom ribbon button MyButton declared inside feature and deployed on site. Now I need to change it's tooltip value in some cases:

  • Button enabled: Some text
  • Button disabled and there is value in web property bag: Some text
  • Button disabled and there are no value in web property bag: Other text

First, I've tried to get ribbon button object in JavaScript and edit it's properties after page load. Such API wasn't found, though. Actually, I can get button object in the way like:

var ribbonPageManager = SP.Ribbon.PageManager.get_instance();
var ribbon = ribbonPageManager.get_ribbon();
var button = ribbon.$38_1.$5_0
{
    Id: "Ribbon.Documents.New.MyButton",
    Alt: "My Button",
    Command: "MyButtonAction",
    LabelText: "My Button",
    Image16by16: "/_layouts/15/mybutton_16.png",
    Image32by32: "/_layouts/15/mybutton_32.png",
    TemplateAlias: "o1",
    ToolTipTitle: "My Button",
    ToolTipDescription: "Some text"
}

Second, I've tried to create PageComponent but there are no ribbon button properties either.

Next, I've tried to replace CommandUIDefinition on server side after changing web property bag value. But it duplicated existing ribbon button instead of replacing. My PowerShell snippet:

$web = Get-SPWeb http://mysite
$xml = New-Object XML;
$xml.Load("C:\CommandUIExtension.xml");
$action = $web.UserCustomActions.Add()
$action.Location = "CommandUI.Ribbon"
$action.RegistrationType="ContentType"
$action.RegistrationId="0x0101005FFBD71906344882B67C08F40847968B"
$action.Sequence = 10001
$action.Title = "Replace My Button Action"
$action.CommandUIExtension = $xml.OuterXml
$action.Update()

Where CommandUIExtension.xml contains:

<CommandUIExtension>
    <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.New.Controls._children">
            <Button Id="Ribbon.Documents.New.MyButton" 
                TemplateAlias="o1" 
                Command="MyButtonAction"
                LabelText="My Button" 
                ToolTipTitle="My Button" 
                ToolTipDescription="Other text"
                Image16by16="/_layouts/15/mybutton_16.png" 
                Image32by32="/_layouts/15/mybutton_32.png" />
        </CommandUIDefinition>
    </CommandUIDefinitions>
    <CommandUIHandlers>
        <CommandUIHandler Command="MyButtonAction" CommandAction="javascript: MyButtonAction();" EnabledScript="javascript: MyButtonActionEnable();"/>
    </CommandUIHandlers>
</CommandUIExtension>

The only solution now is to handle DOMSubtreeModified event and to change tooltip text on the go:

$(document).on('DOMSubtreeModified', function(){
    _tooltip = $("#Ribbon\\.Documents\\.New\\.MyButton_ToolTip.ms-cui-tooltip");
    //If tooltip exists and it wasn't modified yet
    if (_tooltip.length > 0 && !_tooltip.data("modified")) {
        if (MyButtonCheck()) {
            _tooltip.data("modified", true);
            _tooltip.find(".ms-cui-tooltip-description").html("Other text");
        }
    }
});

Is there any solution to resolve this problem? Thank you.

1
  • 1
    Enam, It appears that others are using JavaScript to make these type of dynamic changes to the buttons once they load. See: add-in-express.com/docs/…
    – jpollar
    Commented Jan 16, 2016 at 0:45

1 Answer 1

0

It may be resolved by CSS trick.

For example, the "Attach file" ribbon element. In html-code it presents by the following code:

<a class="ms-cui-ctl-large" 
   id="Ribbon.ListForm.Edit.Actions.AttachFile-Large" 
   role="button" 
   aria-describedby="Ribbon.ListForm.Edit.Actions.AttachFile_ToolTip" 
   onclick="return false;" 
   href="javascript:;" unselectable="on" mscui:controltype="Button">
   <!--...-->
</a>

Here we need the value of property aria-describedby: Ribbon.ListForm.Edit.Actions.AttachFile_ToolTip

It's a DOM-element Id that's generated for the tooltip: when pointer is over the button, SP generates the following code in the end of <html> element:

<span class="ms-cui-tooltip" id="Ribbon.ListForm.Edit.Actions.AttachFile_ToolTip" role="tooltip" aria-hidden="false" style="left: 256px; top: 156px; width: auto; visibility: visible; position: absolute; -ms-overflow-y: auto; max-height: none; min-width: 250px;" unselectable="on">    
    <div class="ms-cui-tooltip-body" unselectable="on">
        <div class="ms-cui-tooltip-glow" unselectable="on">
            <h1 unselectable="on">Attach file</h1>
            <div class="ms-cui-tooltip-description" unselectable="on">Attaching file in this element.</div>
        </div>
    </div>
</span>

Tooltip's text is in <div class="ms-cui-tooltip-description" unselectable="on"> element.

So we can add the following css-code by script editor webpart:

<style>
    #Ribbon\.ListForm\.Edit\.Actions\.AttachFile_ToolTip .ms-cui-tooltip-description{
        text-indent: -9999px;
        line-height:0;
    }

    #Ribbon\.ListForm\.Edit\.Actions\.AttachFile_ToolTip .ms-cui-tooltip-description:after{
        text-indent:0;
        content:'Put your text here';
        display:block;
        line-height: 15px;
    }
</style>

For your button it would be:

<style>
    #Ribbon\.Documents\.New\.MyButton_ToolTip .ms-cui-tooltip-description{
        text-indent: -9999px;
        line-height:0;
    }

    #Ribbon\.Documents\.New\.MyButton_ToolTip .ms-cui-tooltip-description:after{
        text-indent:0;
        content:'Put your text here';
        display:block;
        line-height: 15px;
    }
</style>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.