2

Basically my questiong is regarding the ribbon.

So I want to have the following behaviour, when a person clicks on the button, the contextual ribbon tab should be opened.

Any idea how to do that?

I have the following code:

            SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
            string contextualTabId = ribbonItems[listType];         
            ribbon.MakeTabAvailable(contextualTabId);
            ribbon.MakeContextualGroupInitiallyVisible(contextualTabId, string.Empty);

But the problem is it disappeares after a second.

Another solution might be to focus the web part but how to do it from c#? I tried with js but was not a good solution. And it didn't work very well either.

So any idea?

Shkipper
  • 2,105
  • 1
  • 23
  • 52

2 Answers2

6

Basically I've still succeeded to fix my problem by using the JavaScript.

Here is the code:

var elem = document.getElementById("MSOZoneCell_WebPartWPQ2");
if (elem != null) {
    var dummyevent = new Array();
    dummyevent["target"] = elem;
    dummyevent["srcElement"] = elem;
    elem.parentNode.click(); 
    WpClick(dummyevent);
   }

Hope this helps.

Shkipper
  • 2,105
  • 1
  • 23
  • 52
2

I had to add window.onload to make Shkipper's answer work:

<script unselectable="on">
  window.onload = function(){
  var elem = document.getElementById("MSOZoneCell_WebPartWPQ2");
  if (elem != null) {
      var dummyevent = new Array();
      dummyevent["target"] = elem;
      dummyevent["srcElement"] = elem;
      elem.parentNode.click(); 
      WpClick(dummyevent);
// Below code will click browse

document.getElementsByClassName("ms-cui-tt-span")[0].click();
   }
}
</script>
love thakker
  • 1,136
  • 1
  • 16
  • 41
Kevin .NET
  • 201
  • 1
  • 4
  • This will default open the respective ribbon. added document.getElementsByClassName("ms-cui-tt-span")[0].click(); in code to stop this behavior. – love thakker Aug 23 '18 at 10:54