Development

Windows Server 2008 Disk Cleanup

Posted within Development on by Cornflower Design

SharePoint Central Admin has been prompting me, each time I’ve logged in, that one of my drives is running out of space. To be honest, there isn’t a lot that I can do – a small initial system drive and successive Windows updates have gradually eaten away at the free space – but I needed to free-up what I could.

The default install of Server 2008 doesn’t come with a disk clean-up option. You would normally have to install the “Desktop Experience” feature to get this option.

Handily, I found this post from Microsoft: Disk Cleanup option on drive’s general properties and cleanmgr.exe is not present in Windows Server 2008 or Windows Server 2008 R2 by default

Downloading Solutions from Central Administration

Posted within Development on by Ryan Ball

This is a handy little PowerShell snippet that I needed to use today. There were two .WSP solutions installed within the SharePoint farm and I couldn’t locate the original source code.

$farm = Get-SPFarm
$solution = $farm.Solutions.Item("MySharePointSolution.wsp").SolutionFile
$solution.SaveAs("C:\MySharePointSolution.wsp")

CSS and Nested Border-Radii

Posted within Development on by Cornflower Design

Now, this is an issue I’ve stumbled across while putting together CSS for websites.

You have two elements, one nested within the other, and both have rounded borders. Unless you get the radius of the nested element just right, you’ll end up with a nasty looking ‘hump’ where there should be a nice smooth curve (See Chris Coyier’s post for a visual example).

Fortunately, Joshua Hibbert has blogged about the problem, documented the math­em­at­ical for­mu­las and even coded a tool for the job. Great!

Hiding SharePoint WebParts via Code

Posted within Development on by Cornflower Design

Another note to myself…

When wanting to programmatically hide a WebPart, don’t try to set it’s Visible property to False. This will cause you lots of trouble and create errrors. The correct method is to set its Hidden property to True.

if (items.Count == 0)
{
   this.Hidden = true;
   return;
}

 

Set Up Groups for this Site

Posted within Development on by Cornflower Design

Note to self: Once a site has been created within SharePoint 2010, the ‘Set Up Groups for this Site’ admin page is hidden away – but you can set access it via /_layouts/permsetup.aspx.

PowerShell Snippets for Microsoft Exchange

Posted within Development on by Cornflower Design

These are two very handy PowerShell snippets that I use again and again within our work environment.

Retreive List of All Group Members

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
csvde -f C:\report.csv -s mydomain.co.uk -r "memberof=CN=Dept:MyTartgetGroup,OU=Domain Groups,DC=mydomain,DC=ac,DC=uk" -l "sAMAccountName,name, mail"

Retreive List of All Users with Permission to Email a Group

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Get-DistributionGroup "Dept:MyTargetGroup" | Select name,@{Name="AcceptMessagesOnlyFrom";Expression={[string]::join(";",($_.AcceptMessagesOnlyFrom | foreach {$_.Name}))}} | Export-Csv C:\report.csv

SharePoint & Item-Level Permissions within Document Libraries

Posted within Development on by Ryan Ball

I have a love/hate relationship with SharePoint. Often the things that should be really simple, just aren’t. Take the ”Item-level Permissions” option for instance. Every list appears to have this option (available via List Settings > Advanced Settings) except for document libraries. I’m sure the SharePoint Development Team at Microsoft have a very good reason for this…

Step forth PowerShell!

$web = Get-SPWeb http://mysharepointsite
$list = $web.Lists[“A Document Library”]
$list.ReadSecurity = 2
$list.WriteSecurity = 2
$list.Update()
$web.Dispose()

The ReadSecurity value can be one of the following:

1 = “Read all items”
2 = “Read items that were created by the user”

The “Create and Edit access” option can also be set by specifiying a value for WriteSecurity.

1 = “Create and edit all items”
2 = “Create items and edit items that were created by the user”
4 = “None”

The Post Slug

Posted within Development on by Cornflower Design

While within The Loop, WordPress provides many functions to quickly access the properties of the Post – but it doesn’t include a built-in function to get the slug for the current Post (although, admittedly, you could simply use $post->post_name).  This functionality can be easily included by adding the following two mini-functions within the functions.php file.

function get_the_slug() {
	global $post;
	return $post->post_name;
}

function the_slug() {
	echo get_the_slug();
}

Update (26 August 2012)

To be absolutely correct, to avoid possible conflicts with other core WordPress or third-party functions, the two sample functions should really be prefixed with a unique identifier, e.g. cornflowerdesign_get_the_post(). I would also recommend wrapping the functions with a function_exists() check, like so.

if ( !function_exists( 'cornflowerdesign_get_the_slug' ) ) {
	function cornflowerdesign_get_the_slug() {
		global $post;
		return $post->post_name;
	}
}

Numeric Position of Post

Posted within Development on by Cornflower Design

Just a quickie… within a WordPress loop you can easily determine the current post position via $wp_query->current_post.

<?php
while ( have_posts() ): the_post();
the_title();
echo $wp_query->current_post;
endwhile;
?>

Selecting Randon Users

Posted within Development on by Cornflower Design

Unlike the get_posts() and get_pages() functions within WordPress, get_users() doesn’t provide a way to return a random selection of users (the results can only be sorted by ‘nicename’, ‘email’, ‘url’, ‘registered’, ‘display_name’, or ‘post_count’). For several projects, usually for sidebar widgets, theres been a need to return a randomly sorted sub-set of users, so I wrote the following custom function:

if ( !function_exists( 'get_random_users' ) ) {
	function get_random_users( $args = array() ) {
		$random_users = array();
		$number = 10;

		$defaults = array( 'number' => $number, 'exclude_administrators' => FALSE );
		$args = wp_parse_args( $args, $defaults );
		$number = $args['number'];

		unset( $args['number'] );

		if ( $args['exclude_administrators'] == TRUE ) {
			$administrators = get_users( array( 'role' => 'administrator' ) );

			if ( $administrators && !is_wp_error( $administrators ) ) {
				$exclude = array();

				foreach( $administrators as $user ) {
					$exclude[] = $user->ID;
				}

				$args['exclude'] = $exclude;
			}

		}

		$users = get_users( $args );

		if ( $users && !is_wp_error( $users ) ) {
			$total_users = count( $users );
			$number = ( $total_users > $number ) ? $number: $total_users;

			if ( $total_users == 1 ) {
				$random_users = $users;
			} elseif ( $total_users > 1 ) {
				shuffle( $users );
				$random_users = array_slice( $users, 0, $number );
			}

		}

		return $random_users;
	}
}

Essentially it works and accepts the same parameters as get_users(), but I have included an option that allows you to exclude Administrators from the returned array. I’m sure the code can be improved – and I probably will do when I get the chance, but for now it does the job.

Alternatively, the following two lines of code, added into the wp-includes/user.php file at line 446, would remove the need for the above function and allow ‘rand’ to be a valid value for the ‘orderby’ parameter.

} elseif ( 'rand' == $qv['orderby'] ) {
	$orderby = 'RAND()';

But I would never condone editing the core WordPress files.