You usually don't want to make the commands or javascript functions unique, what you want is some way for them to figure out what to work on. This can very much depend on what you want them to do.
- What should the ribbon do when there is one web part on the page?
- What should it do when there is two or more?
If the ribbon works on a single web part then the best idea is probably to implement a full contextual ribbon/web part. The easiest way to do that is to use the CKS template see Wictor Wilén: CKSDev version 2.0 is released - includes Contextual Web Part SPI
It may seem like a lot of work to start with, but if you're going to do more work using the Ribbon you'll be glad you changed.
Here is a cookbook of one way to pass information from webpart to ribbon buttons:
- I created a new Web Part (RibbonWebPart) using CKS Contextual Web Part
Added a property:
[WebBrowsable(true)]
[Personalizable(PersonalizationScope.Shared)]
public string Message { get; set; }
Inside CreateChildControls add a hidden span with value of property:
this.Controls.Add(new LiteralControl("<span class='message' style='display:none'>" + (Message ?? "(null)") + "</span>"));
In PageComponent.js change handleCommand to get web part div, find "message" span and do an alert using content:
handleCommand: function (commandId, properties, sequence) {
switch (commandId) {
case 'RibbonWP.RibbonWebPart.ContextualGroup.Group.ButtonCommand':
// Get Webpart div by componentid
//
var element = document.getElementById(this._webPartPageComponentId);
// Get All spans
//
var spans = element.getElementsByTagName('span');
for (var i = 0; i < spans.length; i++) {
var span = spans[i];
// Get Message span
//
if (span.className === 'message') {
alert(span.innerHTML);
break;
}
}
break;
default:
return false;
}
},