Just little about C#, .NET, SQL Server, SharePoint and SAP

SharePoint Alerts Export Import add-in Part – 3

Posted by zieglers on November 9, 2009


Here is the 3rd part of the article – SharePoint Alerts Export Import add-in. I realized that it’s been a while that i wrote first and second articles. Taking a quick look at those, i see that “importing alerts” logic is left for this article.

Importing alerts can be done for a selected site collection and also individual subsites can also be selected explicitly. Alerts export logic was exporting all alerts of a site collection, not only root site, but also all subsites. Also those alert details were stored in an xml file in a structured manner. However, for import operation we need a selective import logic. Not all the time we might need to import alerts for a whole site collection. That’s why logic will be a little more complicated than exporting alerts.  

Moreover, an import operation can take some time based on number of alerts being imported and number of subsites exist. That’s why we need to have an SP Long operation so that user knows operation is being executed. Here there is a nice thing to note, that is, if user somehow closes the browser, import operation is not interrupted and goes on in the background until finished.Another important thing is the difference between type of alerts:  sp list alerts and search alerts. Although both of them are using SPAlert class, based on attributes being set a generic list alert can become a search alert. (p_query is the most important attribute for a search alert, basically which holds the query of the search alert)Here is a look how our Alerts Import application page ui is going to be like.   


After this intro, it’s time to get started.  Firstly let’s take a look at UI design. 

Alerts Import Page – UI Design

As for site collection selector seen above in first subsection of UI, we’ll use OOTB SharePoint SiteAdministrationSelector control. This control is frequently used in many application pages of Central Admin. Then, in order to list all subsites (in subsection 2) after a site collection is selected, we’ll have a CheckBoxList control. Finally, there is going to be a FileUpload control to select import file – the file which is exported using Alerts Export page. In addition, there is a “Verify” button to validate selected import file.  

SiteAdministrationSelector control 

<!-- *********************************************************
<wssuc:InputFormSection runat="server"
   Title="<%$Resources:spadmin, multipages_sitecollection_title%>"
   Description="<%$Resources:spadmin, multipages_sitecollection_desc%>" >
         <!-- Put your control here-->

<SharePoint:SiteAdministrationSelector id="SiteCollectionSelector" runat="server" OnContextChange="OnContextChange" /></td></tr></Template_InputFormControls> </wssuc:InputFormSection><!-- ********************************************************** -->   

Subsites CheckBoxList 
<!-- **********************************************************
<wssuc:InputFormSection runat="server"
   Title="Web Sites"
   Description="Check web sites for which you want to import alerts" >

      <asp:CheckBoxList ID="cbWebs" runat="server" CssClass="ms-listheaderlabel" EnableViewState="true"   />
<!-- *********************************************************** -->

FileUpload control for Alerts Import file and Verify Button
<!-- File name section -->
    Title="Select Alerts File"
    Description="Browse to the alerts file you intend to import."

        <wssuc:InputFormControl runat="server" LabelText="Name :">
         <span dir="ltr"> 
          <asp:FileUpload ID="FileUpload1" runat="server" />

        <wssuc:InputFormControl LabelText="Before importing, you can check this alerts import file for detailed errors, warnings, and other information." runat="server">
          <asp:button class="ms-ButtonHeightWidth" runat="server"

        <wssuc:InputFormControl runat="server">
          <div><asp:Label id="lblFileValidation" runat="server"/></div>

Alerts Import Page – Import Logic Design

So far, we have our UI elements ready. Now, let’s take a look at application logic. Almost all page logic is executed after user clicks ok button. Here is BtnSubmit_Click logic:

Basic validations

  • Check if at least one web selected or not –> Display validation message if no web selected
  • Check uploaded file –>Restrict the user to upload only .xml file


  • Assign import log file name and path
  • Get url, port, virtual path details

Import Operation

  • Read Alerts xml file for import operation
  • Initialize date and alert count
  • Iterate through each row of ‘Alerts’. This is also equivalent to looping through each web object.
    • Only import alerts for selected webs
    • Get alert property values from xml
    • Check Alert Template Type. This determines if an alert is a Search alert, List Alert, Item Alert …etc.
    • Create alerts

Alerts Import Page – Implementation

Basic validations

Before starting to import alerts, first we need to check alerts import file. Here we get some info about file using FileUpload control and only allow .xml files.

