ISE- Integrated scripting environment

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. $spWeb = Get - SPWeb - Identity $SiteURL
  6. $spTemplate = $spWeb.ListTemplates["Custom List"]
  7. $spListCollection = $spWeb.Lists
  8. $spListCollection.Add($listName, $listName, $spTemplate)
  9. $path = $spWeb.url.trim()
  10. $spList = $spWeb.GetList("$path/Lists/$listName")
  11. # adding the field type(Number) to the list
  12. $spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
  13. $spList.Fields.Add("Area", $spFieldType, $false)
  14. # adding the field type(Number) to the list
  15. $spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
  16. $spList.Fields.Add("Population", $spFieldType, $false)
  17. $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. $caml = "<Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $filename + "</Value></Contains></Where>"
    3. $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. Add - PSSnapin Microsoft.SharePoint.PowerShell
  3. $sourceWebURL = "http://sites1/Demo/"
  4. $sourceListName = "Continents"
  5. $RecordID = "2";
  6. $spSourceWeb = Get - SPWeb $sourceWebURL
  7. $spSourceList = $spSourceWeb.Lists[$sourceListName]
  8. $caml = "<Where><Eq><FieldRef Name='ID' /><Value Type='Number'>" + $RecordID + "</Value></Eq></Where>"
  9. $query = new - object Microsoft.SharePoint.SPQuery
  10. $query.Query = $caml
  11. $spSourceItems = $spSourceList.GetItems($query)
  12. # $spSourceItems = $spSourceList.GetItemById("1")# $spSourceItems = $spSourceList.Items | where
  13. {
  14. $_['ID'] - ne 0
  15. }
  16. Write - Host("{0} | {1} | {2} | {3}" - f 'ID', 'Title', 'Area', 'Population')
  17. Write - Host ""
  18. $spSourceItems | ForEach - Object
  19. {
  20. $data1 = $_['ID']
  21. $data2 = $_['Title']
  22. Write - Host("{0} | {1} | {2} | {3}" - f $_['ID'], $_['Title'], $_['Area'], $_['Population'])
  23. }
  24. Write - Host ""
Image Snap

image

Step 3:

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

image