SharePoint Online: PowerShell'de Arama Alanı için CAML Sorgusu
Gereksinim: PowerShell kullanarak SharePoint Online'da Arama alanı için CAML Sorgusu.
SharePoint Arama Alanı Değeri için CAML:
SharePoint'teki arama alanları, kullanıcıların başka bir listeden bir değer seçmesine olanak tanır ve bu değer daha sonra geçerli liste öğesine bağlanır. Arama alanları iki liste arasında ilişki oluşturmak için kullanılır. CAML (İşbirlikçi Uygulama İşaretleme Dili), SharePoint'te listelerden ve kitaplıklardan veri sorgulamak için kullanılan XML tabanlı bir sorgu dilidir. Bu yazımızda PowerShell'de bir arama alanı için CAML sorgusunun nasıl oluşturulacağını göreceğiz.
Projeleri ana proje arama alanı değeri olan “Adres Defteri”ne göre filtreleyelim. Arama alanı için SharePoint CAML'i burada bulabilirsiniz.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$ListName="Projects"
$LookupValue="Address Book"
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List Object
$List = $Ctx.Web.lists.GetByTitle($ListName)
#Define the CAML Query
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<Eq>
<FieldRef Name='ParentProject'/><Value Type='Lookup'>$LookupValue</Value>
</Eq>
</Where>
</Query>
</View>"
#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of Items:"$ListItems.count
#Loop through each List Item
ForEach($Item in $ListItems)
{
#Do something
Write-host $Item.id
Write-host $Item["Title"]
}
Arama Alanını kimliğe göre Filtrelemek için SharePoint CAML Sorgusu:
Üst listede tanımlanan arama metni her an değişebileceği için metin yerine arama alanı kimliğini kullanalım. İşte arama sütunu için PowerShell CAML sorgusu.
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$ListName="Projects"
$LookupID="25" #ID of the Parent Lookup Item
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List Object
$List = $Ctx.Web.lists.GetByTitle($ListName)
#Define the CAML Query
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<Eq>
<FieldRef Name='ParentProject' LookupId='TRUE'/><Value Type='Lookup'>$LookupID</Value>
</Eq>
</Where>
</Query>
</View>"
#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of Items:"$ListItems.count
#Loop through each List Item
ForEach($Item in $ListItems)
{
#Do something
Write-host $Item["Title"]
}
Çoklu Arama Alanı için SharePoint Çevrimiçi CAML:
Burada, "Ana Proje" arama alanı değerinin hem "Adres Defteri"ni hem de "Duyurular"ı içerdiği yerleri filtreleyen, çoklu arama sütunu için SharePoint CAML sorgusu verilmiştir.
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$ListName="Projects"
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List Object
$List = $Ctx.Web.lists.GetByTitle($ListName)
#Define the CAML Query
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<And>
<Includes>
<FieldRef Name='ParentProject'/>
<Value Type='LookupMulti'>Address Book</Value>
</Includes>
<Includes>
<FieldRef Name='ParentProject'/>
<Value Type='LookupMulti'>Announcements</Value>
</Includes>
</And>
</Where>
</Query>
</View>"
#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of Items:"$ListItems.count
#Loop through each List Item
ForEach($Item in $ListItems)
{
#Do something
Write-host $Item["Title"]
}