Script that deletes items from Sharepoint

R

RemyMaza

Here is the script that I'm trying to use:


##
=====================================================================
## Title : Remove-SPItem
## Description : Removes An Item From a List
## Author : Idera
## Date : 24/11/2009
## Input : Remove-SPItem [[-url] <String>] [[-List] <String>] [[-
Field] <String>] [[-Item] <String>]
## Output :
## Usage : Remove-SPItem -url http://moss -List Users -Field
Title -Item "My Item"
## Notes : Adapted From Niklas Goude Script
## Tag : Item, Sharepoint, Powershell
## Change log :
##
=====================================================================

param (

[string]$url = "$(Read-Host 'url [e.g. http://moss]')",
[string]$List = "$(Read-Host 'List Name [e.g. My List]')",
[string]$Field = "$(Read-Host 'Field To match Item With [e.g.
Title]')",
[string]$Item = "$(Read-Host 'Item Name [e.g. First Item]')"
)

function main() {

[void]
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

Remove-SPItem -url $url -List $List -Item $Item -Field $Field
}


function Get-SPSite([string]$url) {

New-Object Microsoft.SharePoint.SPSite($url)
}

function Get-SPWeb([string]$url) {

$SPSite = Get-SPSite $url
$SPSite.OpenWeb()
}

function Remove-SPItem([string]$url, [string]$List, [string]$Item,
[string]$Field) {

$OpenWeb = Get-SPWeb $url
$OpenList = $OpenWeb.Lists[$List]

if($OpenList.Items | where { $_[$Field] -eq $Item }) {
$OpenItem = $OpenList.Items | where { $_[$Field] -eq $Item }
$OpenItem.delete()
} else {
Write-Host "Item $Item Not Found"
}
}

main


The problem comes when $OpenItem.delete() is ran. It sends back this
error:

Method invocation failed because [System.Object[]] doesn't contain a
method named 'delete'.
At C:\!Scripts\Sharepoint - DeleteItems.ps1:48 char:19
+ $OpenItem.delete <<<< () | Get-Member
+ CategoryInfo : InvalidOperation: (delete:String) [],
RuntimeException
+ FullyQualifiedErrorId : MethodNotFound


So to put that to rest, I instead ran $OpenItem | Get-Member instead
of .delete(). I do in fact get an operation that I can use named
delete. Here are the operations that are available to me:

