2

I am new to SharePoint 2013 and am not a coder.

I have a List, "IRM Summary", that contains

  • Date Opened,
  • Date Closed
  • Days Open and
  • Today columns.

If there is a

Date Closed date, then Days Open = Date Closed-Days Open.

Else

Days Open = Today-Date Created.

This is working fine except that in SharePoint 2013, as I've found out, Today needs to be updated via a script and nightly scheduled task.

I've found the code below online (and several variations of it) and have adapted the names to our environment.

Start-SPAssignment -Global
$web = Get-SPWeb http://mysite/sites/IRM
$list = $web.Lists[“IRM Summary”]
$list.Items | ForEach-Object {
$_[“Today”] = [System.DateTime]
$_.SystemUpdate($false)
$list.Update()
}
$web.Dispose()
Stop-SPAssignment -Global

However, when I run it in SharePoint 2013 Management Shell, I get the following errors:

Cannot index into a null array. At C:\SPListItemsWithTodaysDate.ps1:26 char:1 + $_["Today"] = [System.DateTime] #Replace TodaysDate with your field name + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray

You cannot call a method on a null-valued expression. At C:\SPListItemsWithTodaysDate.ps1:27 char:1 + $_.SystemUpdate($false) + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression. At C:\SPListItemsWithTodaysDate.ps1:28 char:1 + $list.Update() + ~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

The list itself is populated, and currently there are values in the Today column for every row in the list.

Any help would be appreciated, either with "fixing" the script or using another (non-Javascript) solution or workaround.

2
  • 1
    Might be just copy+paste issue, but FYI several sites format "-symbols with , which are not converted to default quotation marks when ran in command line.
    – moe
    Commented Jan 8, 2016 at 13:01
  • Thanks. Yes, my script was using the second type of quotation marks. Changed to the first type but getting the same errors. Commented Jan 8, 2016 at 15:07

1 Answer 1

2

In this line

$_["Today"] = [System.DateTime]

apart from the vs " quotes (which I believe are OK in your script as per the output), you need to assign a date object e.g. $_["Today"] = [System.DateTime]::Today. The [System.DateTime] expression represents the DateTime class, but it's not an instace of it.

Apart from that, the $list.Update() is only necessary when updating properties in the list. In this case you are updating items only, so no need for it.

4
  • Thanks Fran. Changing the line to $_["Today"] = [System.DateTime]::Today unfortunately produces the same result as before. Commented Jan 8, 2016 at 16:04
  • Please can you post the full code of your script? Commented Jan 8, 2016 at 16:14
  • The full code is as per the code block in my original post, just with the addition of "::Today" as you recommended above. Start-SPAssignment -Global $web = Get-SPWeb mysite/sites/IRM/teamroom $list = $spweb.Lists["IRM Summary"] $list.Items | ForEach-Object { $_["Today"] = [System.DateTime]::Today $_.SystemUpdate($false) $list.Update() } $web.Dispose() Stop-SPAssignment -Global Commented Jan 11, 2016 at 11:59
  • The one you just pasted here is slightly different to the one in the post (different URL, use of $spweb instead of $web). Also, the errors you are getting in powershell reference lines 26-28, the script you provide is shorter. I tested your example locally with a different URL and a different list name and works fine. Are you sure the list name is OK?. You could also add some checks like $list.ItemCount, $list.Items.Count, etc, to verify that the list exists and contains items Commented Jan 11, 2016 at 12:49

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.