When you open a linked WorkItem Query in TFS Web Access the
default initial state is expand all and if you have massive tree of
linked WorkItem it’s kind of annoying:

Today one of my customers asked me if I can change this default
behavior and make it appear by default with all nodes collapsed.
BTW, in Team Explorer there are buttons for collapse and expand all
but Web Access doesn’t offer this option as described here.
After a little digging in pages an java scripts I found the solution.
Disclaimer: The solution provided below is on your own risk.
On the server that hosts TFS 2010 navigate to:
C:\Program Files\Microsoft Team Foundation Server 2010\Application Tier\Web Access\Web\Resources\Scripts .
Find the file QueryResultGrid.js (backup original file before you make
any change). Open the file and find initialize function at the end of the
function before the line:
this.m_initialized = true;
Copy and paste this line:
The function now should look like this:
/*overridden methods*/
initialize: function () {
TSWA.QueryResultGrid.callBaseMethod(this, 'initialize');
this.m_rows = {};
this.m_gutterRows = {};
this.m_rowStack = [];
this.m_neededWorkItemsStack = [];
this.m_selectedRows = {};
this.prepareQueryInfo();
this.takeMeasurements();
this.attachDOMEvents();
if (this.m_queryInfo.isDirty) {
this.setDirty(true, false);
}
Sys.Debug.trace(String.format("Query ResultGrid on initialize"));
this.layout();
this.collapseAllNodes();
this.m_initialized = true;
},
Save and reopen web page.
That’s it !

How it works ? this Java Script dynamically creates the result grid on
demand, if you will scroll down the file you will fund that the function
collapseAllNodes already declared, we just added a call to it at the
end of grid initialization.
Thanks to Shlomo for his help.