您的位置:首页 > 运维架构 > Shell

PowerShell调用SharePoint web service遍历/修改List(列表)数据

2012-11-13 17:43 405 查看
昨天在检测一段PowerShell代码,调用SharePoint List Web Servicel来查看List items,修改一个List item值时,总是报错,前后检查一个多小时,都没有发现错误原因, 今天早上来一看,才发现,是因为batch file内容的"Cmd='Update'",被我不小心写成了"cmd='Update'", 因为一个字母的大小写,纠结了一个多小时,拿出来跟大家分享一下:

代码:

#test update list item using web service

$uri = 'http://v-qiwei:100/site1/_vti_bin/lists.asmx?wsdl'
$listName = 'List_11' #the list name in the site

# define query object to retrieve list items.
$xmlDoc = new-object System.Xml.XmlDocument
$query = $xmlDoc.CreateElement("Query")
$viewFields = $xmlDoc.CreateElement("ViewFields")
$queryOptions = $xmlDoc.CreateElement("QueryOptions")
$query.set_InnerXml("FieldRef Name='Full Name'")
$rowLimit = "1000"

$list = $null
$service = $null

#create service object
try{
$service = New-WebServiceProxy -Uri $uri  -Namespace SpWs  -UseDefaultCredential
}
catch [Exception] {
return $_.Exception.Message
}

# use the service object to retrieve the list.
if($service -ne $null){
try{
$list = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $queryOptions, "")
}
catch [Exception] {
return $_.Exception.Message
}
}

# check the retrieved list item
($list.data.row).count

# Get Guids of the list the view
$ndlistview = $service.getlistandview($listname, "")
$strlistid = $ndlistview.childnodes.item(0).name
$strviewid = $ndlistview.childnodes.item(1).name

write-host $strlistid
write-host $strviewid

# Create an xmldocument objec
$xmldoctest = new-object system.xml.xmldocument

# create a batch file
$batchelement = $xmldoctest.CreateElement("Batch")
$batchelement.SetAttribute("OnError","Continue")
$batchelement.SetAttribute("ListVersion","1")
$batchelement.SetAttribute("ViewName",$strviewid)

$xml = ""
$xml+="<Method ID='1' Cmd='Update'>"+
"<Field Name='ID'>1</Field>"+
"<Field Name='Title'>TestTitle</Field>"+
"</Method>"

# Set the xml content
$batchelement.innerxml = $xml

write-host $batchelement.innerxml
try{
$service.UpdateListItems($strlistid, $batchelement)
} catch [Exception] {
return $_.Exception.Message
}


如果xml 大小写有问题的话,扑捉异常后,就会有exception:

Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

没有扑捉异常的情况下,exception:

Exception calling "UpdateListItems" with "2" argument(s): "Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown."
At line:72 char:37
+ $ndreturn = $service.UpdateListItems <<<< ($strlistid, $batchelement)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