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.