Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I basically have a WP with its own custom Ribbon Buttons and Commands. The command's are hard-coded inside the the Elements.xml. However, the webpart basically breaks once you add another "same webpart". Thie is because the JavaScript functions are not unique, not using ClientID. I cannot do ClientID because the Commands are located in the elements.xml.

Is there any way to make the commands / javascript functions unique?

Many Thanks!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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:

  1. I created a new Web Part (RibbonWebPart) using CKS Contextual Web Part
  2. Added a property:

    [WebBrowsable(true)]
    [Personalizable(PersonalizationScope.Shared)]
    public string Message { get; set; }
    
  3. Inside CreateChildControls add a hidden span with value of property:

    this.Controls.Add(new LiteralControl("<span class='message' style='display:none'>" + (Message ?? "(null)") + "</span>"));
    
  4. 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;
      }
    },
    
share|improve this answer
    
But how would I differentiate between the webparts? For instance, I have two webparts that are supposed to go to different links when their ribbon button is clicked. I can inject the url in the javascript, but how would I know which webpart's button was clicked? I have everything in place, that is the only part amiss. –  Pathachiever11 Jun 5 '12 at 22:39
    
BTW, +1 for the link, made my life much easier. –  Pathachiever11 Jun 5 '12 at 22:40
    
I've added an cookbook of how to get current web part if using PageComponent –  Per Jakobsen Jun 6 '12 at 17:08
    
Awesome, with some tweaks and code changes, I was able to make the ribbon unique and give each wp its own id....I'll probably blog my entire experience with the ribbon, so someone else doesn't have to waste time doing this craziness. –  Pathachiever11 Jun 6 '12 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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