3

Over View:

  I’m trying to automate updates to a SharePoint list using PowerShell via the Web service object.   The end goal is to remove items all from the list, then import new data via a csv file.

 

Task 1, get items from a SharePoint list - done

Task 2 update/Add items in a share point List - pending < Need help with this part, I'm receiving an error.... See Below! >

Task 3, Remove all items in the list. - Pending

Not Started

Task 1

Powershell Code:

$listName = "DemoList"
$uri = "http://companyweb/_vti_bin/lists.asmx"
$xmlDoc = new-object System.Xml.XmlDocument
$query = $xmlDoc.CreateElement("Query")
$viewFields = $xmlDoc.CreateElement("ViewFields")
$queryOptions = $xmlDoc.CreateElement("QueryOptions")
$rowLimit = "0"
$service = New-WebServiceProxy -UseDefaultCredential -uri $uri
$list = $service.GetListItems($listName, "" , $query, $viewFields, $rowLimit, $queryOptions, "")
### testing - confirm row count 
$list.data.row.count
##### Select all data
$list.data.row | Select *  | Format-table

Task 2 Source Example

Powershell Code:
# Get name attribute values (guids) for list and view            
$ndlistview = $service.getlistandview($listname, "")            
$strlistid = $ndlistview.childnodes.item(0).name            
$strviewid = $ndlistview.childnodes.item(1).name            

# Create an xmldocument object and construct a batch element and its     attributes.             
$xmldoc = new-object system.xml.xmldocument             

# note that an empty viewname parameter causes the method to use the default view               
$batchelement = $xmldoc.createelement("batch")            
$batchelement.setattribute("onerror", "continue")            
$batchelement.setattribute("listversion", "1")            
$batchelement.setattribute("viewname", $strviewid)              
# Specify methods for the batch post using caml. to update or delete, specify the id of the     #item, and to update or add, specify the value to place in the specified     column            
$id = 1            
$xml = ""            
# The row to be modified            
$rowId = $list.data.row.ows_id            
# New field value            
$newsport ="soccer"               
$xml += "<method id='$id' cmd='Update'>" +            
        "<field name='ID'>$rowId</field>" +            
        "<field name='Sport'>$newsport</field>" +            
        "</method>"                  
# Set the xml content                    
$batchelement.innerxml = $xml            
$ndreturn = $null             
try {            
    $ndreturn = $service.updatelistitems($listname, $batchelement)             
}            
catch {             
    write-error $_ -erroraction:'stop'            
}

Error : Update failed

Exception calling "UpdateListItems" with "2" argument(s): "Exception of type Microsoft.SharePoint.SoapServer.SoapServerException' was thrown." At line:98 char:41

Task 3 Pending

1 Answer 1

3

Task 2

Replace this line

$batchelement = $xmldoc.createelement("batch")  

by this

$batchelement = $xmldoc.createelement("Batch") 

Problem is your passing wrong parameter to method in return its not taking it in.

Task 3

try
{ 

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "1"); 

string strBatch = "<Method ID='1' Cmd='Delete'>" +
"<Field Name='ID'>" + "4" + "</Field></Method>";

elBatch.InnerXml = strBatch;
ListReference.UpdateListItems("List Name", elBatch); 


}
catch (Exception ex)
{
Response.Write(ex.Message);

}
}

all you have to do is create a different batch statement and pass it to updatelistitem method, for more explanation check this link out

Hope it helps :)

2
  • Yaron, I can't believe i missed the (B) This helps out Big Time! Thanks you!!!
    – Mike
    Commented Oct 4, 2012 at 9:06
  • 1
    @SharePointer Unfortunately Mike hasn't been seen since 2012, so I upvoted your helpful answer.
    – Benny Skogberg
    Commented Jan 10, 2014 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.