none
Retrieve and Set User Alerts RRS feed

  • Question

  • Hi - I would like to programmatically retrieve all user alerts from sharepoint 2007 in a format which would then allow me to set  against another instance of sharepoint (2013). This would require iterating through all webs/lists and storing as csv or xml.

    I plan to iterate the SPAlertCollection, get the values, store and then set against another collection.

    Does this approach sound possible? Any examples of guidance appreciated.

       static void Main(string[] args)
            {
                _siteUrl = "";
                _collWebsites = _site.AllWebs;
                foreach (SPWeb oWebsite in _collWebsites)
                {
                    SPAlertCollection collAlerts = oWebsite.Alerts;
                    foreach (SPAlert oAlert in collAlerts)
                    {
                        Console.WriteLine("Website Title: {0} || Alert Title: {1} || User: {2}", oWebsite.Title,
                                          oAlert.Title, oAlert.User.LoginName);
                    }
                }

    Tuesday, August 13, 2013 2:58 PM

Answers

  • Hi,

    Based on your description, you want to retrieve all user alerts from SharePoint 2007 and store as csv or xml file, then set new alerts in SharePoint 2013 according this csv or xml file. So the whole procedure would be like this:

    1. Retrieve all user alerts from SharePoint 2007 and store in local drive as a xml file.

    The code snippet you provided can retrieves the alert's properties, more properties can be found in this post. So what we need to do now is save the data as xml file.

    Stefan's post in this thread can write/read xml:

    http://social.msdn.microsoft.com/Forums/en-US/2aa61d5c-6804-46e3-b186-7515a2dfe7e4/create-modify-and-read-xml-files-in-c

    2. Then we come to SharePoint 2013. Please ensure that there are lists, libraries and users as same as those in SharePoint 2007, then we can create user alerts according to this xml file in SharePoint 2013.

    This link will show how to create alerts programmatically:

    http://www.sharepointology.com/development/how-to-create-alerts-programmatically/

    best regards
    Wednesday, August 14, 2013 5:25 AM
    Moderator
  • This has some powershell line of code to get all the alerts setted:

    clear
    
    $webAppUrl = Read-Host -Prompt "Url Web Application: "
    
    if ($webAppUrl.Length -ile 0) {
    	$webAppUrl = "http://win-7iu1bqtgj2n:17927/sites/en-us"
    }
    
    $web = Get-SPWeb $webAppUrl
    
    $myalerts = @()
    
    $alerts = $web.Alerts
    
    foreach ($alert in $alerts) {
    	if ($alert.User.LoginName.Contains("myval") -eq "True") {
    		continue
    	}
    
    	"Status							- " + $alert.Status
    	"Title								- " + $alert.Title
    	"User ID							- " + $alert.User.ID
    	"User Name							- " + $alert.User.Name
    	"User Login							- " + $alert.User.LoginName
    	"Frequency							- " + $alert.AlertFrequency
    	"List								- " + $alert.List.Title
    
    	Write-Host "_____________________"
    	$myalerts += 1
    }
    
    Write-Host "Num of alerts: " $myalerts.Count

    you can do the same in C# and then write the result has a XML file.

    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful" salvatoredifaziosharepoint.blogspot.com

    Twitter: @Salvodif
    MVP SharePoint Server

    Wednesday, August 14, 2013 11:02 AM

All replies

  • Hi,

    Based on your description, you want to retrieve all user alerts from SharePoint 2007 and store as csv or xml file, then set new alerts in SharePoint 2013 according this csv or xml file. So the whole procedure would be like this:

    1. Retrieve all user alerts from SharePoint 2007 and store in local drive as a xml file.

    The code snippet you provided can retrieves the alert's properties, more properties can be found in this post. So what we need to do now is save the data as xml file.

    Stefan's post in this thread can write/read xml:

    http://social.msdn.microsoft.com/Forums/en-US/2aa61d5c-6804-46e3-b186-7515a2dfe7e4/create-modify-and-read-xml-files-in-c

    2. Then we come to SharePoint 2013. Please ensure that there are lists, libraries and users as same as those in SharePoint 2007, then we can create user alerts according to this xml file in SharePoint 2013.

    This link will show how to create alerts programmatically:

    http://www.sharepointology.com/development/how-to-create-alerts-programmatically/

    best regards
    Wednesday, August 14, 2013 5:25 AM
    Moderator
  • Thanks - I'll take a look :)
    Wednesday, August 14, 2013 10:08 AM
  • This has some powershell line of code to get all the alerts setted:

    clear
    
    $webAppUrl = Read-Host -Prompt "Url Web Application: "
    
    if ($webAppUrl.Length -ile 0) {
    	$webAppUrl = "http://win-7iu1bqtgj2n:17927/sites/en-us"
    }
    
    $web = Get-SPWeb $webAppUrl
    
    $myalerts = @()
    
    $alerts = $web.Alerts
    
    foreach ($alert in $alerts) {
    	if ($alert.User.LoginName.Contains("myval") -eq "True") {
    		continue
    	}
    
    	"Status							- " + $alert.Status
    	"Title								- " + $alert.Title
    	"User ID							- " + $alert.User.ID
    	"User Name							- " + $alert.User.Name
    	"User Login							- " + $alert.User.LoginName
    	"Frequency							- " + $alert.AlertFrequency
    	"List								- " + $alert.List.Title
    
    	Write-Host "_____________________"
    	$myalerts += 1
    }
    
    Write-Host "Num of alerts: " $myalerts.Count

    you can do the same in C# and then write the result has a XML file.

    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful" salvatoredifaziosharepoint.blogspot.com

    Twitter: @Salvodif
    MVP SharePoint Server

    Wednesday, August 14, 2013 11:02 AM
  • Hi Salvo - this looks good. Although I'll need to use a console app and web services as the source and destination SharPoint instances are on different (internal) servers.

    I'm not sure how to start because the app will need to connect to the source, iterate through the alerts and then when creating a new alert, open a new connection. Are you aware of an efficient way to do this (in terms of opening and closing connections across two servers)?

    In addition as a lists or user may not exist I require some some of validation.. Could be a bigger job than I thought :/

    Friday, August 23, 2013 1:33 PM