Name MemberType
Definition
---- ----------
----------
BreakRoleInheritance Method System.Void
BreakRoleInheritance(bool
CopyRoleAssignments)
CheckPermissions Method System.Void
CheckPermissions(Microsoft.SharePoint.SPBasePermissions
permissionMask)
CopyFrom Method System.Void
CopyFrom(string sourceUrl), System.Void CopyFrom(string sourceUrl,
hashtable properties, byte[] stream)
CopyTo Method System.Void
CopyTo(string
destinationUrl)
Delete Method System.Void
Delete()
DoesUserHavePermissions Method bool
DoesUserHavePermissions(Microsoft.SharePoint.SPBasePermissions
permissionMask), bool
DoesUserHavePermissions(Microsoft.SharePoint.SPUser ...
EnsureWorkflowInformation Method System.Void
EnsureWorkflowInformation(), System.Void
EnsureWorkflowInformation(bool retrieveAssociations, bool
retrieveWorkflows)
Equals Method bool
Equals(System.Object
obj)
GetFormattedValue Method string
GetFormattedValue(string
fieldName)
GetHashCode Method int
GetHashCode()
GetType Method type
GetType()
Recycle Method System.Guid
Recycle()
ReplaceLink Method System.Void
ReplaceLink(string oldUrl, string
newUrl)
ResetRoleInheritance Method System.Void
ResetRoleInheritance()
SystemUpdate Method System.Void
SystemUpdate(), System.Void SystemUpdate(bool
incrementListItemVersion)
ToString Method string
ToString()
UnlinkFromCopySource Method System.Void
UnlinkFromCopySource()
Update Method System.Void
Update()
UpdateOverwriteVersion Method System.Void
UpdateOverwriteVersion()
Item ParameterizedProperty System.Object Item(int
index) {get;set;}, System.Object Item(string fieldName) {get;set;},
System.Object Item(System.Guid fieldId) {get;set;}
AllRolesForCurrentUser Property
Microsoft.SharePoint.SPRoleDefinitionBindingCollection
AllRolesForCurrentUser
{get;}
Attachments Property
Microsoft.SharePoint.SPAttachmentCollection Attachments
{get;}
Audit Property
Microsoft.SharePoint.SPAudit Audit
{get;}
BackwardLinks Property
Microsoft.SharePoint.SPLinkCollection BackwardLinks
{get;}
ContentType Property
Microsoft.SharePoint.SPContentType ContentType
{get;}
CopyDestinations Property
Microsoft.SharePoint.SPCopyDestinationCollection CopyDestinations
{get;}
CopyFieldMask Property
Microsoft.SharePoint.SPCopyFieldMask CopyFieldMask
{get;}
CopySource Property System.String
CopySource
{get;}
DisplayName Property System.String
DisplayName
{get;}
EffectiveBasePermissions Property
Microsoft.SharePoint.SPBasePermissions EffectiveBasePermissions
{get;}
Fields Property
Microsoft.SharePoint.SPFieldCollection Fields
{get;set;}
File Property
Microsoft.SharePoint.SPFile File
{get;}
FileSystemObjectType Property
Microsoft.SharePoint.SPFileSystemObjectType FileSystemObjectType
{get;set;}
FirstUniqueAncestor Property
Microsoft.SharePoint.ISecurableObject FirstUniqueAncestor
{get;}
Folder Property
Microsoft.SharePoint.SPFolder Folder
{get;}
ForwardLinks Property
Microsoft.SharePoint.SPLinkCollection ForwardLinks
{get;}
HasPublishedVersion Property System.Boolean
HasPublishedVersion
{get;}
HasUniqueRoleAssignments Property System.Boolean
HasUniqueRoleAssignments
{get;}
ID Property System.Int32 ID
{get;}
Level Property
Microsoft.SharePoint.SPFileLevel Level
{get;}
ListItems Property
Microsoft.SharePoint.SPListItemCollection ListItems
{get;}
MissingRequiredFields Property System.Boolean
MissingRequiredFields
{get;}
ModerationInformation Property
Microsoft.SharePoint.SPModerationInformation ModerationInformation
{get;}
Name Property System.String Name
{get;}
ParentList Property
Microsoft.SharePoint.SPList ParentList
{get;}
Properties Property
System.Collections.Hashtable Properties
{get;}
RecurrenceID Property System.String
RecurrenceID
{get;}
ReusableAcl Property
Microsoft.SharePoint.SPReusableAcl ReusableAcl
{get;}
RoleAssignments Property
Microsoft.SharePoint.SPRoleAssignmentCollection RoleAssignments
{get;}
ServerRedirected Property System.Boolean
ServerRedirected
{get;}
Tasks Property
Microsoft.SharePoint.Workflow.SPWorkflowTaskCollection Tasks
{get;}
Title Property System.String Title
{get;}
UniqueId Property System.Guid UniqueId
{get;}
Url Property System.String Url
{get;}
Versions Property
Microsoft.SharePoint.SPListItemVersionCollection Versions
{get;}
Web Property
Microsoft.SharePoint.SPWeb Web
{get;}
Workflows Property
Microsoft.SharePoint.Workflow.SPWorkflowCollection Workflows
{get;}
Xml Property System.String Xml
{get;}


Listed in that jumbled mess is a delete. Why do I get an error saying
the method doesn't exist?

Cheers,
Matt B.
 

My Computer

W

Wolfgang Kais

Hello Matt.

"RemyMaza" wrote:

> Here is the script that I'm trying to use:
[snip]

> function Remove-SPItem([string]$url, [string]$List, [string]$Item,
> [string]$Field) {
>
> $OpenWeb = Get-SPWeb $url
> $OpenList = $OpenWeb.Lists[$List]
>
> if($OpenList.Items | where { $_[$Field] -eq $Item }) {
> $OpenItem = $OpenList.Items | where { $_[$Field] -eq $Item }
> $OpenItem.delete()
> } else {
> Write-Host "Item $Item Not Found"
> }
> }
>
> The problem comes when $OpenItem.delete() is ran. It sends back this
> error:
>
> Method invocation failed because [System.Object[]] doesn't contain a
> method named 'delete'.
> At C:\!Scripts\Sharepoint - DeleteItems.ps1:48 char:19
> + $OpenItem.delete <<<< () | Get-Member
> + CategoryInfo : InvalidOperation: (delete:String) [],
> RuntimeException
> + FullyQualifiedErrorId : MethodNotFound
Reading that message carefully, it tells us that $OpenItem is an array.

> So to put that to rest, I instead ran $OpenItem | Get-Member instead
> of .delete(). I do in fact get an operation that I can use named
> delete. Here are the operations that are available to me:
[snip]

Get-Member lists the members of the objects in the pipeline, in this
case, all objects are of the same type and have the same members.
To see that there are multiple objects, try this:

$OpenItem | Measure-Object

To solve the problem and to delete all items found, use this instead:

$OpenItem | ForEach-Object { $_.Delete() }

--
Regards,
Wolfgang
 

My Computer

R

RemyMaza

On May 24, 3:12 pm, "Wolfgang Kais" <[email protected]> wrote:

> Hello Matt.
>
>
>
>
>
>
>
> "RemyMaza" wrote:

> > Here is the script that I'm trying to use:
> [snip]

> > function Remove-SPItem([string]$url, [string]$List, [string]$Item,
> > [string]$Field) {
>

> > $OpenWeb = Get-SPWeb $url
> > $OpenList = $OpenWeb.Lists[$List]
>

> > if($OpenList.Items | where { $_[$Field] -eq $Item }) {
> > $OpenItem = $OpenList.Items | where { $_[$Field] -eq $Item }
> > $OpenItem.delete()
> > } else {
> > Write-Host "Item $Item Not Found"
> > }
> > }
>

> > The problem comes when $OpenItem.delete() is ran.  It sends back this
> > error:
>

> > Method invocation failed because [System.Object[]] doesn't contain a
> > method named 'delete'.
> > At C:\!Scripts\Sharepoint - DeleteItems.ps1:48 char:19
> > +         $OpenItem.delete <<<< () | Get-Member
> >    + CategoryInfo          : InvalidOperation: (delete:String) [],
> > RuntimeException
> >    + FullyQualifiedErrorId : MethodNotFound
>
> Reading that message carefully, it tells us that $OpenItem is an array.
>

> > So to put that to rest, I instead ran $OpenItem | Get-Member instead
> > of .delete().  I do in fact get an operation that I can use named
> > delete.  Here are the operations that are available to me:
>
> [snip]
>
> Get-Member lists the members of the objects in the pipeline, in this
> case, all objects are of the same type and have the same members.
> To see that there are multiple objects, try this:
>
> $OpenItem | Measure-Object
>
> To solve the problem and to delete all items found, use this instead:
>
> $OpenItem | ForEach-Object { $_.Delete() }
>
> --
> Regards,
> Wolfgang
Wolfgang! YOU ROCK! This code tweak works like a charm. Thanks for
the Measure-Object tip. I'll have to remember that one for future
usage.

Cheers,
Matt
 

My Computer

Top