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
<!-- ********************************************************* DISPLAY THE SITE COLLECTION SELECTOR USING THE InputFormSecton AND SiteAdministrationSelector CONTROLS. --> <wssuc:InputFormSection runat="server" Title="<%$Resources:spadmin, multipages_sitecollection_title%>" Description="<%$Resources:spadmin, multipages_sitecollection_desc%>" > <Template_InputFormControls> <tr><td><!-- Put your control here--><SharePoint:SiteAdministrationSelector id="SiteCollectionSelector" runat="server" OnContextChange="OnContextChange" /></td></tr></Template_InputFormControls> </wssuc:InputFormSection><!-- ********************************************************** -->
<!-- ********************************************************** DISPLAY ALL SUBSITES USING THE InputFormSecton AND ASP:CheckBoxList CONTROLS --> <wssuc:InputFormSection runat="server" Title="Web Sites" Description="Check web sites for which you want to import alerts" > <Template_InputFormControls> <tr><td> <asp:CheckBoxList ID="cbWebs" runat="server" CssClass="ms-listheaderlabel" EnableViewState="true" /> </td></tr> </Template_InputFormControls> </wssuc:InputFormSection> <!-- *********************************************************** --> FileUpload control for Alerts Import file and Verify Button<!-- File name section --> <wssuc:InputFormSection Id="SelectFileSection" Title="Select Alerts File" Description="Browse to the alerts file you intend to import." runat="server"> <Template_InputFormControls> <wssuc:InputFormControl runat="server" LabelText="Name :"> <Template_Control> <span dir="ltr"> <asp:FileUpload ID="FileUpload1" runat="server" /> </span> </Template_Control> </wssuc:InputFormControl> <wssuc:InputFormControl LabelText="Before importing, you can check this alerts import file for detailed errors, warnings, and other information." runat="server"> <Template_Control> <asp:button class="ms-ButtonHeightWidth" runat="server" text="Verify" OnClick="ButtonVerifyClick" id="ButtonVerify"> </asp:button> </Template_Control> </wssuc:InputFormControl> <wssuc:InputFormControl runat="server"> <Template_Control> <div><asp:Label id="lblFileValidation" runat="server"/></div> </Template_Control> </wssuc:InputFormControl> </Template_InputFormControls> </wssuc:InputFormSection>
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
Preparations
- 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;
}
else
strMessages += “<br />Only <b>.xml</b> file is allowed, try again!<br />”;
}
else
strMessages += “<br /><b>Select Alerts File: </b>You must select a valid alerts import file.<br />”;
Preparations
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.ReadXml(alertsFile);
ds.Locale = System.Globalization.CultureInfo.CurrentUICulture;
// Read xml document
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(alertsFile);
// Keep a list of selected webs in an arraylist
ArrayList selectedWebsList = new ArrayList();
foreach (ListItem item in cbWebs.Items)
if (item.Selected)
selectedWebsList.Add(item.Text);
- 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
zieglers


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!
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.
Best,
zieglers