Friday, 16 May 2014

Add/Delete list items

Quick script to add some items to a SharePoint list and specify the list items content type, and then delete those items

Clear-Host
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$web = get-spweb "https://collaboration-dev5.ioc-lab.local/Approvals/Apps/Marks/"
$lst = $web.GetList("https://collaboration-dev5.ioc-lab.local/Approvals/Apps/Marks/Lists/TopSponsorItems")

$ct = $lst.ContentTypes["Top Sponsor Item"]

for($i=190; $i -le 00; $i++){

$item = $lst.Items.Add();
$item["ContentTypeId"] = $ct.Id;

$item["Title"] = "Item $i"

$item["CustomID"] = $i;
$item["SectionGroup"] = "Brands"
$item["SponsorSection"]= "Brands"
$item["TopSponsor"]= "Coke"

$item.update();
Write-host "`tCreate item Title: $($item.Title)" -foregroundcolor red
}

#Delete items with 
foreach ($item in $lst.items)
{

    if($item["CustomID"] -gt 190){
    Write-host "`tdelete item id: $($item.id)" -foregroundcolor red
   
    $lst.getitembyid($item.id).Delete()
    }
}



$web.dispose();