Write to SPList remotely with PowerShell SOAP
Have you needed to write to a SPList on a remote target farm? Get-SPSite works great locally, but remote scenarios need different techniques. The below PowerShell will write to a SharePoint list remotely over HTTP using the SOAP endpoint “Lists.asmx” by sending XML with the action detail (insert command and field values).
PowerShell
Function WriteCAML($targetUrl, $targetListName, $data) {
# Open remote service
$uri = $targetUrl + "/_vti_bin/lists.asmx?WSDL"
$service = New-WebServiceProxy -Uri $uri -Namespace SpWs -UseDefaultCredential
# Get list and view ID
$detail = $service.GetListAndView($targetListName, "")
$listid = $detail.ChildNodes.Item(0).Name
$viewid = $detail.ChildNodes.Item(1).Name
# Make batch with attributes
$doc = New-Object System.Xml.XmlDocument
# NOTE - Empty viewname will use the default view
$batch = $doc.CreateElement("Batch")
$batch.SetAttribute("onerror", "continue")
$batch.SetAttribute("listversion", "1")
$batch.SetAttribute("viewname", $viewid)
$batch.InnerXml += "" +
"$title "+
"$($data.sites) "+
"$($data.webs) "+
"$($data.dbs) "+
" "
# Call remote service
return $service.UpdateListItems($listName, $batch)
}
# Main
WriteCAML "http://sharepoint/sites/metrics" "DailyStats" $data
