none
Help working with alerts.asmx webservice

    Question

  • Hi Everyone,

    I am trying to use the alerts.asmx webservice to get back a list of user alerts, and then to be able to delete the alerts, but so far I havn't been able to get anything back. My code so far:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using ConfirmationModal.AlertsWebServiceReference;

    namespace ConfirmationModal.TestModal
    {
        public partial class TestModalUserControl : UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //ConfirmationModal.AlertsWebServiceReference.Alerts alertService = new ConfirmationModal.AlertsWebServiceReference.Alerts();
                //alertService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                //Web_Reference_Folder_Name.AlertInfo allAlerts = alertService.GetAlerts();
                //MessageBox.Show("Server: " + allAlerts.AlertServerName + ControlChars.Lf + "URL: " + allAlerts.AlertServerUrl + ControlChars.Lf + "Web Title: " + allAlerts.AlertWebTitle + ControlChars.Lf + "Number: " + allAlerts.Alerts.Length.ToString());

                ConfirmationModal.AlertsWebServiceReference.Alerts alertService = new ConfirmationModal.AlertsWebServiceReference.Alerts();
                alertService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                ConfirmationModal.AlertsWebServiceReference.AlertInfo alertInfo = alertService.GetAlerts();
                ConfirmationModal.AlertsWebServiceReference.Alert[] alerts = alertInfo.Alerts;
                string[] delString = new string[alerts.Length + 1];
                int i = 0; for (i = 0; i <= delString.Length - 1; i++)
                {
                    delString[i] = alerts(i).Id.ToString();
                }
                try
                {
                    alertService.DeleteAlerts(delString);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }
    }

    So I found this code out on the web, but to tell the truth, this part:ConfirmationModal.AlertsWebServiceReference.Alerts

    Dosn't work at all. Not sure what is really going on here, but could really use some help.

    Any help at  all would be greatly appreciated!!

    Thanks in advance


    Best regards, Mike



    • Edited by dzitam Thursday, January 03, 2013 11:23 PM
    Thursday, January 03, 2013 7:44 PM

Answers

  • For anyone that is interested, I figured it out:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Data.SqlClient;
    using System.Data;
    using System.Collections.Specialized;
    using System.Collections;
    using Microsoft.SharePoint;

     

    namespace ConfirmationModal.TestModal
    {
        public partial class TestModalUserControl : UserControl
        {
            String site = SPContext.Current.Web.Url;

            protected void Page_Load(object sender, EventArgs e)
            {
                GridView1.DataSource = this.GetAlerts();
               // GridView1.Columns[2].Visible = true;
                GridView1.DataBind();
                //GridView1.Columns[2].Visible = false;
            }

            private DataTable GetAlerts()
            {
                using (SPSite site = new SPSite(this.site))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPUser user = SPContext.Current.Web.CurrentUser;

                        DataTable table = new DataTable();
                        table.Columns.Add("Title", typeof(string));
                        table.Columns.Add("AlertID", typeof(string));

                        //SPUser user = web.EnsureUser(@"domainName\userName");

                        SPAlertCollection alertColl = user.Alerts;
                        DataRow row;

                        foreach (SPAlert alert in alertColl)
                        {
                            try
                            {
                                row = table.Rows.Add();
                                row["Title"] = alert.Title.ToString();
                                row["AlertID"] = alert.ID.ToString(); //Hide


                                //table.Rows.Add(row);
                            }
                            catch (Exception ex)
                            {
                                ex.StackTrace.ToString();
                            }
                        }

                        return table;
                    }
                }
            }


    Best regards, Mike

    Wednesday, January 09, 2013 5:23 PM

