1

尝试使用 PowerShell 从 SharePoint 获取列表项。

我使用了此处Windows PowerShell Blog中的示例,并对其进行了修改以与我的站点一起使用。现在我收到以下错误:

Exception calling "GetListItems" with "7" argument(s): "Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown."
At line:30 char:1
+ $list = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $qu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SoapException

我正在使用的脚本:

# The uri refers to the path of the service description, e.g. the .asmx page            
$uri = "http://SITE/sites/DefaultCollection/Engineering/Subsite%20(UK)/_vti_bin/lists.asmx"             

# Create the service            
$service = New-WebServiceProxy -Uri $uri  -Namespace SpWs  -UseDefaultCredential            

# The name of the list             
$listName = "Test1"             

# Create xml query to retrieve list.             
$xmlDoc = new-object System.Xml.XmlDocument            
$query = $xmlDoc.CreateElement("Query")            
$viewFields = $xmlDoc.CreateElement("ViewFields")            
$queryOptions = $xmlDoc.CreateElement("QueryOptions")            
$query.set_InnerXml("FieldRef Name='Text1'")             
$rowLimit = "10"            

$list = $null             
$service = $null              

try
    {            
    $service = New-WebServiceProxy -Uri $uri  -Namespace SpWs  -UseDefaultCredential
    }            
catch
    {             
    Write-Error $_ -ErrorAction:'SilentlyContinue'             
    }

$list = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $queryOptions, "")

if($service -ne $null)
    {            
    try
        {                    
        $list = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $queryOptions, "")
        }            
    catch
        {             
        Write-Error $_ -ErrorAction:'SilentlyContinue'            
        }            
}

以前有人试过吗?我该如何解决这个问题?

4

3 回答 3

3

这是对我有用的解决方案..

将 "?WSDL" 添加到 $uri 字符串的末尾

$uri = "http://SITE/sites/DefaultCollection/Engineering/Subsite%20(UK)/_vti_bin/lists.asmx?WSDL"

根据此链接:要返回使用 ASP.NET 创建的 Web 服务的服务描述,请将“?WSDL”附加到 Web 服务的 URL(例如, http://www.ss64.com/MyWebService .asmx?WSDL )

于 2015-10-06T20:42:45.567 回答
0

它引发错误的行是:

$list = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $queryOptions, "")

该行没有被捕获,但这是一个类似的问题,引发了相同的错误。如果您想尝试获得更好的异常,请将该行包装在另一个 try catch 中:

try{
$list = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $queryOptions, "")
}
catch{
[System.Exception]
write-host ($_.Exception).Message
}
于 2013-04-02T10:55:03.227 回答
-1

请确保将“?WSDL”添加到您的 URI,这就是代码中断的原因。假装它不知道函数的定义(至少我猜)......

比如下面这张。我很惊讶地看到这使我的代码正常工作。诡异的..

$uri = "http://<>/sites/DefaultCollection/Engineering/Subsite%20(UK)/_vti_bin/lists.asmx?WSDL"

于 2014-12-09T01:11:11.157 回答