0

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?

2 Answers 2

3

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()
2
  • Ah, yes, the brackets. Thank you, this was the solution.
    – vilmarci
    Aug 2, 2017 at 6:57
  • Awesome ! Good to know that it worked . Aug 2, 2017 at 7:02
1

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()
1
  • I added the full code to the question. Also added the Description, but this didn't solve the issue. Also, the other, simple text field remains blank too.
    – vilmarci
    Aug 1, 2017 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.