PowerShell ile SharePoint'te Olay Alıcıları Ekle-Kaldır-Al


Olay alıcılarını SharePoint'teki hedef listeye veya kitaplığa kaydetmek için genellikle C# konsol uygulamasını veya özellik etkinleştirme kodunu kullanırız. Bazen olay alıcısı ilişkilerini açıkça ele almamız gerekebilir. Elbette olay alıcılarını yönetmek için bazı harika araçlar (ör. SharePoint Etkinlik Alıcı Yöneticisi) vardır. Ancak güvenlik nedeniyle bu tür araçları kullanmama izin vermeyen bir üretim ortamında şu seçenek kaldı: PowerShell. Bu nedenle, PowerShell kullanarak SharePoint'teki olay alıcılarını yönetmek için PowerShell kod parçacıklarımı paylaşıyorum.

Tüm olay alıcılarının bir Listeye eklenmesini sağlayın:

Belirli bir listeyle ilişkili olay alıcılarını almak için SharePoint 2010 PowerShell'i bulalım.


 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

 #Get the web
 $Web = Get-SPWeb "https://sharepoint.crescent.com"

 #Get the Target List
 $List = $Web.Lists["Documents"]

 #Retrieve all event receivers associated with the list
 $List.EventReceivers | Select Id, Type, Assembly, Class | FL 

Bir web uygulamasında tüm sitelerdeki tüm Etkinlik alıcılarını bulun:

Benzer şekilde, aşağıdaki PowerShell betiğini kullanarak tüm site koleksiyonlarındaki çeşitli listeler ve kitaplıklarla ilişkili tüm olay alıcılarını alabiliriz.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebAppURL = "https://sharepoint.crescent.com"

 #Get All Webs
 $Webs = Get-SPWebApplication $WebAppURL | Get-SPSite -Limit All | Get-SPWeb 
 
foreach($web in $Webs)
{
    #Get Lists with Event receivers excluding System lists
    $web.lists | where { ($_.Author.LoginName -ne "SHAREPOINT\system") -and ($_.Hidden -eq $false) -and ($_.IsCatalog -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false) -and ($_.EventReceivers.count -gt 0) } | Select Title, EventReceivers | FL
} 

Bu PowerShell, SharePoint'teki liste olay alıcısını kontrol eder.

Listeye Olay Alıcısı Eklemek için SharePoint PowerShell

Genellikle bir olay alıcısını belirli bir listeyle ilişkilendirmek için bir C#.net konsol uygulaması oluştururuz. Bu sefer PowerShell betiğiyle denedim: Hadi olay alıcısını SharePoint 2013'te PowerShell kullanarak dağıtalım.

PowerShell’de tanımı oluşturarak olay alıcısını ekleyelim:


#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"

#Get the Target List
$List = $Web.Lists["Documents"]

#Add new event receiver definition
$def = $list.EventReceivers.Add()
$def.Assembly = "Crescent.DocRestrict, Version=1.0.0.0, Culture=neutral, PublicKeyToken=677b45b1314c252c"
$def.Class = "Crescent.Utilities.DocRestrict.Restrict" 
$def.Type = [Microsoft.SharePoint.SPEventReceiverType]::ItemAdded
$def.Name = "ItemAdded Event Receiver";
$def.SequenceNumber = 3000
$def.Synchronization = [Microsoft.SharePoint.SPEventReceiverSynchronization]::Synchronous
$def.Update() 

SharePoint PowerShell ile Olay Alıcısı Ekleme

Alternatif olarak, SharePoint'te PowerShell ile listeye olay alıcısı eklemek için:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"

#Get the Target List
$List = $Web.Lists["Documents"]

#Retrieve all event receivers associated with the list
#$List.EventReceivers | Select Id, Type, Assembly, Class | FL

$Assembly = "Crescent.DocRestrict, Version=1.0.0.0, Culture=neutral, PublicKeyToken=677b45b1314c252c"
$Class= "Crescent.Utilities.DocRestrict.Restrict"
Write-Host "Attaching Event Receiver..."
# sharepoint 2010 powershell register event receiver
$list.EventReceivers.Add("ItemAdding", $Assembly , $Class)

İçerik türleri nasıl? PowerShell kullanarak olay alıcısı ekleyebilir miyim? Neden?


#Get the content type
$ctype = $web.ContentTypes["content type name"]
#Add event receiver
$ctype.EventReceivers.Add("ItemAdding", "Assembly Name", "Class")

SharePoint PowerShell ile Olay Alıcısını Sil:

SharePoint'te PowerShell'i kullanarak olay alıcısını silmek için kod aşağıdadır:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"

#Get the Target List
$List = $Web.Lists["Documents"]

$Assembly = "Crescent.DocRestrict, Version=1.0.0.0, Culture=neutral, PublicKeyToken=677b45b1314c252c"

$EventReceivers = $list.EventReceivers | Where {$_.Assembly -eq $assembly}

if ($EventReceivers) #.Count -gt 0)
{
    foreach($Receiver in $EventReceivers)
    { 
         Write-Host "Deleting Event Receiver from " $list.RootFolder.ServerRelativeUrl 

         $Receiver.Delete()
    }
} 

Olay Alıcısını Kaldırmak için SharePoint PowerShell:

Belirli bir olay alıcısını kimliğine göre de silebilirsiniz. PowerShell kullanarak olay alıcılarını SharePoint listesinden kaldırmanın bir örneğini burada bulabilirsiniz.


#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"

#Get the Target List
$List = $Web.Lists["Documents"]

#Get all event receivers associated with the list
$List.EventReceivers | Select Id, Type, Assembly, Class | FL 

#$EventReceiverID = "625c59a9-72d7-4479-a1e7-35f040e4f9a1"
#Uncomment below line to sharepoint 2010 remove event receiver from list powershell
#$list.EventReceivers[[GUID]$EventReceiverID].delete()

Yinelenen Olay Alıcılarını Kaldır:

SharePoint olay alıcılarının yinelenen örnekleri bilinen bir sorundur. Farklı sıra numaralarıyla ortak bir düzeneği paylaşırlar. Yani iki kez ateş ediyorlar.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"

#Get the Target List
$List = $Web.Lists["Documents"]

#Get all event receivers grouped by assembly/class/type
$list.EventReceivers | Group-object assembly, class, type |  where { $_.Count -gt 1 } 

#If any of the assembly/class/type pairs are same, we have a duplicate!

#Leave the first event receiver at 0th position and delete all other
$list.EventReceivers | Group-object assembly, class, type |  where { $_.Count -gt 1 } |  foreach { $_.Group[1..50] } | foreach { $_.Delete() }

C# ile program aracılığıyla SharePoint listesine olay alıcıları eklemek için şuraya bakın: SharePoint listeye olay alıcısı ekleme

İlgili makaleler