Jaa


How to create SearchAlert Programmatically?

Note: Make sure that you have enabled search based alerts SSP-> Search Settings -> Search Based Alerts

    1: SPSite spSite = new SPSite("https://nishand:300/sites/PubSite/Search/");
    2: SPWeb web = spSite.OpenWeb();
    3: Query alertQuery = new KeywordQuery(spSite);
    4: alertQuery.QueryText = "Hello" // Keyword
    5: SearchAlert searchAlert = new SearchAlert(spSite, alertQuery); 
    6: searchAlert.InnerAlert.AlertFrequency = SPAlertFrequency.Daily; 
    7: searchAlert.InnerAlert.Title = "Alert#2"
    8: searchAlert.InnerAlert.User = web.Users["domain\\user"]; 
    9: searchAlert.Update();

How to check whether an alert named 'Alert#2' has been created?

  Go to Site Actions -> Site Settings - User Alerts -> Select 'user name' and click on 'update' button.

Comments

  • Anonymous
    July 16, 2008
    Have you ever tried to set frequency "Immediate" ....??  i have tried and its not working for SearchAlert... if you have any insight on this.. plz let me know asap... so your code would be something like this searchAlert.InnerAlert.AlertFrequency = SPAlertFrequency.Immediate*; Thanks, kp.

  • Anonymous
    December 03, 2012
    I tried this code .. Alerts has been created .. But it is not triggering any mail when an item is created .. Kindly help me .. Do i need to write any serach query ?? Thanks K7

  • Anonymous
    December 03, 2012
    The above code is working for daily alerts , but it is not for Immediate searchAlert.InnerAlert.AlertFrequency = SPAlertFrequency.Immediate is not working .. If anyone knows the solution plz do reply .