3

I have an announcement list, where i need to populate it with some old data we have on emails, documents, etc. so i did the following:-

1.inside our Team site i added a new "Announcement list" . 2.then i wrote the following power-shell, to be able to add announcement items using our data, as follow:-

PS C:\Windows\system32> $web = get-spweb "http://servername/"
PS C:\Windows\system32> $list = $web.lists["News & Announcements"]
PS C:\Windows\system32> $newItem = $list.items.Add();
PS C:\Windows\system32> $newItem["Title"] = "123";
PS C:\Windows\system32> $newItem["Modified"] = "8/30/2015";
PS C:\Windows\system32> $newItem["Created"] = "6/30/2015";
PS C:\Windows\system32> $newItem["Body"] = "123456789 123456789";
PS C:\Windows\system32> $newItem["Editor"] = "test.user";
PS C:\Windows\system32> $newItem["Author"] = "test.user";
PS C:\Windows\system32> $newItem.Update();

but i got this error :-

Exception calling "Update" 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 line:1 char:1 + $newItem.Update(); + ~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SPException

so can anyone advice on this please?

4

Different points to note:

  1. Use $list.AddItem() instead of $list.items.Add(). Much much better for performances.
  2. You cannot set the Modified, Created, Author and Editor fields with a call to Update. You need to call the method UpdateOverwriteVersion instead.
  3. In datetime fields (Modified, Created), you need to set actual DateTime values, e.g. New-Object System.DateTime(2015, 8, 30)
    EDIT: actually, this is true only in pure C# code... for PowerShell, you need to pass a string, see for instance http://sharepointcherie.blogspot.fr/2014/02/powershell-error-when-inserting-dates.html.
  4. In users fields (Author, Editor), you need to set an SPUser object. You can get an SPUser object by using SPWeb.EnsureUser() method, e.g. $user = $web.EnsureUser("domain\LoginName")
PS C:\Windows\system32> $web = Get-SPWeb "http://servername/"  
PS C:\Windows\system32> $list = $web.Lists["News & Announcements"]  
PS C:\Windows\system32> $newItem = $List.AddItem()  
PS C:\Windows\system32> $newItem["Title"] = "123"  
PS C:\Windows\system32> $newItem["Body"] = "123456789 123456789"  
PS C:\Windows\system32> $newItem["Modified"] = "8/30/2015"  
PS C:\Windows\system32> $newItem["Created"] = "6/30/2015"  
PS C:\Windows\system32> $user = $web.EnsureUser("domain\test.user")  
PS C:\Windows\system32> $newItem["Editor"] = $user  
PS C:\Windows\system32> $newItem["Author"] = $user  
PS C:\Windows\system32> $newItem.UpdateOverwriteVersion()  
  • thanks for your help. but i got this error "Specified cast is not valid. At line:1 char:1 + $newItem["Modified"] = New-Object System.DateTime(2015, 8, 30) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], InvalidCastException + FullyQualifiedErrorId : System.InvalidCastException" on the 'Modified' & on the 'Created' fields – john Gu Dec 21 '16 at 13:48
  • Try Get-Date "10/10/2015" instead of New-Object System.DateTime(2015, 8, 30) – Evariste Dec 21 '16 at 14:13
  • You confirm: Modified and Created are the standard SP fields, not custom ones you've created with these display names? – Evariste Dec 21 '16 at 14:15
  • yes Modified & Created are the built-in columns,, those are the display+internal names... now when i try the following "$newItem["Modified"] = Get-Date "10/10/2016"" i will get this error "Specified cast is not valid. At line:1 char:1 + $newItem["Created"] = Get-Date "10/10/2015" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], InvalidCastException + FullyQualifiedErrorId : System.InvalidCastException" – john Gu Dec 21 '16 at 14:51
  • 1
    OK, sorry for the error, was still thinking "C#"... PowerShell actually needs dates as strings, as you initially wrote. I corrected the script. – Evariste Dec 21 '16 at 17:01

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.