All replies

  • Just out of curiosity, why are you trying to get the information from a web service in a user control code behind? Since you have access to the object model, wouldn't it be easier and more performant to use object model code?

    http://msdn.microsoft.com/en-us/library/bb897864(v=office.14).aspx

    "Use the Alerts property of either the SPUser or SPWeb class to return a SPAlertCollection object that represents the collection of alerts for the user or Web site. Use an indexer to return a single alert from the collection."


    Doug Hemminger http://www.sharepointdoug.com

    Friday, January 04, 2013 3:09 AM
  • Hi Doug,

    I was looking into doing it that way, but I couldn't find any code on the web to help me get started. Any chance you can help me out here??


    Best regards, Mike

    Friday, January 04, 2013 3:35 AM
  • Here is the code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    namespace Alerts
    {
    class Program
    {
    static void Main(string[] args)
    {
    using (SPSite site = new SPSite("http://serverName:22222/sites/TestSite/"))
    {
    using (SPWeb web = site.RootWeb)
    {
    SPUser user = SPContext.Current.Web.CurrentUser;
    //SPUser user = web.EnsureUser(@"domainName\userName");
    SPAlertCollection alertColl = user.Alerts;
    foreach (SPAlert alert in alertColl)
    {
    Console.WriteLine(alert.Title);
    }
    Console.ReadLine();
    }
    }
    }
    }
    }
    you might have to modify the code accordingly.the example is from console app you can get it from current user context


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Friday, January 04, 2013 7:25 AM
  • Great, this is working really well!

    However, I also need to be able to add checkboxes next to each alert that I get back in order to do a delete on each one of these alerts, any idea how I can do that? This is what I have so far:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;

     

    namespace ConfirmationModal.TestModal
    {
        public partial class TestModalUserControl : UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                using (SPSite site = new SPSite("http://serverName:22222/sites/TestSite/"))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPUser user = SPContext.Current.Web.CurrentUser;
                        //SPUser user = web.EnsureUser(@"domainName\userName");
                        SPAlertCollection alertColl = user.Alerts;
                        foreach (SPAlert alert in alertColl)
                        {
                          
                            alert.Title += "," + Label1.Text;

                            this.Label1.Text = alert.Title;
                      
                        }
                     
                    }
                }
            }
        }
    }


    Best regards, Mike

    Friday, January 04, 2013 5:27 PM
  • Great. You can create a collection and bind it to the gridview which will have a checkbox, the usual asp.net approach. check this out http://www.c-sharpcorner.com/uploadfile/syedshakeer/checkbox-in-gridview/

    Please mark appropriate answer as reply or vote as helpful, it might help others.


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Friday, January 04, 2013 5:35 PM
  • Um no this doesn't work at all. First of all, I can't connect via a SQLDataSource. Maybe you can supply me with some code to help me out here??

    Best regards, Mike

    Friday, January 04, 2013 10:36 PM
  • Hi Mike,

    you dont have to SQLDataSourcem you already have a collection, you can directly bind the collection to gridview

    http://mostlydevelopers.com/blog/post/2008/08/08/Bind-a-Collection-to-a-GridView.aspx

    Hope this helps. Please mark appropriate reply as answer or vote as helpful, this will help others..


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Monday, January 07, 2013 4:48 AM
  • No sorry, this doesn't help at all!! Can you please provide me with some code to help me out??

    Best regards, Mike

    Monday, January 07, 2013 6:26 PM
  • For anyone that is interested, I figured it out:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Data.SqlClient;
    using System.Data;
    using System.Collections.Specialized;
    using System.Collections;
    using Microsoft.SharePoint;

     

    namespace ConfirmationModal.TestModal
    {
        public partial class TestModalUserControl : UserControl
        {
            String site = SPContext.Current.Web.Url;

            protected void Page_Load(object sender, EventArgs e)
            {
                GridView1.DataSource = this.GetAlerts();
               // GridView1.Columns[2].Visible = true;
                GridView1.DataBind();
                //GridView1.Columns[2].Visible = false;
            }

            private DataTable GetAlerts()
            {
                using (SPSite site = new SPSite(this.site))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPUser user = SPContext.Current.Web.CurrentUser;

                        DataTable table = new DataTable();
                        table.Columns.Add("Title", typeof(string));
                        table.Columns.Add("AlertID", typeof(string));

                        //SPUser user = web.EnsureUser(@"domainName\userName");

                        SPAlertCollection alertColl = user.Alerts;
                        DataRow row;

                        foreach (SPAlert alert in alertColl)
                        {
                            try
                            {
                                row = table.Rows.Add();
                                row["Title"] = alert.Title.ToString();
                                row["AlertID"] = alert.ID.ToString(); //Hide


                                //table.Rows.Add(row);
                            }
                            catch (Exception ex)
                            {
                                ex.StackTrace.ToString();
                            }
                        }

                        return table;
                    }
                }
            }


    Best regards, Mike

    Wednesday, January 09, 2013 5:23 PM
  • great..

    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Wednesday, January 09, 2013 6:12 PM