你不能在sharepoint在线管理shell中调用空值表达式的方法

时间:2017-03-16 12:43:55

标签: powershell sharepoint sharepoint-online

当我们在SharePoint Online Management Shell中执行此脚本时,我们会收到错误信息,如图...

  

“你不能在空值表达式上调用方法”

enter image description here

1 个答案:

答案 0 :(得分:0)

您正在使用不正确的PowerShell插件来处理您尝试执行的操作。 SPOSite用于租户管理功能,不会打开SharePoint对象模型(如Fields.AddFieldAsXml)。您需要使用SharePoint Client SDK,而不是download here

然后你的powershell将会是这样的:

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #loads sharepoint client runtime
$destination = "http://my.sharepoint.com/url"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($destination)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) #You can also pass a credential here if you want that dialog prompt instead of $username and $password
$myNewField = $context.Web.Fields.AddFieldAsXml($fieldXML)
$context.Load($myNewField)
$context.ExecuteQuery()