How to Get SP Item filtered from Power Shell

We are going to look how we can retrieve a SharePoint List item by querying a string column.

I should admit I’m really impressed by new Visual Studio Code Power Shell Extension

in below snippet I have described how method will be called to get the output. in second Section you will have all details about the Power Shell Method



$spWeb =Get-SPWeb $spSiteURL;


$spList = $spWeb.Lists["SPListName"];

$spResultItem = Get-SPItem -ItemValue "SharePoint" -spList $spList

you can see implementation of the Get-SPItem Method in below section

function Get-SPItem
{
 #$ItemValue is the value of the column
 #RecordID is the Name of the string column
 param ($ItemValue,$spList) ;
$spqQuery="";
        try
        {
            if ($spList) 
            {       
                $spqQuery = New-Object Microsoft.SharePoint.SPQuery
                $spqQuery.Query = 
                    "   <Where>
                        <Eq>
                            <FieldRef Name='RecordID' />
                            <Value Type='Text'>"+$ItemValue+"</Value>
                        </Eq>
                    </Where>"
                $spqQuery.ViewFields = "<FieldRef Name='RecordID' /><FieldRef Name='Title' />"
                $spqQuery.ViewFieldsOnly = $true;
                $splListItems = $spList.GetItems($spqQuery) ;

                    if(![string]::IsNullOrEmpty($splListItems)) #Check the returned List Items are null
                    {
                        if($splListItems.Count -gt 0)
                        {                   
                            $tempObj = $splListItems[0];       #Get the First Item of the collection            
                            $rtnListItem = $spList.GetItemById($tempObj["ID"]);
                            return $rtnListItem;
                        }
                        else
                        {
                            return $null;
                        }
                    }                
                     else
                    {return $null;
                    }  
            }
            else
            {Write-Host "No Items";
            }
        }
            catch [System.Exception]
            {
                write-host -f red $_.Exception.ToString()
            }
           
}

Leave a Reply

Your email address will not be published. Required fields are marked *