Skip to content

Instantly share code, notes, and snippets.

@ctkirkman
Created November 8, 2016 15:09
Show Gist options
  • Save ctkirkman/a575e1941ca79d2ade79c7724aa29939 to your computer and use it in GitHub Desktop.
Save ctkirkman/a575e1941ca79d2ade79c7724aa29939 to your computer and use it in GitHub Desktop.
function Get-SPSiteList {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[String]$WebAppUrl
)
process {
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebAppUrl)
$q = new-object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
$q.QueryText = "ContentClass=STS_Site"
$se = new-object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
$r = $se.ExecuteQuery($q)
$ctx.ExecuteQuery()
$ret = @()
$r.Value.ResultRows | %{
$ret += New-Object PSObject -property @{
Title = $_["Title"]
Description = $_["Description"]
Url = $_["Path"]
}
}
return $ret
}
}
function Get-SPWebList {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[String]$SearchCenter
)
process {
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SearchCenter)
$q = new-object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
$q.QueryText = "ContentClass=STS_Web OR ContentClass=STS_Site"
$se = new-object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
$r = $se.ExecuteQuery($q)
$ctx.ExecuteQuery()
$ret = @()
$r.Value.ResultRows | %{
$ret += New-Object PSObject -property @{
Title = $_["Title"]
Description = $_["Description"]
Url = $_["Path"]
}
}
return $ret
}
}
function New-SPSearchQuery {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[String]$SearchCenter,
[Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[String]$KeywordQuery
)
process {
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SearchCenter)
$q = new-object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
$q.QueryText = $KeywordQuery
$se = new-object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
$r = $se.ExecuteQuery($q)
$ctx.ExecuteQuery()
return $r.Value.ResultRows
}
}
function Get-SPWeb {
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[String]$Identity
)
process {
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Identity)
$ctx.Credentials = [System.Net.CredentialCache]::DefaultCredentials
$web = $ctx.web
$ctx.Load($web)
$ctx.Load($web.lists)
$ctx.Load($web.contenttypes)
$ctx.Load($web.webs)
$ctx.Load($web.folders)
$ctx.ExecuteQuery()
return $web
}
}
function Get-SPList {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[Microsoft.SharePoint.Client.Web]$Web,
[Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()]
[String]$Name
)
process {
$list = $null
try {
[System.Guid]::Parse($Name) | Out-Null
$list = $Web.Lists.GetById($Name)
} catch {
$list = $Web.Lists.GetByTitle($Name)
}
$web.Context.Load($list)
$web.Context.Load($list.ContentTypes)
$web.Context.Load($list.Fields)
$web.Context.ExecuteQuery()
return $list
}
}
function Get-SPListItems {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[Microsoft.SharePoint.Client.List]$List,
[Parameter(Position=1, Mandatory=$false)]
[String]$Filter = "<View><Query></Query><RowLimit>5000</RowLimit></View>"
)
process {
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = $Filter
$items = $List.GetItems($query)
$List.Context.Load($items)
$List.Context.ExecuteQuery()
return $items
}
}
function Get-SPListItemsAll {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[Microsoft.SharePoint.Client.List]$List,
[Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()]
[String]$RowThreshold = 5000,
[Parameter(Mandatory=$false)]
[String]$Filter = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$RowThreshold</RowLimit></View>"
)
process {
$results = @()
[Microsoft.SharePoint.Client.ListItemCollectionPosition]$position = $null
Do{
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = $Filter
$query.ListItemCollectionPosition = $position
$items = $List.GetItems($query)
$List.Context.Load($items)
$List.Context.ExecuteQuery()
$position = $items.ListItemCollectionPosition
$results += $items
} Until ($position -eq $null)
return $results
}
}
function Remove-SPListItems {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[System.Object[]]$Items,
[Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()]
[String]$BatchSize = 250
)
process {
$ctx = $Items[0].ParentList.ParentWeb.Context
$x = 0
$Items | %{
$x++
$_.DeleteObject()
if($x -ge $BatchSize) {
$ctx.ExecuteQuery()
$x = 0
}
}
$ctx.ExecuteQuery()
}
}
function New-SPListItem {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[Microsoft.SharePoint.Client.List]$List,
[Parameter(Position=1, Mandatory=$false)][ValidateNotNullOrEmpty()]
[System.Collections.Hashtable]$Values
)
process{
$info = new-object Microsoft.SharePoint.Client.ListItemCreationInformation
$item = $List.AddItem($info)
if ($Values -eq $null) {
return $item
} else {
$Values.Keys | %{
$item[$_] = $Values[$_]
}
$item.update()
$List.Context.Load($item)
$List.Context.ExecuteQuery()
return $item
}
}
}
function Update-SPListItem {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[Microsoft.SharePoint.Client.ListItem]$Item,
[Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()]
[System.Collections.Hashtable]$Values
)
process{
$Values.Keys | %{
$Item[$_] = $Values[$_]
}
$Item.update()
$List.Context.Load($Item)
$List.Context.ExecuteQuery()
return $Item
}
}
function Get-SPUser {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[Microsoft.SharePoint.Client.Web]$Web,
[Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()]
[String]$Login
)
process{
$User = $Web.EnsureUser($Login)
$Web.Context.Load($User)
$Web.Context.ExecuteQuery()
return $User
}
}
function Get-SPWebPermissions{
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
[Microsoft.SharePoint.Client.Web]$Web
)
process {
$Web.Context.Load($Web.SiteGroups)
$Web.Context.ExecuteQuery()
$Web.SiteGroups | %{ $Web.Context.Load($_.Users) }
$Web.Context.ExecuteQuery()
$Web.SiteGroups | %{
Write-Host $_.Title -ForegroundColor "Yellow"
Write-Host "============================================" -ForegroundColor "Yellow"
$_.Users | %{ Write-Host "$($_.Title) [$($_.LoginName)] : $($_.PrincipalType)" }
Write-Host ""
}
}
}
function Get-SPObjectPropertyRetrievals {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[System.Type]$Object,
[Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()]
[System.Array]$Properties
)
process {
$retrievals = @()
$lambda = [System.Linq.Expressions.Expression].GetMethods() | ?{ $_.Name -eq "Lambda" -and $_.IsGenericMethod -and $_.GetParameters()[0].ParameterType -eq [System.Linq.Expressions.Expression] -and $_.GetParameters()[1].ParameterType -eq [System.Linq.Expressions.ParameterExpression[]] }
$lambdaGM = $lambda.MakeGenericMethod($(Invoke-Expression "[System.Func``2[$($Object.FullName),System.Object]]"))
$Properties | %{
$retrievals += $lambdaGM.Invoke(
$null,
[System.Object[]]@(
[System.Linq.Expressions.Expression]::Property([System.Linq.Expressions.Expression]::Parameter($($Object), "o" ), $_),
[System.Linq.Expressions.ParameterExpression[]] @([System.Linq.Expressions.Expression]::Parameter($($Object), "o" ))
)
)
}
return $retrievals
}
}
function Get-SPObjectIncludeRetrievals {
#[Microsoft.SharePoint.Client.ClientObject]
#[Microsoft.SharePoint.Client.ClientObjectCollection]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[System.Type]$Object,
[Parameter(Position=1, Mandatory=$true)]
[System.Type]$Collection,
[Parameter(Position=2, Mandatory=$true)]
[System.String]$PropertyName,
[Parameter(Position=3, Mandatory=$true)][ValidateNotNullOrEmpty()]
[System.Array]$Properties
)
process {
#$include = [Microsoft.SharePoint.Client.ClientObjectQueryableExtension].GetMethod("Include")
#$includeGM = $include.MakeGenericMethod($($Collection.BaseType.GenericTypeArguments[0].FullName))
#$lambda = [System.Linq.Expressions.Expression].GetMethods() | ?{ $_.Name -eq "Lambda" -and $_.IsGenericMethod -and $_.GetParameters()[0].ParameterType -eq [System.Linq.Expressions.Expression] -and $_.GetParameters()[1].ParameterType -eq [System.Linq.Expressions.ParameterExpression[]] }
#$lambdaGM = $lambda.MakeGenericMethod($(Invoke-Expression "[System.Func``2[$($Object.FullName),System.Object]]"))
#$retrievals = Get-SPObjectPropertyRetrievals -Object $Collection -Properties $Properties
#$callMethod = [System.Linq.Expressions.Expression]::Call($null, $include.MakeGenericMethod($($Collection.BaseType.GenericTypeArguments[0].FullName)), $retrievals)
#$return = $lambdaGM.Invoke($null, @($callMethod, [System.Linq.Expressions.ParameterExpression[]] @($collectionParam)))
$retrievals = Get-SPObjectPropertyRetrievals -Object $Collection.GetProperty("Item").PropertyType -Properties $Properties
$newArrayInitParam1 = Invoke-Expression "[System.Linq.Expressions.Expression``1[System.Func````2[$($Object.FullName),System.Object]]]"
$newArrayInit = [System.Linq.Expressions.Expression]::NewArrayInit($newArrayInitParam1, $retrievals)
$collectionParam = [System.Linq.Expressions.Expression]::Parameter($parentObject.GetType(), "cp")
$collectionProperty = [System.Linq.Expressions.Expression]::Property($collectionParam, $PropertyName)
$expressionArray = @($collectionProperty, $newArrayInit)
$includeMethod = [Microsoft.SharePoint.Client.ClientObjectQueryableExtension].GetMethod("Include")
$includeMethodGeneric = Invoke-Expression "`$includeMethod.MakeGenericMethod([$($Object.FullName)])"
$lambdaMethodGeneric2 = Invoke-Expression "`$lambdaMethod.MakeGenericMethod([System.Func``2[$($parentObject.GetType().FullName),System.Object]])"
$callMethod = [System.Linq.Expressions.Expression]::Call($null, $includeMethodGeneric, $expressionArray)
$expression2 = $lambdaMethodGeneric2.Invoke($null, @($callMethod, [System.Linq.Expressions.ParameterExpression[]] @($collectionParam)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment