SPAlert And SPView

I am developing a news module for SharePoint 2010 and we wanted to create a list alert based on a list view, for each channel / folder … but we needed to do that programmatically

I surfed the internet looking for a solution or a code explaining how to do that but without any luck

so i decided to investigate the properties of the out of the box alert object and see what special values does it

have that makes it connected with the list view


But still that didn’t had any indications where is the alert object is associated with the list view

i only had one last place to look at and that was the SPALert properties bag … and yes there the connection to the list view happens

I made that custom code for creating an SPALert with the caml filters of the default list view

public static bool CreateAlert(string folderName)

{

 return DoNewsAction<bool>((web) =>

{

web.AllowUnsafeUpdates = true; 

SPList list = web.Lists[ValueHelper.NewsListName];

 SPFolder folder = list.RootFolder.SubFolders[folderName];

 if (folder == null|| string.IsNullOrEmpty(folder.ServerRelativeUrl)) return false;

 SPAlert alert = web.Alerts.Add();

 alert.AlertFrequency = SPAlertFrequency.Daily;

 alert.AlertType = SPAlertType.List;

 alert.AlwaysNotify = false;

 alert.DeliveryChannels = SPAlertDeliveryChannels.Email;

 alert.EventType = SPEventType.Modify;

 alert.List = list;

 string filter = string.Format(@"<Or><Eq><FieldRef Name='ItemFullUrl'/><Value type='string'>{0}</Value></Eq><BeginsWith><FieldRef Name='ItemFullUrl'/><Value type='string'>{0}/</Value></BeginsWith></Or>", folder.ServerRelativeUrl.TrimStart('/'));

 string viewQuery = list.DefaultView.Query;

 if (viewQuery.Contains("<Where>") && viewQuery.Contains("</Where>"))

 {

 viewQuery = viewQuery.Substring(viewQuery.IndexOf("<Where>") + "<Where>".Length, viewQuery.LastIndexOf("</Where>") - (viewQuery.IndexOf("<Where>") + "<Where>".Length));

 alert.Filter = string.Format(@"<And>{0}{1}<And>", filter, viewQuery);

 }

 else

 {

 alert.Filter = filter;

 }

 //use a view filtering option

 alert.Properties.Add("filterindex", "4");

 //the list view to associate with the alert item

 alert.Properties.Add("viewid", list.DefaultView.ID.ToString("D"));

 //the path of the folder to be alerted on

 alert.Properties.Add("filterpath", string.Format("{0}/", folder.ServerRelativeUrl.TrimStart('/')));

 //alert me only if item was modified in that view

 alert.Properties.Add("eventtypeindex", "2");

 //send sms alert

 alert.Properties.Add("sendurlinsms", "False");

 //the mobile view url

 alert.Properties.Add("mobileurl", string.Format("{0}/_layouts/mobile/ ", web.Url));

 //the site collection url

 alert.Properties.Add("siteurl", web.Site.Url);

 //the default item display form

 alert.Properties.Add("dispformurl", list.DefaultDisplayFormUrl);

 alert.Title = string.Format("{0} : News", folderName);

 alert.User = web.CurrentUser;

 alert.Update(true);

 return true;

 });

 }

That took some time to understand :)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


1 Comment

  1. Cool, thanks alot that dispformurl rescued me to amend the task change link url!

 

View Abdel-Rahman Awad's profile on LinkedIn

Archives

 

Rss Feed Tweeter button Facebook button Linkedin button Delicious button Digg button