Create Random or Demo SharePoint Content with PowerShell

One of the things we need to a lot whilst creating SharePoint solutions or demonstrating its capabilities is to work with example data. However, creating reams of sample data can be quite laborious to say the least!

That’s where PowerShell comes in… We can rapidly create dozens of sample items from a very simple script. Best of all – the scripts can be used to create the document libraries or lists that hold the sample data. With a little bit of effort, the scripts can be used to prepopulate Libraries and Lists throughout an entire site collection.

The script example below is used to create two SharePoint Calendars and populate each with a dozen or so events. As we use this script in demonstrations, we’ve set it up to create the events within a 30 day window of the current date. That way, when the calendar is viewed, you’ll always see some events in the current month and some in the preceding and subsequent months.

Here’s what the script creates, two calendars populated with events:

image

Note the two calendars in the Quick Launch – Team Calendar and My Calendar – this is what we’ll be creating.

The Script

If you’re not familiar with PowerShell a great article on getting started can be found here: http://www.somewhere.com

The script example used in this article can be downloaded here: Create Calendars Script

First of all, the script is started with the Add-PSSnapin Microsoft.SharePoint.PowerShell command. This loads the SharePoint PowerShell cmdlets and allows the script to be executed directly from the PowerShell Editor.

image

Without this the PowerShell editor with throw the following error as it tries to execute SharePoint commands:

The term ‘Get-SPWeb’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Documents\SP2010\Create Calendars.ps1:42 char:21
+ $OpenWeb = Get-SPWeb <<<< $WebUrl
+ CategoryInfo : ObjectNotFound: (Get-SPWeb:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Now we’ve loaded the SharePoint cmdlets, the next thing we do is create a random number object; this will be used latter when we insert items into the list.

image

Next, the script sets up a list of titles to be used for the events, add as many as you want; I’ve added 26 separate items:

image

Finally, having created all our event titles, we connect to SharePoint (line 42) and delete the list defined by the $LibraryName variable if it already exists (line 43). You may not wish to do this in your scripts, but for us in a demonstration environment it’s nice to be able to drop and recreate the required lists simply by executing the same script again. Additionally, by leaving the list delete in place (line 43) you can see the speed that PowerShell runs at but simple re-running the script over and over again. 😉

image

Having deleted any existing ‘Team Calendar’ list we simply recreate it with the following;

image

Line 47 adds the new list to the opened web and uses list template number 106 (calendar list). For a complete list of available template numbers, see: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx. Once added, line 48 opens the list we’ve just added and line 49 ensures the list is displayed on the Quick Launch bar.

Now we’ve successfully added our new list, it’s time to insert some random items. This is done very simply by the following code:

image

Line 53 sets up our loop, and in our example loads the first 13 events only (the other 13 are loaded into a second calendar list).

The title for our event is taken from the list of event titles we defined at the start of the script. The start date for the event (EventDate) is created by taking the current date from the Get-Date function and then adding a random number of days to it (line 57). In this example, we add events anywhere from 30 days prior to 30 days subsequent from the current date. The end date for the event is calculated by adding a random number of minutes to the start date (line 59). Here we are assuming each event we add in anywhere between 30 and 90 minutes in duration. Finally, line 60 saves our new item to the calendar list.

Because our script is setup to create two calendars so we can demonstrate Calendar Overlays, I’ve skipped over lines 62 to 87 as this is just a repeat of what we’ve seen above. The only difference with the second iteration of list operations is that when we run the same commands again we change the $LibraryName to ‘My Calendar’ and there is no need to re-open the web again:

image

Finally, we need to dispose of the SPWeb object we opened earlier (line 42) to release its resources; this is done simply by calling its Dispose method:

image

The complete script is available for download here: Create Calendar Script. Feel free to modify and use it to meet you own requirements and enjoy not having to manually create sample data ever again!

I hope this helps…

Advertisements

4 Responses to Create Random or Demo SharePoint Content with PowerShell

  1. Pingback: » SharePoint 2010 + PowerShell: Export/Import Calendar Items

  2. Charles Ralston says:

    This is really cool. I was wondering, if you had any scripts that actually created documents (word, excel, etc…) as well?

  3. briancartmel says:

    Hi Charles,

    This is what I use: https://sharepintblog.com/2011/10/14/upload-multiple-documents-to-sharepoint-library/

    I’ve got another script for loading tens of thousands of documents into a large document library but this is a bit specialised.

    I hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: