Nov
20
2008

Showing "my links" with QuickLinksMicroView web part - Issues you should know about

Ishai's post - Showing "my links" on a web page has explained how you can use the QuickLinksMicroView web part to show users "my links" on SharePoint web part page.

The web part looked great when I first added it to the web part page, but it wasn't long before I discovered some of "bugs" in this web part. Bugs or by design?

I will show you what those bugs are by using following configurations: Two groups, 6 links in the first group and 3 links in the second group.

James Tsai .Net SharePoint Blog - QuickLinksMicroView Items

Now let's change the default "Number of rows to display" value from 10 to 2

 James Tsai .Net SharePoint Blog - QuickLinksMicroView Configuration

Click Ok and see what it gets us.

1. "Show/Hide additional links" hyperlink will not work without "portal.css" CSS registration

James Tsai .Net SharePoint Blog - QuickLinksMicroView No Portal CSS

As you can see there is a "show 4 additional links" hyperlink at bottom of the first group (Don't worry about the second group for now, I will get to that later).  Click on the link toggles the display text (hide/show 4 additional links) without change the number of displayed items in group (no actual hide or show for additional items).

The problem here is that the QuickLinksMicroView web part hides and shows item by changing the CSS class name of the items from the "groupHide" to "groupShow" or vice versa. Both "groupHide" and "groupShow" are the CSS classes defined in the "portal.css". The QuickLinksMicroView web part uses those CSS class without registering the "portal.css" within its code, that means it can only rely on the other controls on the same page to do it.

If you can't re-produce this problem, it probably means that there is a control on your page has already done the registration. You can check it by look into "SPContext.Current.m_cssList" or "SPContext.Current.CssReferences" properties and see if you can find "portal.css". Or you can simply re-produce this problem by adding the QuickLinksMicroView web part to blank new team site.

To fix the problem, override the OnLoad method (If you've decided to create a custom class that inherits from the OOTB QuickLinksMicroView class)

public class NewQuickLinksMicroView : QuickLinksMicroView

{

    protected override void OnLoad(object sender, EventArgs e)

    {

        base.OnLoad(sender, e);

        CssRegistration.Register("portal.css");

    }

}

The code above make sure the portal.css is available to the NewQuickLinksMicroView web part when it is used on the page.

You can now test "Show/Hide additional links" again with this new class, and see it is now working.

James Tsai .Net SharePoint Blog - QuickLinksMicroView with Portal CSS

2. "Show/Hide additional links" hyperlink is not displaying for the group with number of items less or equal to ((number of rows to display + 1 ) X (number of columns to display))

 

(number of rows to display + 1 ) X (number of columns to display)

 

Ok, this is a very interesting bug and you probably have no idea of what I am talking about. But let's try and experiment with it and you'll see.

 

First test -

The "Group 1" in my example has 3 items. We also set

"number of columns to display" = 1

"number of rows to display" = 2

We would expect the "Show/Hide additional links" hyperlink will be displayed for this group (to show and hide 1 additional item)

Now, applying above values to the magic formula described above, we have

1 * (2 + 1) = 3. Which is equal to the number of items in "Group 1" and you can see that the "Show/Hide additional links" hyperlink is not showing in this case.

James Tsai .Net SharePoint Blog - QuickLinksMicroView One Column Two Rows

 

Second test -

This time we change columns from 1 to 2 and rows from 2 to 1.

Group 1 items = 3

"number of columns to display" = 2

"number of rows to display" = 1

We still expect the "Show/Hide additional links" hyperlink to display in this scenario, since it still has 1 additional item to hide and show.

2 * (1 + 1) = 4. The result value is greater than the total number of items in Group 1 (3 items) and hyperlink is not showing again in this case.

 

James Tsai .Net SharePoint Blog - QuickLinksMicroView Two Columns One Row

Why (number of rows to display + 1 ) X (number of columns to display) ?

QuickLinkMicroView web part uses ShouldRenderMoreLink() method in PrivacyItemViewer class to determine if the number of items in group excess the configured number to be displayed. (Microsoft.SharePoint.Portal.WebControls.PrivacyItemViewer is the base class of QuickLinkMicroView)

//ShouldRenderMoreLink() in Microsoft.SharePoint.Portal.WebControls.PrivacyItemViewer

private bool ShouldRenderMoreLink(int rows, GroupInfo info)

{

    if(rows < this.DisplayRows)

    {

        return false;

    }

    return (info.ItemsInGroup > (this.DisplayColumns * (this.DisplayRows + 1)));

}

The "if" statement checks whether the current processing row number is smaller than the defined "number of rows to display" value. If it is true, the method returns the value "false" to indicates that the current row does not need to be hidden.

If "rows < this.DisplayRows" returns false, it means the current row number is greater than the "number of rows to display" value. It tells QuickLinksMicroView to hide this row (or any other rows from now) by default and tells web part to generate the "Show/Hide additional links" hyperlink on page.

But before it does that, it carefully checks if there are actually more items to be displayed in this row. Because rows are iterated using rows++, so it will always get an extra empty row count.

As you can see in the code block the checking is done by using (info.ItemsInGroup > (this.DisplayColumns * (this.DisplayRows + 1))), and is where the bug exist, the extra "+1" on the "this.DisplayRows" is responsible for the causes of incorrect calculation.

If you remove the "+1" from the calculation, the QuickLinksMicroView web part will hide and show extra rows correctly.

James Tsai .Net SharePoint Blog - QuickLinksMicroView Fixed

Note:

I don't see there is a purpose of having "+1" on the "this.DisplayRows". Please let me know if you do

To fix this problem (to remove the "+1") you will have to re-create or override some of the methods, variables and classes. Because ShouldRenderMoreLink() is a private method of PrivacyItemViewer and it uses an internal class as its parameter.

Hope it helps!

James

blog comments powered by Disqus