PowerShell Get a List of Site Templates

If you need to script out a list of available site templates, here is the answer.


Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue
$site = Get-SPSite "http://www.sharepointed.com/tacoSiteCollection"
$web = $site.RootWeb
$listTemplates = $site.GetCustomListTemplates($web)

foreach($xx in $listTemplates)
 {
 Write-Host $xx.FeatureId
 }

$site.Dispose()

PowerShell Calculated Column Create and Edit

How do you create a calculated column using PowerShell?  In my example, I’m creating a new field called Calculated Date Field, setting the formula, then setting the output type to DateTime.

How do I build the formula?  Go into your list, add a new calculated column, create your formula, test the formula.  If it works, copy the formula and delete the column.


$site = Get-SPweb "http://sharepointed.com/site/salsa/"

$mList=$site.Lists["Nacho"]

 $mList.Fields.Add("Calculated Date Field", "Calculated", 0)
 $SPField = $mList.Fields.GetField("Calculated Date Field")
 $SPField.Formula="=DATE(YEAR([Document Date])+7,MONTH([Document Date])+0,DAY([Document Date])+0)"
 $SPField.OutputType="DateTime"
 $SPField.Update()

$site.Dispose()

PowerShell Get SharePoint Page Layouts

I was having an issue with a script and needed to find my available page layouts. Below is the script you will need to run to find the available page layouts for a given site.

