4/27/2009

SharePoint: Displaying Pictures and Thumbnails in a Data View Web Part

 

This is in response to a question about displaying a thumbnail from a Picture Library in a Data View web part…

 

Displaying the full size picture is really easy. When selecting your fields for the Data View web part select “Url Path”. This will be added to the XSLT of the Data View web part as:

<img border="0" src="{@FileRef}" alt="{@FileRef}"/>

(@FileRef is the XSLT fieldname used for “Url Path”)

 

The trick is getting the URL to the picture library’s auto-created thumbnail. The thumbnail for “test.jpg” is “/_t/test_jpg.jpg” and is not a field in the SharePoint Designer list of fields. To get it you will need to do some XSLT string work:

concat(@FileDirRef,'/_t/', substring-before(@FileLeafRef,'.'),'_',substring-after(@FileLeafRef,'.'),'.',@FileType)

Note: the above will only work if only one “.” exists in the file name. It will fail for a file named my.airshow.picture.jpg

    @FileDirRef is the name of the library (airshow%20pictures)

    @FileLeafRef is the file name (helicopter.jpg)

    @FileType is the file type (jpg)

    ‘/_t/’ is the path to the auto-created thumbnails

  substring-before / after are XSLT string functions

The above “stuff” will convert:

   /airshow%20pictures/helicopter.jpg

into

   /airshow%20pictures/_t/helicopter_jpg.jpg 

which is the path to the thumbnail.

 

The final <IMG> tag looks like this:

<img border="0" src="{concat(@FileDirRef,'/_t/', substring-before(@FileLeafRef,'.'),'_',substring-after(@FileLeafRef,'.'),'.',@FileType)}" alt="{@FileRef}"/>

 

If you would like to let the user click on the thumbnail to see the full picture and meta data then wrap the <IMG> in a <A> tag and supply the ID of the picture:

<a href="{concat(@FileDirRef,'/Forms/DispForm.aspx?ID=', @ID)}">
<img border="0" src="{concat(@FileDirRef,'/_t/', substring-before(@FileLeafRef,'.'),'_',substring-after(@FileLeafRef,'.'),'.',@FileType)}" alt="{@FileRef}"/>
</a>

SharePoint: Synchronize Document Library Web Part Column Widths

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:

  1. 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
  2. Add a Content Editor web part and move it below the library web parts
  3. Click Edit, Modify Shared Web Part
  4. 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.
  5. In the Appearance section find Chrome and select None to hide the title bar
  6. 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>

 

 

.

4/24/2009

Get on the bus (at MAX!)

Microsoft has a bus! The CAREER EXPRESS BUS, and it's going to TechEd 2009. Each day of the trip, the CAREER EXPRESS will visit Microsoft Learning partners and IT Academies; meet with individuals, organizations, authors, and user group leaders; and hold Microsoft Certified Professional (MCP) and Microsoft Certified Trainer (MCT) meet-ups along the way. On May the 5th the bus is stopping at MAX Technical Training. If you are in the Cincinnati / Dayton area stop in and visit, get a free lunch and maybe win some cool prizes! (there are rumors of an X-Box and a lot of other stuff!)
More info here.
MAX will have various games and activities at the event to win entries into the multiple PRIZE DRAWINGS. For the free food and the prize drawing, you must register: REGISTER HERE How to get in on the drawings: YOU MUST:
  • REGISTER for the event = 1 entry
  • Attend the event = 1 entry
  • Drag another body to the event = 1 entry per additional body (body must be breathing)
  • Student Alumni - if you register and attend you will get 1 additional entry for each class you have already attended at MAX during the 2008 and 2009 calendar years
  • Bring your MCP or MCT identification card = 1 entry
  • Play the various games to earn even more entries
  • DRAWINGS WILL BE AT (approx.) 12:30 MUST BE PRESENT TO WIN

See you there!

Mike

4/01/2009

SharePoint: Adding User Profile when Using Local Accounts

Manually adding Local Accounts to Shared Services Profiles Just a note about odd behaviour when using local accounts with SharePoint... You are most likely to run into this within a test VPC... Shared Services -> User Profile and Properties, View user Profiles: Clicking New Profile displays an edit screen where you can add a manually entered profile. If you complete the form, and then go back and check it you will find that all of the data, except for the Account Name is gone. The trick is to click New Profile, enter the Account Name (as entered in Windows in Local Users and Groups), and click Save. Then edit the profile and add the rest of the data.

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.