Create site with approval workflow using PowerShell Part 2

This is the fourth Cross Blog post that I’ve made with Mattias and they are getting better and better.

Now, we are heading onto MOSS and the SiteDirectory. Scripting the SiteDirectory is basically the same as scripting a list with items. The fun part is checking the “Approved” value set by an administrator or by someone with the right credentials. The Approved Status can be set to: Approved, Rejected or Pending (default). In PowerShell, the values will be 0, 1, 2 (default). so if we want to check for items that are approved, we check for items that equal 0.

check-sitedirectory01

I’ve included a couple of if else statements that check if the Site is Approved, if the New Site already exists, If the Parent Site Url is valid and so on. An example is if you approve a site that has the following Url:

http://Sharepoint/SiteThatDoesnExist/SiteToCreate

The script won’t create the Site since it’s parent doesn’t exist and will instead prompt you with a recommendation to either create the Parent Site or change the item value.

Script Parameters:

  • -Url Url to SharePoint (Required)
  • -Web Relative Url to SiteDirectory (Required)
  • -List List containing information regarding Sites (Required)
  • -Template Template to use when creating New Sites (Required)
  • -help Prints the HelpFile (Optional)

If you are unsure of whitch Template to use, you can always run the following function to find out which templates are available on your SharePoint Server:


