Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm having issues with this powershell script below. It does work with SP 2007 but won't work with Sp 2010. My goal as you can see is to filter the list returned using the field Date - 1.

But getting the error:

You cannot call a method on a null-valued expression.

At C:\Users\AppData\Local\Temp\2\e70c0e06-6e66-4318-8480-952ccae0460e.ps1:31 char:20 + $col=$list.GetItems <<<< ($query)[0] + CategoryInfo : InvalidOperation: (GetItems:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

function Daily()
{
    [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
    [System.Reflection.Assembly]::LoadWithPartialName("System.web")


    $site    =    new-object Microsoft.SharePoint.SPSite("http://mysites/sites/Intranet/")                                                        
    #$web     =    $site.OpenWeb()                                                                                                
    $web=$site.rootWeb

    $list    =    $web.Lists["Communications"]                                                                              


    #$today = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd")
    $today = (Get-Date).ToString("yyyy-MM-dd")


    $caml = '<Where>
                 <Eq>
                  <FieldRef Name="Date"/>
                  <Value Type="DateTime">
                  {0}
                  </Value>
                 </Eq>
               </Where>
             ' -f $today 


    $query=new-object Microsoft.SharePoint.SPQuery
    $query.Query=$caml
    $col=$list.GetItems($query)[0]

    $Results = @()
    foreach ($r in $col) { 


        $Title = "<b style='font-family:Calibri;'>" + $r.Title + "</b>"
        $Description = "<span style='font-family:Calibri;'>" + $r.Body + "</span>"
        $LinkToitemID = "http://Commus/DispForm.aspx?ID=" + $r.ID
        $scommsLink = "<a href=$LinkToitemID style='font-family:Calibri;'> read </a>"
        $concResult = "" + "<br><br><b style='color:#0054A4;font-size:17px;font-weight:bold'>" + $Title + "</b><br/><br/>" + $Description + "<br>" + $scommsLink + "<br><hr>"

        $Results += $concResult


    }


    $web.Dispose()
    $site.Dispose()  


    write-host $Results 

}
share|improve this question
    
What's that [0] after GetItems($query)? –  Jussi Palo Jan 24 '12 at 6:57
add comment

2 Answers 2

up vote 2 down vote accepted

I've been fiddling around this a bit and found out that the described error occurs when list with the specified title does not exist.

Please, make sure that you have list with title "Communications" created on the http://mysites/sites/Intranet/ site collection.

If the list exist, code works fine in my environment and line $col=$list.GetItems($query)[0] doesn't raise any errors. However, I anticipate that the [0] indexer is unwanted here, because you're getting the first item of the collection to $col variable, which obviously is a logical error.

share|improve this answer
    
Omlin the [0] was an error from my end. You are right and thanks not good working too late.:( I later fond out this morning i was using $web=$site.rootWeb thinking the site was after intranet wheareas the intranet was the root site and then communications was the subsite and then a list called communications.:( One more questions though i have a field called Body and its a rich textbut i noticed it doesn't return any value.The only fields that i get values from is if its a "single line of text" Any ideas? –  naijacoder Jan 24 '12 at 23:40
add comment

Your caml query may also need to be surrounded with

<query> ... <query>

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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