When you add document library web parts to a page, SharePoint automatically sets the width of the columns leaving you with multiple web parts, all with different column widths, and an unstructured look to the page.
Synchronizing the column widths can be done with yet another Content Editor Web Part (CEWP) trick. The trick this time is finding something in common across all of the library web parts on a page, and it turns out that this is the table ID: "onetidDoclibViewTbl0". It looks like this ID is used for Document Libraries and Wiki Libraries.
The JavaScript below will take the column widths from the first web part on the page and then adjust the column widths for all other library web parts on the same page. This should only impact library web parts, and not others such as Tasks or Calendars, but you will need to do some testing!
Steps:
- In 2010 create a Notepad file with the 2010 version of the code. Save the file to a local drive and then upload to a SharePoint library in the site. In the library, right-click the file name, click Properties and copy the URL to the file. In the Content Editor Web Part, paste the URL in the Source Link box. Site Actions, Site Settings, Edit Page
- Add a Content Editor web part and move it below the library web parts
- Click Edit, Modify Shared Web Part
- In the Appearance section set the title of the web part to something like “Document Library Column Width Adjuster” so you will know what it is for six months from now.
- In the Appearance section find Chrome and select None to hide the title bar
- In 2007 click Source Editor and paste the following JavaScript
The Code
(this is the 2007 version, scroll down for the 2010 version)
<script type="text/javascript" language="javascript">
function CoordinateColWidths() {
try
{
//find all document library web parts
var x = document.all["onetidDoclibViewTbl0"]
//Get first table's column widths
var cw = []
var j
for (j=0;j<x[0].rows[0].cells.length;j++)
{
cw[j] = x[0].rows[0].cells[j].offsetWidth
}
//Set all other tables
var i;
for (i=1;i<x.length;i++)
{
for (j=0;j<cw.length;j++)
{
x[i].rows[0].cells[j].width = cw[j]
}
}
}
catch(err)
{
txt="<font size=1>There was an error adjusting column widths.<br/>";
txt+="Error description: " + err.description + "</font>";
CoordinateColWidthsErr.innerHTML=txt
}
}
//call only after page fully loaded
_spBodyOnLoadFunctionNames.push("CoordinateColWidths")
</script><span id=CoordinateColWidthsErr></span>
Watch outs and warnings!
- This has not been tested in a production environment – you are on your own! (Please let me know if it works or does not work for you)
- So far I have only tested this:
- in IE 6 on a MOSS Enterprise installation, but it should work in WSS.
- in IE 7, IE 8 and Firefox in SharePoint Server 2010
- As this uses a hidden web part, remember to document this somewhere so you can recreate the site when needed..
- Batteries not included, your mileage may vary…
Have fun, and let me know if this works or does not work for you.
Code for 2010
<script language="javascript" type="text/javascript"> // SP 2010 version function CoordinateColWidths() { try { //find all document library web parts var x = []; var tables = document.getElementsByTagName("table"); for (var i=0; i<tables.length; i++) { if (tables[i].id=="onetidDoclibViewTbl0") { x[x.length]=tables[i]; } } //Get first table's column widths var cw = []; var j; for (j=0;j<x[0].rows[0].cells.length;j++) { cw[j] = x[0].rows[0].cells[j].scrollWidth-16; } //Set all other tables var i; for (i=1;i<x.length;i++) { for (j=0;j<cw.length;j++) { x[i].rows[0].cells[j].width = cw[j]; } } } catch(err) { txt="<font size=1>There was an error adjusting column widths.<br/>"; txt+="Error description: " + err.description + "</font>"; CoordinateColWidthsErr.innerHTML=txt; } } //call only after page fully loaded _spBodyOnLoadFunctionNames.push("CoordinateColWidths"); </script> <span id="CoordinateColWidthsErr"></span>
.
7 comments:
It does not work for Sharepoint 2010. Any solution for SP2010 ? Thanks.
-vmrao
> It does not work for Sharepoint 2010. Any solution for SP2010 ?
Not yet, but it's on my to-do list!
Mike, this is EXCELLENT! Works great on my libraries. Been trying to find a work around for weeks.
Hi Mike,
Just wondering if you ever found a 2010 solution for this!
Thanks,
Justin
Justin,
I have added the 2010 version to the end of the article above.
Mike
Hi All,
This code is exactly what i'm looking for. Unfortunately, i'm not that advanced with JS at all. I have tried applying this to a content editor at the very end of the web part list and get the error below.
There was an error adjusting column widths.
Error description: Unable to set property 'width' of undefined or null reference
Would assume it can't find the first table reference. Could this be the way that my Sharepoint site is set up? Currently, I have a content editor as a title, then document libraries in a list underneath.
Hope you can offer some help if possible?
Many thanks,
Carl.
Carl,
Are you using this in SP2007 or SP2010? I have not tested in 2013.
As a test, move you content editor web below the library web parts.
Mike
Post a Comment