function Get-Templates([string]`$Url) {

	$SPSite = New-Object Microsoft.SharePoint.SPSite($Url)
	$SPSite.GetWebTemplates(1033) | Select Name, Description
	$SPSite.Dispose()
}

PS > Get-Templates http://SharePoint

Now with the basic script procedure in place, we can start Approving sites and scripting them into SharePoint. Start by Approving a Site.

check-sitedirectory03

Now if we check out the Sites list, the Item will be Approved.

check-sitedirectory04

Next, start up PowerShell and trigger the script. Change the Arguments to fit your environment. Run the script.

check-sitedirectory05

And if you check the Url specified in the Item thats Approved, the New Site will have been created.

check-sitedirectory06

Here’s an example on running the script:


PS > .\Check-SiteDirectory.ps1 -Url http://makanigo -Web SiteDirectory -List Sites -Template STS#0

PS >  .\Check-SiteDirectory.ps1 -help

Click here to Download the Script

Rating 3.00 out of 5
[?]

Set-Theme.ps1, Set Theme in Sharepoint using PowerShell

I wrote a blog post about this a couple of months ago, so here’s an updated script version that let’s you change theme on your SharePoint sites. It’s pretty straight forward and it includes a help function that explains how to use the Script.

Let’s say you have a site http://wss/Teams/TeamWiki. You get a request from your collegues to change the theme to BELLTOWN, since they like it more than the Default SharePoint theme. as an admin, you can either start Internet Explorer, browse to the site in question and Change the Theme through Site Settings or you can fancy off with a PowerShell script that does the work for you.

It might seem a little overkill to start up PowerShell and change the theme on a site when it’s almost as easy as browsing to the site and changing the theme manually.. But, what if you have 10 different TeamWikis on various locations and you have to change all of them?

Here’s an example on running the script on a site. Below is a Screenshot of the site before the script is run.

sharepoint-20

Running the following command:


PS > Set-Theme.ps1 -Url http://wss -Web Teams/TeamWiki -Theme BELLTOWN

Changes the Theme to this:

sharepoint-21

If you want to display the helpfile, simply type:


PS > Set-Theme.ps1 -help

Click here to download the Script.

Rating 3.00 out of 5
[?]

Export a list with all the sites in a site collection part 2

This is a follow-up on Part 1 by Mattias Karlssons

As Mattias mentioned, generating a list with Site information is a feature that many people ask for. It’s possible to get the information from the Site Hierarchy or the Site Content and Structure, as Mattias shows in his post

I’m going to show you how to retrieve the information through PowerShell and also provide a script that loops through each site and automates this.

First off, PowerShell can retrieve a great amount of information about Sites. If you run a simple command such as:


PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://yoursite"); $SPSite.AllWebs; $SPSite.Dispose()

You will get tons of information from every Site in your collection. The scipt that I’ve made for this post gathers the following information from each site:

  • Name
  • Title
  • Description
  • Theme
  • WebTemplate
  • Author
  • Created
  • Modified
  • Sites
  • Users
  • ParentWeb
  • Url
  • ServerRelativeUrl
  • ID

I’ve also added a switch -ToCsv, that lets you write the information to a Csv file instead of writing it to the PS Host.

Running The Script:


PS > ./Get-SiteInformation.ps1 -Url http://wss

PS > ./Get-SiteInformation.ps1 -Url http://wss -ToCsv

PS > ./Get-SiteInformation.ps1 -help

Download the Script Here

Rating 2.75 out of 5
[?]

Remove Sharepoint Users Programmatically Part 2

Mattias Described the Scenario in part 1 of this post

Following up on his blog post, I’m going to descrbibe how to script this through SharePoint.

Starting off, we connect to the Site that we believe the User Object is in. Then we create a simple If Else statement that checks if the User Exists, and If so, delete the User.

Here’s the function used in the script.


function RemoveUser([string]$Site, [string]$SiteCollection, [string]$User) {

	# GAC

	[System.Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
	[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null

	# Connect To Sharepoint

	$SPSite = New-Object Microsoft.SharePoint.SPSite($Site)
	$OpenWeb = $SPSite.OpenWeb($SiteCollection)

	# Check if User Exists in Site

	if ($OpenWeb.SiteUsers | Where {$_.LoginName -eq $User}) {
		$User = $OpenWeb.SiteUsers | Where {$_.LoginName -eq $User}
		$OpenWeb.SiteUsers.Remove($User)

		Write-Host "User: $User Successfully Removed from Site: $Site/$SiteCollection"
	} else {
	Write-Host "User: $User Does not exist on Site: $Site/$SiteCollection"
	}
	$SPSite.Dispose()
	$OpenWeb.Dispose()
}

Here’s a script that automates these steps.

Usage:


PS > Remove-SPUser.ps1 -Site http://wss -SiteCollection Test -User Domain\User

PS > Remove-SPUser.ps1 -help


Rating 3.50 out of 5
[?]

Batch Creation of Sharepoint Sites Part 2

This is the second part of a Post started by Mattias Karlsson. Click Here to See Part One

Following on Mattias lead, I’m going to describe how to build a script that automates
the creation of 700+ Sites in sharepoint.

The scenario is well described by Mattias so I’m just going to dig directly into the code.

The first thing we want to consider is the Csv file. Since we work in a Swedish / Finnish Company, there are alot of funny looking characters and non-friendly names that could cause a problem in sharepoint. So step one is getting rid of all funnies. This is a perfect job for the -replace operator.

sharepoint-19

Importing the csv File to PowerShell is done through the Import-Csv CmdLet, as soon as we have imported the Csv file, we can edit each line in it.

There are basically 2 steps that we want to do in Sharepoint, Adding information to a Directory List and creating Sites. The Directory list can be treated in the same way as we altered Custom Lists in this post.

The only “new” item that we want to add now is the URL, which takes 2 arguments, URL and Description.

The Connections to Sharepoint are done in the same way as in previous examples, just make sure to change which list you want to connect to and where your SiteDirectory is.

Creating Sites is described in this post. What’s new in this example is the Template that we want to use. In order to tell SharePoint that we want to build our sites based on a template file, we have to add the template as an argument to the Add() method.

Now that we have gone through all the techniques set and ready, let’s put it all togheter in a ps1 script. The script consists of 3 functions, one Help function that describes how to use the ps1 script, one Csv function that imports the csv file and prepares it and finally a function that adds all data to sharepoint.

Click here to download the Script.

Click here to download a template Csv File

Rating 3.00 out of 5
[?]