Share via


How to get all sharepoint user alerts for a sitecollection using powershell

Question

Wednesday, November 21, 2018 11:13 AM

I am new to powershell scripts development.Currently i developed following powershell script to get all user alerts in the site collection. how to loop through the user alerts in the foreach loop.

$siteUrl = Read-Host "Enter url"
# Example - http://sharepoint2016:81/sites/Ramesh

if($siteUrl -ne $null -and $siteUrl -ne '')
{
    #Get the client context and load root web and sub webs
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $site = $context.Web
    $context.Load($site)
    $context.Load($site.Webs)
    $context.ExecuteQuery()
    
    #Run for the all webs under Root web
    foreach($web in $site.Webs){
   
    }

    Write-Host "Completed!"
}

All replies (6)

Wednesday, November 21, 2018 2:51 PM

Hi,

As you work on an on-premises version of SharePoint, you should use the SharePoint Management Shell (PowerShell), rather than csom.

You will find your answerr here: GET ALL USER ALERTS FOR SHAREPOINT SITE COLLECTION

My technical blog on SharePoint || My contributions on the TechNet Gallery


Wednesday, November 21, 2018 3:39 PM

 Thanks for the quick reply Benoit Jester [MVP].

I am not working in the on-premises.

In that server machine Sharepoint Management Shell was not installed.[It is not a sharepoint server].

So when i am tring to run the Get-SPSite in Windows powershell method is not recognized.

So that i went for Powershell using CSOM 

and Below is the script.

      

$site = Read-Host "Enter url"
# Example - http://sharepoint2016:81/sites/Ramesh
#Write-Host "Scanning Sites........."

    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site)
    $web = $ctx.Web
    $ctx.Load($web)

    $siteUsers = $web.SiteUsers
    $ctx.Load($siteUsers)
    $alerts = $web.Alerts
    $ctx.Load($alerts)

    $ctx.ExecuteQuery()

    foreach ($alert in $alerts) {
    foreach ($siteUser in $siteUsers){ 
        If ($siteUser.Id -eq $alert.UserId){
            $alert.Title
            $siteUser.id
            $siteUser.email
        }
    } 
   }

    Write-Host "Completed!"

Getting error at line no 52

 $ctx.Load($alerts)


Thursday, November 22, 2018 3:31 AM

Hi,

Please check the Web.Alerts property from the article below, it only support to SharePoint Online currently.

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.web.alerts.aspx?f=255&MSPPError=-2147217396

In on-premise, you will get the error in the following line of code.

$ctx.Load($alerts)

As a workaround, we can use alerts.asmx web service to achieve it. The C# code below for your reference.

static void Main(string[] args)
{
    Alerts alerts = new Alerts();
    alerts.Url =  "http://sp/sites/team/_vti_bin/alerts.asmx";
    alerts.Credentials = CredentialCache.DefaultCredentials;
    AlertInfo alertInfo = alerts.GetAlerts();
    Console.WriteLine("AlertWebTitle: "+alertInfo.AlertWebTitle);
    Console.WriteLine("AlertServerName: "+alertInfo.AlertServerName);
    Console.WriteLine("AlertServerType: " + alertInfo.AlertServerType);
    Console.WriteLine("AlertServerUrl: "+alertInfo.AlertServerUrl);
    Console.WriteLine("Alerts Number:" + alertInfo.Alerts.Length.ToString());
    Console.WriteLine("CurrentUser: "+alertInfo.CurrentUser);
    foreach (Alert alert in alertInfo.Alerts)
    {
        Console.WriteLine("Alert Information: ");
        Console.WriteLine("");
        Console.WriteLine("Title: "+alert.Title);
        Console.WriteLine("AlertForUrl: "+alert.AlertForUrl);
    }
    Console.ReadLine();
}

More information: https://www.c-sharpcorner.com/UploadFile/anavijai/get-alerts-from-sharepoint-2010-site-using-web-service/

Using web service from PowerShell, please refer to the article below.

https://unstructuredqueries.wordpress.com/2013/04/22/sharepoint-web-services-and-powershell/

Best Regards,

Dennis

Best Regards,

Dennis

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


Friday, November 23, 2018 1:24 PM

Thanks Dennis Guo.


Monday, November 26, 2018 1:11 AM

Hi Rameshbabu,

If my reply helps you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

Best Regards,

Dennis

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


Friday, November 30, 2018 6:35 AM

Hi,

Would you please provide us with an update on the status of your issue?

Best Regards,
Dennis

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.