4
votes

I've created a sharepoint page that has an xslt webpart and a 2nd webpart that is unrelated to the question

When we add this second webpart the ribbon bar is hidden and you have to click the webpart to get the ribbon bar shown again. Clicking the webpart isn’t something we can ask from our users so I’m trying to get the ribbon bar visible at all times with the context of our xslt listview webpart.

When searching for this problem I found out that when you search for this hidden ribbon behavior with reflector in the SharePoint source code it seems this is behavior that is designed by Microsoft as the example below shows:

public override bool Visible { 
get {
if (!this.SingleWebPartPresentOnPage)
return false;
else
return base.Visible; 
} 
}

Someone with same problem but no solution: http://www.glynblogs.com/2011/02/list-view-selector-missing-with-multiple-web-parts-in-sharepoint-2010.html

Is it possible to force the ribbon bar to visible with server side code or can I call the javascript code that is being used when I click the webpart to show the ribbon bar?

I think it should be possible with javascript because if you click the xslt webpart the ribbon is visible but i can't reproduce the code thats being executed.

5
I've covered the missing View selector problem here along with a couple of solutions. I've not yet got around to looking if the same technique can be used for the Ribbon. blog.pentalogic.net/2011/03/…Ryan

5 Answers

6
votes

you can use JavaScript to reselect the XSLTListViewWebPart, that the ribbon appears again.

$(document).ready(function() { 
    var target = document.getElementById("MSOZoneCell_WebPartWPQ2"); 
    if(target != null) { 
        var fakeEvent = new Array();
        fakeEvent["target"] = target;
        fakeEvent["srcElement"] = target;
        WpClick(fakeEvent); 
    } 
 });
1
votes

Below Javascript worked for me!!

<script>
setTimeout(function() {
var elem = document.getElementById("MSOZoneCell_WebPartWPQ4");
if(elem != null) {
var dummyevent = new Array();
dummyevent["target"] = elem;
dummyevent["srcElement"] = elem;
WpClick(dummyevent);
}
}, 100);
</script>

In the above script the MSOZoneCell_WebPartWPQ4 being my list view web part

0
votes

A great solution is to grab the contextualInfo for the main webpart on your view page.

public class MyView : WebPart, IWebPartPageComponentProvider 
{

      protected override void CreateChildControls(){.............}
      public WebPartContextualInfo WebPartContextualInfo
      {
            get
            {
                // get default current view webart (WebPartWPQ2)
                ListViewWebPart listView = this.WebPartManager.WebParts
                     .OfType<ListViewWebPart>().FirstOrDefault();
                // use reflection to get non-public member containing contextualinfo
                var t = listView.GetType();
                WebPartContextualInfo oViewInfo = (WebPartContextualInfo)t.InvokeMember("Microsoft.SharePoint.WebControls.IWebPartPageComponentProvider.WebPartContextualInfo", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.GetProperty, null, listView, new object[] { });

                return oViewInfo;
            }
        }
        protected override void OnPreRender(EventArgs e)
        {

            SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
            // Ensure ribbon exists.
            if (ribbon != null)
            {
                // Load dependencies if not already on the page.
                ScriptLink.RegisterScriptAfterUI(this.Page, "SP.Ribbon.js", false, true);
            }
            base.OnPreRender(e);
       }
}
0
votes

Below a version using SharePoint's Script On Demand instead of 100ms timeout or jquery. I think thats more solid, cuz it is exactly executed after the ribbon is initialized.

SP.SOD.executeOrDelayUntilScriptLoaded(function () {
    //using setTimeout to ensure it will be executed after the code of sp.ribbon.js has done its initialization
    setTimeout(function () {
        //try to focus the default webpart so the ribbon will show
        var elem = document.getElementById("MSOZoneCell_WebPartWPQ2");
        if (elem != null) {
            var dummyevent = new Array();
            dummyevent["target"] = elem;
            dummyevent["srcElement"] = elem;
            WpClick(dummyevent);
        }
    }, 0);
}, "sp.ribbon.js");
0
votes

Similar to Thorstens solution, I use jQuery to fire the WpClick function on the mouseenter event. This approach also handles the issue where the Full Toolbar freaks out when a user first enters a page and tries to use one of the menus. You can trap the event bubble for any number of web parts on the page if desired. For example:

$("body").on("mouseenter","#MSOZoneCell_WebPartWPQ2,#MSOzoneCell_WebPartWPQ3, . . . etc.",function() {
  WpClick(event);
});

Where "body" could be any parent element you want that contains the web parts to auto select when hovering.

When only one web part is of concern, or for optimal performance on large pages you could also set the event directly on the zone.

$("#MSOZoneCell_WebPartWPQ2").attr("onmouseenter","WpClick(event)");

or if jQuery is not available

var el = document.getElementById("MSOZoneCell_WebPartWPQ2"); 
if(el != null) {
  el.setAttribute('onmouseenter','WpClick(event);');
}

Optionally, you can still force the Ribbon to appear after the page loads and before a user hovers by triggering the event manually. Just include the appropriate code after attaching the event above. e.g. using jQuery

$("#MSOZoneCell_WebPartWPQ2").mouseenter();