Field is not updated
-
29-12-2020 - |
题
I try to update a list item, with PowerShell:
try{
$root = GetRootSite #this function returns the site collection url
$site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $root
$customerlist = "Customer-List"
$customers = $site.RootWeb.Lists[$customerlist]
$spqQuery = New-Object Microsoft.SharePoint.SPQuery
$spqQuery.Query = "<OrderBy><FieldRef Name = 'ID' Ascending = 'TRUE'/></OrderBy><Where><Eq><FieldRef Name='CODE' /><Value Type='Text'>TEST</Value></Eq></Where>"
$spSourceItems = $customers.GetItems($spqQuery)
foreach ($i in $spSourceItems) {
$code = $i["CODE"]
write-host $code
[string]$currentlink = $i["LinkToOperations"] -as [Microsoft.SharePoint.SPFieldUrlValue]
$linktooperations = $root + "/Customers/" + $code
if ($currentlink -eq "") {
$field = New-Object Microsoft.SharePoint.SPFieldURLValue
$field.Description = $code
$field.Url = $linktooperations
$i["LinkToOperations"] = $field
$i["CustomerURLText"] = "Updated"
$i.Update
write-host $i["LinkToOperations"]
write-host $i["CustomerURLText"]
}
}
$spSourceItems.Update
$customers.Update
}
catch {
Write-Host "error"
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
exit #exit if cannot load the files above
}
Basically, if the link is empty, I want to add it. My problem is that when the code runs, the correct output is written to the console, but I don't see the results in the list. The $i object is the listitem returned by a query. I have full control on the list, no error is thrown during the process.
The LinkToOperations
is a url field, the CustomerURLText
is text.
I also get the following output upon finish:
MemberType : Method OverloadDefinitions : {System.Void Update(), System.Void Update(bool bFromMigration)} TypeNameOfValue
: System.Management.Automation.PSMethod Value : System.Void Update(), System.Void Update(bool bFromMigration) Name
: Update IsInstance : True
what am i missing?
解决方案
Reading through your code, Update call has been used like a property without brackets. It should be a method call as :
$i["LinkToOperations"] = $field
$i["CustomerURLText"] = "Updated"
$i.Update()
其他提示
A URL field in SharePoint is the URL and the description. You haven't included all of your code, so I can't tell if you're accessing it wrong, but the below example (from here) should help.
Write-Host "Adding New Link"
$field = New-Object Microsoft.SharePoint.SPFieldURLValue
$field.Description = "Microsoft"
$field.URL = "http://www.microsoft.com"
$item = $list.Items.Add()
$item["URL"] = $field
$item["Comments"] = "Added From PowerShell"
$item.Update()