Delete all SharePoint List Items using PowerShell


Sometimes when I’m working on something related to SharePoint, I come across scenarios where I’m constantly building up test data in Lists. Frequently when doing this, I have a need to almost constantly delete and recreate list items.

Rather than using the UI or writing nice PowerShell one-liners (which for the record I love doing), I decided to throw together a little script for this.

This script takes two mandatory parameters, one for URL and one for List Name. Assuming you give it the correct values, it will quickly use Object Model code to call the Delete() method against every item in the list (or library).

Here is the script, hope it works for you! Note: I did not add any kind of confirmation, so if you run this – it WILL literally delete all items, it won’t first make sure you’re serious about it – so make sure you know what you’re doing! 🙂

RD

<#
.Synopsis
	Use this PowerShell Script to delete all list items, very quickly!
.Description
	This advanced script uses SharePoint Cmdlets and the SP Object Model to bulk 
	delete SharePoint list items from a specified SPList.
.Example
	C:\PS>.\Delete-SPListItems.ps1 -Url http://intranet -ListName "Test List"
	This example deletes list items from a list called Test List in the
	http://intranet web.
.Notes
	Name: Delete-SPListItems
	Author: Ryan Dennis
	Last Edit: 8/2/2012
	Keywords: Delete List Items
.Link
	http://www.sharepointryan.com
 	http://twitter.com/SharePointRyan
.Inputs
	None
.Outputs
	None
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][System.String]$Url,
[Parameter(Mandatory=$true)][System.String]$ListName
)
$Web = Get-SPWeb $Url
$List = $Web.lists[$ListName]
if($List -eq $null){
	Write-Error "The List cannot be found";return
}
Write-Warning "Deleting all list items from $($ListName)"
$Items = $List.GetItems()
$Items | ForEach-Object{
$List.GetItemById($_.Id).Delete()
}
$List.Update()
$Web.Dispose()

Remove ‘ReadOnlyField’ lock from columns created in InfoPath using PowerShell


Have you ever created an InfoPath Form Template and published fields from the template to SharePoint Columns in your content type?

I’m sure you have, otherwise you probably wouldn’t be reading this.

Essentially, the columns will be grayed out in the list settings:

And if you try to set the value of the field using SharePoint Designer, you won’t see it in your list of available columns:

However, luckily we have PowerShell – and we can simply edit the ReadOnlyField value from $true to $false – thus allowing us to edit this field. Here’s how!

$web = Get-SPWeb http://weburl
$list = $web.Lists["List Name"]
$field = $list.Fields["Field Name"]
$field.ReadOnlyField = $false
$field.Update()
$list.Update()
$web.Update()
$web.Dispose()

After doing so, the column is not only editable in the browser – we can choose it in Designer:

Get a Web, List and Library Inventory using PowerShell


A colleague of mine recently asked me “Ryan, can you give me some PowerShell code that can give me a list of all sites and sub sites as well as all lists and libraries within each of those sites – for an entire web application?”

“Of course”, I said…

I had some other scripts and functions that were similarly constructed, so I simply took one that was close and adapted it to make it work.

This function, which I’ve called “Get-SPSiteInventory” will run against either an entire Web Application (using the -WebApplication switch param) or a single Site Collection (using the -SiteCollection switch param).

I’ve tested this both by sending the output straight to a file (using the Out-File cmdlet) as well as just running in the shell – both work pretty nicely.

I’ve excluded comment-based help for better readability, and the syntax is as follows…

To run against a site collection:

Get-SPSiteInventory -Url http://spsite -SiteCollection

To run against a web application:

Get-SPSiteInventory -Url http://spwebapp -WebApplication

The entire function:

function Get-SPSiteInventory {
Param(
[string]$Url,
[switch]$SiteCollection,
[switch]$WebApplication
)
Start-SPAssignment -Global
	if ($SiteCollection) {
		$site = Get-SPSite $Url
		$allWebs = $site.allwebs
		foreach ($spweb in $allWebs) {
			" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
			$spweb.Url
			$spweb.Lists | select Title, BaseType
			$spweb.dispose()
		}
		$site.dispose()
	} 
	elseif ($WebApplication) {
		$wa = Get-SPWebApplication $Url
		$allSites = $wa | Get-SPSite -Limit all
		foreach ($spsite in $allSites) {
			$allWebs = $spsite.allwebs
			foreach ($spweb in $allWebs) {
			" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
			$spweb.Url
			$spweb.Lists | select Title, BaseType
			$spweb.dispose()
			}
		}
	}
Stop-SPAssignment -Global
}

Access Denied while creating Publishing Pages


I ran into one of those weird errors that just kind of gets under your skin because you think you’ve checked everything.

You’ve all seen Access Denied errors in SharePoint, but have you run into the one where users WITH proper permissions get it when trying to create a list item – in this case Publishing Pages? Very bizarre.

I checked all of the usual suspects, permission inheritance, draft items in Style Library, Master Page Gallery, etc. Nothing. But then when I reached out to the search engines just for giggles, I found this post by Gunnar Peipman which gave me exactly what I needed.

The permissions on the Master Page Gallery are unique, and users need at least “Restricted Read” to create pages. What’s frustrating is if you go into Site Permissions and view the Uniquely Secured Content, Master Page Gallery DOES NOT show up in that list. You’d think it would show you all uniquely secured content, but that’s not the case.

Thanks Gunnar for the short post with the fix!

1.Go to Site Actions -> Site Settings ->Modify all site settings
2.Go to Galleries -> Master pages and page layouts
3.From the list toolbar, select Settings -> Document library settings
4.Select permissions for this document library
5.Add ‘Restricted Read’ access to the required groups.

Remove SharePoint List Views Using PowerShell


Ever have a need to delete list views using PowerShell?  Of course you do, it’s a great reason to use PowerShell!

Luckily for us, there is great access to the Object Model and we can readily access lists, items, and of course views.

I threw together a nifty little function that accepts three parameters, WebUrl, ListName and ViewName and will remove a single view from a SharePoint list.

The code is quite simple, basically we grab an SPWeb object using the WebUrl parameter – we then grab an SPList using the ListName parameter, and finally we grab an SPListView using the ViewName parameter.  Once we’ve drilled down to the specific SPListView, we can call the Delete() method using the ID of the view.

Here’s my function, enjoy!

function Remove-SPListView {
Param(
[string]$WebUrl,
[string]$ListName,
[string]$ViewName
)
Start-SPAssignment -Global
$SPWeb = Get-SPWeb $WebUrl
$List = $SPWeb.Lists[$ListName]
$View = $List.Views[$ViewName]
$List.Views.Delete($View.ID)
$List.Update()
$SPWeb.Update()
$SPWeb.Dispose()
Stop-SPAssignment -Global
}