Kashif Nizam Qureshi


Leave a comment

Deleting List Items Through PowerShell Filtered Using CAML Query

Use the following to delete items older than the specified date in CAML below and limiting to 500 all together.

$site = new-object Microsoft.SharePoint.SPSite("<a href="http://dhldepmweb01/PWA">http://dhldepmweb01/PWA</a>")

$web = $site.rootweb

$list = $web.Lists["Project Server Workflow Tasks"]

$caml='<Where>

<Leq>

<FieldRef Name="Created" />

<Value IncludeTimeValue="TRUE" Type="DateTime">2015-12-31T02:32:20Z</Value>

</Leq>

</Where>'

$query=new-object Microsoft.SharePoint.SPQuery

$query.Query=$caml | Write-Output

$items=$list.GetItems($query)

$listItemsTotal = $items.Count;

$count=0

#Limiting the deletion till 500 count

for($x=$listItemsTotal-1;$x -ge 0 -And $count -le 500; $x--)

{

$items[$x].Delete()

$count++

}

$web.Dispose()

$site.Dispose()
Advertisements


Leave a comment

Adding Resource Calendar Exceptions Programmatically

ResourceDataSet rds = _resourceClient.ReadResource(new Guid(resGuid));

// be sure that the resource is checked out !

if (rds.CalendarExceptions != null)
{
for (int i = 0; i &lt; rds.CalendarExceptions.Rows.Count; i++)
{
// check row data...

// delete row if necessary
rds.CalendarExceptions.Rows[i].Delete();
break;
}
// don´t use GetChanges() method on the dataset !
_resourceClient.UpdateResources(resourceDataSet, false, false);
}

// Read resource data again
rds = _resourceClient.ReadResource(new Guid(resGuid));

// create new datarow
ResourceDataSet.CalendarExceptionsRow exceptionsRow = rds.CalendarExceptions.NewCalendarExceptionsRow();

// set necessary data for the new exception row here
//exceptionsRow.Start
//exceptionsRow.Finish
//exceptionsRow.Name
//exceptionsRow.RES_UID
//...

// update the resource again
_resourceClient.UpdateResources(resourceDataSet, false, false);

...
// don´t forget to check in the resource