Category Archives: CAML Query

Get items from SharePoint List using PowerShell script

For long time I wanted to dive a bit deeper into PowerShell. Hopefully I will be able to post few of the sharepoint powershell tricks here.
First one is about getting list items from a sharepoint list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebURL = "http://sharepointsite"
$sourceListName = "mylist"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
#$spSourceItems = $spSourceList.GetItems()
#$spSourceItems = $spSourceList.GetItemById("1")
$spSourceItems = $spSourceList.Items | where {$_['ID'] -eq 1}

$spSourceItems | ForEach-Object {
    Write-Host $_['ID']
    Write-Host $_['Title']
}

You can either get all the items with GetItems() method currently commented out or you can filter items by some parameters. Filter applied in the script above is also equal to the GetItemById(“1”) method.

SharePoint Webservice SOAP Call for “recently changed items”

I just read a tweet question: @robertkuzma do you know syntax for web service call to get “recently changed items” from share point?

The important thing here is to properly create the XML you are posting. The article Writing CAML Queries For Retrieving List Items from a SharePoint List might be usefull to construct the SOAP call needed.

Check out the code below – will get you the items modified in last 3 days:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
            <listName>yourListNameHere</listName>
            <query>
                <Query>
                    <Where>
                        <Geq>
                            <FieldRef Name="Modified" />
                            <Value Type="DateTime"><Today OffsetDays="-3" /></Value>
                        </Geq>
                    </Where>
                </Query>
            </query>
        </GetListItems>
    </soap:Body>
</soap:Envelope>

Don’t forget to add SOAPAction = “http://schemas.microsoft.com/sharepoint/soap/GetListItems” to the header of your POST SOAP call.