2

I have my webapp, my webs and a list created. My point is that I would like to populate my lists with values at the time of my deployment, which is done thanks to a powershell script.

So I would like to write a generic function to add items to a list, in a particular folder at deployment time. This function seems to work, but when it comes to add a link in the list, I have an error.

I have a list "Useful Links", which template is Links. In the script, where $list contains my list, and where $folder is my root folder, I wrote this :

$newItem = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue
$urlValue.Url = "http://MyUrl.com"
$urlValue.Description = "This is an awesome description"
$newItem["Url"] = $urlValue

And then I get this error :

Unable to index into an object of type Microsoft.SharePoint.SPListItem.
At line:1 char:11
+ $newItem[ <<<< "Url"] = $urlValue
+ CategoryInfo          : InvalidOperation: (Url:String) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex

I tried with several cases for Url ("URL", "url", "Url"), and different types of value (SPFieldURLValue, string). But nothing works.

Besides, I noticed that when I populate the list from the website interface, when I try to examine items from sharepoint manager, or from powershell, this "Url" attribute is not set, so I wonder where and how to persist a Url in Sharepoint ...

EDIT -------

There were two issues :

  • You must set the "URL" attribute ($item["URL"] = $urlValue)
  • The $urlValue.Url must ABSOLUTELY be formatted as a URL (start with http://), otherwise you get the same error, and that's what lead me here in the first place.
1

Try this:

$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue("")
$urlValue.Description = "This is an awesome description"
$urlValue.Url =  "http://MyUrl.com"
$item["URL"] = $urlValue
$item.update()

Reference: http://sharepointnadeem.blogspot.com/2011/10/add-item-to-sharepoint-links-list.html

  • I already tried that, but without updating. These was the missing piece. Thanks a lot! – Thibault Apr 29 '14 at 7:21
1

Try this. It seems that the expected value is a comma separated string.

$url = "http://url, description";
$newItem["Url"] = $url
  • I 've seen that piece of information somewhere, but it seems to me that I've already tried that one. I'll give it a try as soon as I can. Anyway, thank you for the tip! – Thibault Apr 28 '14 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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