| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Wednesday, November 5, 2008

Change the Icon for a Web Part Page

Sorry that it's been a few days since I've posted anything here. Work is starting to divert me from producing anything really interesting and most of the real grunt-work has already been accomplished with designing the SharePoint site that I've been working on.

You might be wondering why you would want to use this code instead of just editing the web part naturally in SharePoint. If you use one page to display multiple sets of information and you want to rotate through the WP icon, this is how. For example:
States.aspx needs to show information for several states.
"States.aspx?s=Colorado" should show the Colorado state flag (Colorado.png) at the top of the page and "States.aspx?s=Arizona" should show Arizona's flag (Arizona.png).
Make sure you have the .png's in the same folder as State.aspx and use this code to swap out the default WP icon:
<script type="text/javascript">
var aryV = location.search.replace("?","").split("&");
var i=0
while (i < aryV.length)
{ if (aryV[i].indexOf("s=") >= 0)
{ strState = aryV[i].substr(aryV[i].indexOf("=")+1).replace(/%20/g, " ");
strState = strState.replace(/\+/g, " ");
}
i+=1;
}

var theImgs = document.getElementsByTagName("IMG");
var x=0;
while (x &lt; theImgs.length)
{ try
{ if (theImgs[x].src.search("wpicon.gif") &gt;= 0)
{ theImgs[x].setAttribute("src", s + ".png"); break; }
}
catch(err){}
x+=1;
}
</script>
This code goes into a Content Editor Web Part anywhere on the page.

0 comments: