Client Object Model Doesn’t Support Creating SharePoint 2013 Alerts!!

One of things that I looked at passionately thought it might change to the better is the API for SharePoint 2013 Alerts but unfortunately nothing new from 2010!

I had a thread on msdn last year http://social.technet.microsoft.com/Forums/sharepoint/en-US/43d801d4-0e89-4ec5-9d6c-b36f8106fc0d/how-to-create-and-delete-alerts-using-client-object-model-or-even-web-services

The problem is creating SharePoint Alert in client object model.
In Client Object Model, there is very little that you can achieve when it comes to alerts.
There is a ChangeAlert class, but it works on existing SPAlerts. But other than this, I could not really find any other information. http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.changealert.aspx

1

And someone replied with another idea is to call Alerts Web Service in JavaScript, ya but it doesn’t do anything more than what Client Object Model does!! http://msdn.microsoft.com/en-us/library/websvcalerts.alerts_methods

2

So why I wanted to create & delete Alerts from Client Side:
1- Because SharePoint Apps doesn’t support server side code.
2- Because I needed something fast that doesn’t require deployment & IISRESET.
And I also had a good idea which is calling the “New Alert” SharePoint OOB dialog that also will give user options for when to alert the user and on what.

 To open it, from the ribbon in the library click “Alert Me” then click “Set alert on this list”, then dialog will be opened.

3

4

Now I really wanted to call this “New Alert” dialog from a hyperlink with bell icon.
So I did a web part shows the current logged in user what lists or libraries the user had subscribed for it.
It will show gold bell icon beside the list name; which means you subscribed for this list.
If there is silver color bell beside the list name; this means you didn’t subscribe to this list. To subscribe, you can click the list name, it will popup the “New Alert” SharePoint OOB model dialog. Also in this dialog you will have many options of when to receive alerts and on what changes exactly.

In the Tool Part of the Web Part, user can select the lists on current site that the user have permission to only to see it displayed in the web part to subscribe to it.

5

To get the URL for this “New Alert” dialog, when you open it from any list or library as mentioned above, just right click;

Alerts

This is what we get http://sp2013dev/_layouts/15/SubNew.aspx?List=%7B79C60817%2D2CDC%2D4636%2D9302%2D80A35AEB85EA%7D&Source=http%3A%2F%2Fsp2013dev%2FLists%2FCalendar%2Fcalendar%2Easpx&IsDlg=1

Basically this is the URL http://sp2013dev/_layouts/15/SubNew.aspx then there is query strings but we need only one query string to give it to this URL, which is the list ID, we don’t care about the “Source”

In the Tool Part class I added a CheckBoxList to select multiple lists, the display is the List Title and the value is its GUID; like this,

foreach (SPList list in lists)
{
if (!list.Hidden)
chkList.Items.Add(new ListItem(list.Title, list.ID.ToString()));
}

Then I added the CheckBoxList to a dictionary in the Tool Part [I did this for a reason that was required, let me remember and update the post] just remember I am using this dictionary to display the list names.

Alerts

On the link call the “New Alert” OOB SharePoint dialog from JS, here is the function,

function CallAlert(ListID) {
var options = SP.UI.$create_DialogOptions();
var uri = “/_layouts/SubNew.aspx?List=” + ListID;
options.url = encodeURI(uri);
options.height = 700;
options.width = 650;
options.dialogReturnValueCallback = Function.createDelegate(
null, CloseCallback);
void (SP.UI.ModalDialog.showModalDialog(options));
}

I am calling this function from server side code,
foreach (var dic in DicList)
{
if (web.Lists[new Guid(dic.Key)].DoesUserHavePermissions(SPContext.Current.Web.CurrentUser, SPBasePermissions.ViewListItems))
{
if (!alertNames.ContainsValue(dic.Value))
{
writer.Write(AlertResource.GetLocalizedString(DIVFOREACH, CultureInfo.CurrentUICulture.LCID));
writer.Write(“<a class=’divforeach’ href=javascript:CallAlert(‘” + dic.Key + “‘)><img class=’imgAlert’ src=’//www.sharepointeurope.com/Assets/AlertsAssets/spalertdimmed.png’ style=’border:none’/>” + dic.Value + “</a><br/>”);
writer.Write(“</div>”);
}
}
}

In the above code, I am calling the method “DoesUserHavePermissions()” cause in case user doesn’t have permission to this list, it won’t be displayed. Then I am looping using foreach around the CheckBoxList to see what is selected from CheckBoxList that was added to the dictionary in the Tool Part.
Also in the above code, I am adding the image “Bell” icon beside the hyperlink.

The Project is on CodePlex https://spalertme.codeplex.com/

………………………………………………. That’s it.

Don’t hesitate to contact me for any questions: mai_omar_86@live.com
EnjoooOOOooy SharePointing!

If you have any questions for Mai Omar please leave a comment below.

Mai Omar was a speaker at ESPC13. Why not check out her presentation on ‘What’s new in SharePoint 2013 Designer‘ – Download Now>>

Share this on...

Rate this Post:

Share: