Wednesday, June 19, 2013

Add borders in SharePoint 2007 list view

Sometime we get some requirement to add some styles in SharePoint list views. Yesterday, I got it from CEO that he wants to add lines in between each list item in all lists in his Team Site. I tried to add following CSS style on the page by following Heather Solomon guide but for some reason it was not working for me. http://bit.ly/11mXRAd

/*Set border for every row */
.ms-listviewtable > tbody > tr td{
border-bottom: 1px solid #AFAFAF !important; /* !important needed over override SharePoint inline style */
}
 




Initially sample list was look like this.
 

image
 

I added few lines of jQuery on the page and it started look like this.
 

image
 

Following is the code I used to do this.

 

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

$(document).ready(function(){
$("td:empty").html('&nbsp;');

var $listView = $('.ms-listviewtable .ms-vb2 ');
$listView.attr( 'style', 'border-bottom: 1px solid black !important');

var $listView1 = $('.ms-listviewtable .ms-vb-title');
$listView1.attr( 'style', 'border-bottom: 1px solid black!important');

var $listViewTable = $('.ms-listviewtable');

$listViewTable.attr('style', 'border-collapse: collapse !important');

});
</script>

Thursday, June 13, 2013

Read column value for a list item programmatically

While reading list item’s column value using code, we get exception if column is empty/ null. To avoid such exception, we can use this line of code while reading column value.

if (item["ColumnInternalName"] != null && SPEncode.HtmlEncode(item["ColumnInternalName"].ToString()) != string.Empty && !string.IsNullOrEmpty(item["ColumnInternalName"].ToString()))
{
ImageFieldValue image = (ImageFieldValue)item["ColumnInternalName"];
imageURL = image.ImageUrl;
}
else
{
imageURL = "/_layouts/images/icon.png";
}