Monday, June 6, 2011

Add every installed web part to a single SharePoint page

Here is a code snippet that won't be used for normal day usage, but something is very helpful for testing web part UI and branding. This could also be used for performance benchmarking and exploring or demonstrating different web parts in a development environment. By default, the code will alphabetize the web parts by title and then adds them to the top web part zone.You can easily update the code to filter out specific web parts, update web part properties, populate specific web part zones.

InstructionsThe first thing you’ll want to do is increase the maximum web parts per zone. The default is set to 50, but you can increase it by editing your web.config.

Friday, June 3, 2011

Create a favorites icon (favicon) for your SharePoint 2010 site

With SharePoint 2010, Microsoft has provided a new control called SPShortcutIcon. Sadly, if you are trying to use this within a feature deployment and would like to use relative paths using $SPUrl variables, you will run into some issues. This is because the SPShortcut.IconUrl is of type URI and the $SPUrl variable cannot be cast properly. Fortunately, there is a work around. It’s a bit ugly, but instead of using the control, you can use Literal controls to render the proper html link control.
SPShortcut usage– This is hardcoded, so won’t work with feature deployments.
<SharePoint:SPShortcutIcon runat="server" IconUrl="/Style Library/Waffles/favicon.ico"/>
SPShortcut usage with $SPUrl – This will result in the following error: Unable to cast object of type 'System.String' to type 'System.Uri'.
SharePoint:SPShortcutIcon runat="server" IconUrl="<% $SPUrl:~sitecollection/Style Library/Acme/favicon.ico %>" />

asp:Literal runat="server" Text="&lt;link rel='shortcut icon' href='" /><asp:Literal runat="server" Text="<% $SPUrl:~sitecollection/Style Library/Acme/favicon.ico %>" /><asp:Literal runat="server" Text="' type='image/' /&gt;" />’s favorite icon

SharePoint 2010, Favorites Icons (FavIcons) and SPUrl (SPUrlExpressionBuilder)Credit goes to Joel for this tip. Unfortunately, I could only get to his post via Google cache.

Wednesday, June 1, 2011

Get SharePoint audit details across the farm for specific users

This code snippet gets all audit details for specific users across an entire SharePoint 2007/2010 farm and saves them to a CSV file. I have also added a method to include a file extension column. This comes in handy as you will likely want to filter out web extensions like aspx, html, css, etc., picture formats, and folders. In order to get any data, auditing will have to have been enabled at the Site Collection level through site settings or programmatically at the item level. Keep in mind that this will take some time if your AuditData tables are very large, so in this case you will want to make further refinements by filtering by site collection, list, or list item. Site collections can be filtered out by tweaking the loop statements below, while you can filter out list or list items by updating the RestrictToList or RestrictToListItem methods of the SPAuditQuery object.
Helpful Links
Configure audit settings for a site collections
Custom Auditing in SharePoint

Create a c# Console Application in Visual Studio, add a reference to Microsoft.SharePoint, and then add the following code to Program.cs. This will work with SharePoint 2007 and 2010 as long as you reference the correct assembly. Don’t forget to make sure your Visual Studio project’s Platform Target is Any CPU or x64.