# Defintion of function to attach web event receivers
# if they are not already added
Function AddWebEventReceivers($webER)
{
$assembly = "*fully_qualified_assembly_name*"
$class = "*class_name_including_namespace*"
# To Check fully qualified assembly name Click Here
# Only attach receivers if there aren't already added
# You can make the check more specific by checking the Type
# property as well if required
$existingER = $webER.EventReceivers | Where { $_.Class -eq $class }
if($existingER -eq $null -or $existingER.length -eq 0)
{
$webER.EventReceivers.Add("WebDeleting", $assembly, $class)
}
}
# Iterate all webs and attach the web event receivers to
$web = Get-SPWeb "*SubsiteUrl*"
$subwebs = $web.GetSubwebsForCurrentUser()
foreach($web in $subwebs)
{
try
{
AddWebEventReceivers($web)
Write-Host "Event Receiver attached to web: $web"
}
catch [System.Exception]
{
$errorMessage = $Error[0]
Write-Host "Failed: $errorMessage" -NoNewline -F Red
}
finally
{
$web.Dispose()
}
}
$web.Dispose()
No comments:
Post a Comment