$web = Get-SPweb "http://sharepointed.com/sites/taco"

		$pubWeb =[Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
		$pl = $pubWeb.GetAvailablePageLayouts()
		foreach($p in $pl)
		{
			Write-Host $p.Name
		}
$site.Dispose()

PowerShell Get Sub Webs of a Web

If you are looking for a way to get all of the subsites for a given site, PowerShell can help.

Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

$spWeb = Get-SPweb "http://sharepointed.com/sites/taco"

	foreach($subSite in $spWeb.Webs)
		{
			$subSite.Title
			$subSite.Dispose()
		}

$spWeb.Dispose()

1. add the SharePoint Snapin
2. get the site that we want to dig into.
3. loop on each subsite
3.1. display the site title
3.2. close the connection to the subsite.
4. close the connection to the site we dug into.

Get-SPWeb Cannot access the local farm

Get-SPWeb : Cannot access the local farm. Verify that the local farm is properly configured, currently available, an
d that you have the appropriate permissions to access the database before trying again.

I was using one of the farm service accounts to run my PowerShell scripts, not good.
The service account was locked and caused the above error.

One or more field types are not installed properly

Here is the error:
The following exception was thrown when trying to enumerate the collection: “One or more field types are not installed properly. Go to the list settings page to delete these fields.”.
At C:\Users\me\AppData\Local\Temp\2\bca6fde0-a91f-427a-ad21-4794d47cebfc.ps1:24 char:17
+ $spList.GetItems <<<< ($spQuery)
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : ExceptionInGetEnumerator

Why?
Using PowerShell, I was trying to query a list. In my Where statement, I was calling a field by its display name, and not its INTERNAL name.

Bad code:

$web = Get-SPWeb "http://sharepointed.com/sites/taco"

$spList = $web.Lists["Taco History"]
$field = $spList.Fields["Taco Name"]
$value = "Grande Taco"

# Create Query based on field and value
$camlQuery =
'' +
$value +
'';
# SPQuery object
$spQuery = New-Object Microsoft.SharePoint.SPQuery
# Add query
$spQuery.Query = $camlQuery;
$spList.GetItems($spQuery)

This code will toss you the One or more field types are not installed properly….. error.

Using the friendly U2U Caml query tool, I rewrote my PowerShell Caml query. Doing this I noticed my field name issue. My fields display name is Taco Name but its internal name is Taco_x0020_Name. Doh!!!

Rewrite the PowerShell script and life is good!
Notice the new $field = $field.InternalName line in the script.

$web = Get-SPWeb "http://sharepointed.com/sites/taco"

$spList = $web.Lists["Taco History"]
$field = $spList.Fields["Taco Name"]
$field = $field.InternalName
$value = "Grande Taco"

# Create Query based on field and value
$camlQuery =
'' +
$value +
'';
# SPQuery object
$spQuery = New-Object Microsoft.SharePoint.SPQuery
# Add query
$spQuery.Query = $camlQuery;
$spList.GetItems($spQuery)

Workflow Attach Document to an Item

Can you use a SharePoint workflow to attach a document to an item?
YES!!!

What do you need to make this happen?
SharePoint Designer
ILove SharePoint actions from codeplex.

On the server(s) that your web app is running, you will need to modify the PowerShell execution policy.
set-executionpolicy Unrestricted (Use this link for more info)

In SharePoint Designer add the Execute PowerShell Script action.
Click Script and add the PowerShell script below (make the needed changes to reflect your URL)

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$web = Get-SPWeb "http://sharepointed.com/subsite/"
$filePath = "http://sharepointed.com/subsite/docs/taco.docx"

$spList = $web.lists["Your List or Library Name Here"] 
$item = $splist.GetItemById([*Add the workflows Current Item:ID here*])
$file = $web.GetFile($filePath).OpenBinary()

$item.Attachments.Add($filepath, $file)
$item.Update()

$web.Dispose()

$web = the site you are wanting to work with.
$filePath = file path to the document you are wanting to attach to the item that triggered the workflow.
$spList = List that contains the item we are attaching the document to.
$item = $splist.GetItemById() clear out the text between the (), in here you ware wanting to place ID of the item that triggered the workflow. This will tell the PowerShell script what item we are attaching the document to.

PowerShell Add Attachment to Item

Someone asked me how they could automatically add attachments to items. What I did was use a workflow action (ilovesharepoint) to call a powershell script. The script will grab a file from a document library and add it to the list item.

$web = Get-SPWeb "http://taco.com/subsite/"
$filePath = "http://taco.com/subsite/docs/grandetaco.docx"

$spList = $web.lists["Toppings"] 
$item = $splist.GetItemById(2)
$file = $web.GetFile($filePath).OpenBinary()

$item.Attachments.Add($filepath, $file)
$item.Update()

$web.Dispose()

live and learn!
i was having a hard time converting the file to bytes to be able to attach the document to the item.

Delete Distribution Groups Using PowerShell

We had a monster mess where Records Center was enable, and well, it created a TON of groups.
Each of these groups started with Records Center Web Service Submitters ….

Below is the PowerShell to delete the group from a given site. To make things more-funner, it would be better to start at the web app, then loop on the site collections, and sub sites.

$site = Get-SPweb "http://sharepointed.com/sites/TestSite/"

foreach($group in $site.Groups)
	{
		if ($group.Name.Contains("Records Center Web Service Submitters"))
			{
				$site.SiteGroups.Remove($group.Name)
			}
	}
$site.Dispose()

WebFldr link and SharePoint 2010

After migrating one of my Web Apps from SharePoint 2007 to SharePoint 2010, I ran into a small issue. In just about every site in every site collection, the users setup links to the Explorer View. Well, SharePoint 2010 doesn’t much care for the WebFldr.aspx link.

I had two options:
Update every link, in every site one at a time.
Use PowerShell to loop around and update the links for me.

Also noticed that when users clicked on these WebFldr.aspx link, the ribbion would get stuck in a Loading state.

What I’m doing with this script:
Define the items we are looing for and replacing.
Open the site collection.
Loop on each site in the collection.
Get the Quick Launch.
Loop on the Headers.
Loop on the Child Node of each Header.
If the Child Node contains our $stupidLink, we then replace that link with the $goodLink, and update it.
Dispose of our connections.

Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

$stupidLink="WebFldr"
$goodLink="AllItems"

$sSiteColl = Get-SPSite  "http://sharepointed.com/SiteCollection/"
foreach($sSite in $sSiteColl.AllWebs)
	{
	 	$site = Get-SPWeb $sSite.Url
		$qlNav = $site.Navigation.QuickLaunch

		foreach($qHeader in $qlNav)
		{
			foreach($ChldNode in $qHeader.Children)
			{
				if ($ChldNode.Url.Contains($stupidLink)) 
				{
					Write-Host $site.Url -BackgroundColor DarkYellow
					Write-Host $ChldNode.URL -BackgroundColor DarkCyan
					$ChldNode.Url = $ChldNode.Url.Replace($stupidLink,$goodLink)
					$ChldNode.Update()
				}
			}
		}
	}
	$sSite.Dispose()
$sSiteColl.Dispose()

This script is also used to update the Quick Launch children nodes in SharePoint.