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.