Powershell Command Using CMAL Query To Fetch Data From List In SharePoint 2013

ISE- Integrated scripting environment

  • A Command pane for running interactive commands.
  • A Script pane for writing, editing, and running scripts. You can run the entire script or selected lines from the script.
  • A scrollable Output pane that displays a transcript of commands from the Command and Script panes and their results.
  • Up to eight independent Windows PowerShell execution environments in the same window, each with its own Command, Script, and Output panes. This tabbed environment allows you to work on several tasks at the same time.
  • Multiline editing in the Command pane lets you paste multiple lines of code, run them, and then recall them as a unit.

Here are the steps,

Step 1: Create SharePoint List using PowerShell.

  1. cls  
  2. Add - PSSnapin Microsoft.SharePoint.PowerShell  
  3. $listName = "Continents"  
  4. $SiteURL = "http://sites1/Demo/"  
  5.   
  6. $spWeb = Get - SPWeb - Identity $SiteURL  
  7. $spTemplate = $spWeb.ListTemplates["Custom List"]  
  8.   
  9. $spListCollection = $spWeb.Lists  
  10. $spListCollection.Add($listName, $listName, $spTemplate)  
  11.   
  12. $path = $spWeb.url.trim()  
  13. $spList = $spWeb.GetList("$path/Lists/$listName")  
  14.  
  15. # adding the field type(Number) to the list  
  16. $spFieldType = [Microsoft.SharePoint.SPFieldType]::Number  
  17. $spList.Fields.Add("Area", $spFieldType, $false)  
  18.  
  19. # adding the field type(Number) to the list  
  20. $spFieldType = [Microsoft.SharePoint.SPFieldType]::Number  
  21. $spList.Fields.Add("Population", $spFieldType, $false)  
  22. $spList.Update()  
In this example we are referencing the following SharePoint List.

continents
Step 2:

PowerShell command to fetch data using CMAL Query which include Equal condition for matching records.

 

  1. Firstly, create Web Object,
    1. $sourceWebURL = "http:// sites1/Demo/"   
    2. $spSourceWeb = Get-SPWeb $sourceWebURL   
  2. Find list from web to process in next operation.
    1. $sourceListName = "Continents"   
    2. $spSourceList = $spSourceWeb.Lists[$sourceListName]   
  3. Create SPQuery object and set query string to it.
    1. $query = new - object Microsoft.SharePoint.SPQuery  
    2.   
    3. $caml = "<Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $filename + "</Value></Contains></Where>"  
    4.   
    5. $query.Query = $caml  
  4. Finally pass that SPQuery object to GetItems() to fetch matching records. 
    1. $spSourceItems = $spSourceList.GetItems($query)  

Final Code,

  1. cls  
  2.   
  3. Add - PSSnapin Microsoft.SharePoint.PowerShell  
  4.   
  5. $sourceWebURL = "http://sites1/Demo/"  
  6. $sourceListName = "Continents"  
  7. $RecordID = "2";  
  8.   
  9. $spSourceWeb = Get - SPWeb $sourceWebURL  
  10. $spSourceList = $spSourceWeb.Lists[$sourceListName]  
  11.   
  12. $caml = "<Where><Eq><FieldRef Name='ID' /><Value Type='Number'>" + $RecordID + "</Value></Eq></Where>"  
  13. $query = new - object Microsoft.SharePoint.SPQuery  
  14. $query.Query = $caml  
  15. $spSourceItems = $spSourceList.GetItems($query)  
  16.  
  17. # $spSourceItems = $spSourceList.GetItemById("1")# $spSourceItems = $spSourceList.Items | where   
  18. {  
  19.     $_['ID'] - ne 0  
  20. }  
  21.   
  22. Write - Host("{0} | {1} | {2} | {3}" - f 'ID''Title''Area''Population')  
  23. Write - Host ""  
  24.   
  25. $spSourceItems | ForEach - Object   
  26. {  
  27.     $data1 = $_['ID']  
  28.     $data2 = $_['Title']  
  29.   
  30.     Write - Host("{0} | {1} | {2} | {3}" - f $_['ID'], $_['Title'], $_['Area'], $_['Population'])  
  31. }  
  32.   
  33. Write - Host ""  
Image Snap

image

Step 3:

PowerShell command to fetch data using CMAL Query that  contains condition for matching records.
  1. cls  
  2.   
  3. Add - PSSnapin Microsoft.SharePoint.PowerShell  
  4.   
  5. $sourceWebURL = "http://sites1/Demo/"  
  6. $sourceListName = "Continents"  
  7. $filename = "DD";  
  8.   
  9. $spSourceWeb = Get - SPWeb $sourceWebURL  
  10. $spSourceList = $spSourceWeb.Lists[$sourceListName]  
  11.   
  12. $caml = "<Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $filename + "</Value></Contains></Where>"  
  13. $query = new - object Microsoft.SharePoint.SPQuery  
  14. $query.Query = $caml  
  15. $spSourceItems = $spSourceList.GetItems($query)  
  16.  
  17. # $spSourceItems = $spSourceList.GetItemById("1")# $spSourceItems = $spSourceList.Items | where   
  18. {  
  19.     $_['ID'] - ne 0  
  20. }  
  21.   
  22. Write - Host("{0} | {1} | {2} | {3}" - f 'ID''Title''Area''Population')  
  23. Write - Host ""  
  24.   
  25. $spSourceItems | ForEach - Object   
  26. {  
  27.     $data1 = $_['ID']  
  28.     $data2 = $_['Title']  
  29.   
  30.     Write - Host("{0} | {1} | {2} | {3}" - f $_['ID'], $_['Title'], $_['Area'], $_['Population'])  
  31. }  
  32.   
  33. Write - Host ""  
Image Snapshot

image

 

Up Next
    Ebook Download
    View all
    Learn
    View all