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 have a list page to which I need to add an additional webpart (in this case, a more powerful filter). However, the ribbon doesn't appear because the listview does not have focus.

This is easily overcome simply by clicking it, but I have an even bigger related issue. When you select items in the list, and then click somewhere else (including inside of a dialog box!), the listview loses focus again and the selected items get unselected. This is a major problem because my dialog box needs to know what items were selected. Does anyone know if there's any way to prevent this?

Based on the answer to this question, it looks like there's some Microsoft JavaScript function which checks for a single web part and thus causes this annoying behavior. Maybe there's a way to trick that function into thinking there's only a single web part?

share

2 Answers 2

up vote 1 down vote accepted

After a ton of stepping through the SharePoint JS in a debugger, I've finally found a way to prevent this problem.

In SharePoint 2010:

//Set focus on our list web part
var webPart = document.getElementById('WebPartWPQ1');
WpClick({target: webPart});

//Prevent it from losing focus
SP.Ribbon.WebPartComponent.$3_1.deselectWebPartAndZone = function() { };

In SharePoint 2013 Beta:

//Set focus on our list web part
var webPart = document.getElementById('MSOZoneCell_WebPartWPQ2');
WpClick({target: webPart});

//Prevent it from losing focus
SP.Ribbon.WebPartComponent.$3.deselectWebPartAndZone = function() { };  

Note: This is super-hacky, and is in no way supported by Microsoft (thus it's very likely to change in a future version or possibly even the RTM of SharePoint 2013).

Also, note that your web part ids are likely to be different, so you should double check you're giving focus to the correct web part.

Explanation: It basically overrides the instance of SP.Ribbon.WebPartComponent's ability to deselect a web part. From what I can tell, the $3/$3_1 property stores a reference to the SP.Ribbon.WebPartComponent instance.

If anyone knows of a better way to access the instance of SP.Ribbon.WebPartComponent other than the $3/$3_1 property, please speak up, as that would be make this method much more robust.

share

Use JQuery .focus() on document load to target the element you want to be focused.

That's the only solution that came to my head.

share
    
The problem is not the initial focus. It is what happens after the user selects items and then some control creates a dialog box. The list loses focus and the items get unselected. –  MgSam Aug 28 '12 at 13:40
    
I see, you might be able to do something by binding an event to the onchange to store the details somewhere else as they are updated, you can't keep jumping the focus back sadly. I think I would do that, and have the dialogue pull from there. It is perhaps the quickest fix. –  Hugh Wood Aug 28 '12 at 13:43

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .