在共享点列表中,我添加了“标题”,“作者”和公司。然后,我尝试将csv文件导入到共享点列表中,并且遇到以下错误:
Exception calling "ExecuteQuery" with "0" argument(s): "Invalid data has been used to update the list item. The field you are trying to update may
be read only."
At C:\Users\xxxx\Documents\UploadCSV.ps1:36 char:5
+ $Context.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : XmlException
这是我的代码段:
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"
#Variables for Processing
$SiteUrl = "https://tenant.com/sites/TeamSite/"
$ListName="Contacts"
$ImportFile ="c:\Scripts\test.csv"
$UserName="xxx@tenant.com"
$Password ="password"
#Setup Credentials to connect
$Credential = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credential
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
#Get the Data from CSV and Add to SharePoint List
$data = Import-Csv $ImportFile
Foreach ($row in $data) {
#add item to List
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$Item = $List.AddItem($ListItemInfo)
$Item["Author"] = $row.Author
$Item["Company"] = $row.Company
$Item.Update()
$Context.ExecuteQuery()
}
答案 0 :(得分:0)
Author是一个系统人员字段,如果在CSV文件中,Author是用户名,例如“ user@tenant.onmicrosoft.com”,则无法直接设置。 像这样修改Foreach循环:
$data = Import-Csv $ImportFile
Foreach ($row in $data) {
#add item to List
$ListItemInfo = New-Object
Microsoft.SharePoint.Client.ListItemCreationInformation
$Item = $List.AddItem($ListItemInfo)
$User = $Context.Web.EnsureUser($row.Author)
$Context.Load($user)
$Item["Title"]="New Item Added"
$Item["Author"] = $user
$Item["Company"] = $row.Company
$Item.Update()
$Context.ExecuteQuery()
}