// Check uploaded file
if (FileUpload1.HasFile)
    // get the full path of your computer
    string strFileNameWithPath = FileUpload1.PostedFile.FileName;
    // get the extension name of the file
    string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPath);
    // get the filename of user file
    string strFileName = System.IO.Path.GetFileName(strFileNameWithPath);
    // get the file size
    int intFileSize = FileUpload1.PostedFile.ContentLength / 1024; // convert into byte

    // Restrict the user to upload only .xml file
    strExtensionName = strExtensionName.ToLower();
    if (strExtensionName.Equals(“.xml”))
        // upload the file on the server
        FileUpload1.PostedFile.SaveAs(AlertsDir + strFileName);

        strMessages += “Uploaded file details <hr />” +
            “File path on your Computer: ” + strFileNameWithPath + “<br />” +
            “File Name: ” + strFileName + “<br />” +
            “File Extension Name: ” + strExtensionName + “<br />” +
            “File Size: ” + intFileSize.ToString();
        // Assign Alerts File Path
        alertsFile = AlertsDir + strFileName;
        strMessages += “<br />Only <b>.xml</b> file is allowed, try again!<br />”;
    strMessages += “<br /><b>Select Alerts File: </b>You must select a valid alerts import file.<br />”;


You can choose an import log file naming convention based on your needs. Here I used DateTime stamp.

// Assign import log file name and path

string logfile = logDir + “ImportLog_” + DateTime.Today.Day.ToString() + DateTime.Today.Month + DateTime.Today.Year.ToString() + “.txt”;

Import Operation

  • Read Alerts xml file for import operation

// Read Alerts xml file for import operation
System.Data.DataSet ds = new System.Data.DataSet();

// Read selected alerts import xml file
ds.Locale = System.Globalization.CultureInfo.CurrentUICulture;

// Read xml document
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

// Keep a list of selected webs in an arraylist
ArrayList selectedWebsList = new ArrayList();
foreach (ListItem item in cbWebs.Items)
    if (item.Selected)

  • Initialize date and alert count and check datatable loaded by xml file.

// Initialize date and alert count
long AlertCount = 0;
DateTime start = DateTime.Now;
DateTime finish = DateTime.Now;

// Check if xml file has tables for alerts data
if (ds.Tables.Count < 2)
    LogMessageToFile(logDir + “ErrorLog.txt”, “Not a valid Alerts xml file”);
    strMessages += “<br /><font color=red><b>File Error: </b></font>Not a valid Alerts xml file! <br />”;

  • Iterate through each row of ‘Alerts’. This is also equivalent to looping through each web object.
    • Only import alerts for selected webs
    • Get alert property values from xml
    • Check Alert Template Type. This determines if an alert is a Search alert, List Alert, Item Alert …etc.
    • Create alerts

In case you are having trouble implementing the solution, here is AlertsImportIntoWebs.aspx page: AlertsImportIntoWebs.aspx


7 Responses to “SharePoint Alerts Export Import add-in Part – 3”

  1. bertrik said

    All works fine right now!

    Many thanks to you for the great solution…
    We can now export our sitecollections, without losing the alerts.

    Thanks a lot for your help!

  2. zieglers said

    Hi Bertrik,

    I’m glad it works fine for you. After using it, please share your feedback and experience with it. Also i’ll be glad if you can point out the ways this tool can be improved, so that i can come up with new versions. Planning to put this to codeplex as well.


    • Hi Zieglers,

      Does it work in SharePoint 2010. Same functionality tool is available on codeplex, does that works on sp2010.

      Awaiting your response.

      Many Thanks,

  3. rick otton said

    rick otton…

    […]SharePoint Alerts Export Import add-in Part – 3 « Zieglers[…]…

  4. that is awesome!…

    […]SharePoint Alerts Export Import add-in Part – 3 « Zieglers[…]…

  5. Free horseback riding in NYC…

    […]SharePoint Alerts Export Import add-in Part – 3 « Zieglers[…]…

  6. attorney in the philippines,business law attorneys,small business attorney,business law attorney,philippine attorney,lawyers in manila,Legal attorney ,legal aid attorney,legal lawyer,attorney search,legal help,find a lawyer,legal research attorney…

    […]SharePoint Alerts Export Import add-in Part – 3 « Zieglers[…]…

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: