October 07, 2008

Visual Studio 2010 & .NET Framework 4.0 Overview

Microsoft has published an overview of the next generation of Visual Studio and .NET Framework.

Here is the link:
http://msdn.microsoft.com/en-us/vstudio/products/cc948977.aspx

October 01, 2008

How to create SharePoint alerts for lists or list items programmatically

This article will explain how to create SharePoint alert like in the following SharePoint Alert Me page. The illustrated page is the page displayed for a list. In the case of a list item, it's exactly same except that there are no Change Type and Send Alerts for These Changes sections.



Let's imagine a little console application to play the trick...

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
 
namespace UsageInformation
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite mySiteCollection = new SPSite("http://myserver/myspsite"))
            {
                using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))
                {
                    //Code will be put here
                }
            }
        }
    }
}

The first thing to create the alert is to use the Add method of the Alerts property of the SPUser class. This method return an instance of the SPAlert class.

string myUserLogin = @"MyDomain\MyUser";
SPUser myUser = mySite.Users[myUserLogin];
SPAlert newAlert = myUser.Alerts.Add();

To set the title of the alert, you have to use the Title property of the SPAlert class.

newAlert.Title = "My Title";

The following part of the screen corresponds to the Title property.



If the alert is an alert for a list, you have to specify it in using the AlertType property (enumeration) and to set the List property which must be an instance of the SPList class.

SPList myList = mySite.Lists["My List Name"];
newAlert.AlertType = SPAlertType.List;
newAlert.List = myList;

If the alert is an a alert for a list item, you have to specify it in using the AlertType property (enumeration) and to set the Item property must be an instance of the SPListItem class.

SPListItem myListItem = mySite.Lists["My List Name"].Items.GetItemById(1);
newAlert.AlertType = SPAlertType.Item;
newAlert.Item = myListItem;

Now you have to set the AlertTemplate property. This property must be an instance of the SPAlertTemplate class. Typically, you have to set this property with the template of the list (alert for a list) or with the template of the list containing the list item (alert for a list item).

newAlert.AlertTemplate = myList.AlertTemplate;

or

newAlert.AlertTemplate = myListItem.ParentList.AlertTemplate;

The next property to set is the EventType property (enumeration). This property correspond to Change Type section.

newAlert.EventType = SPEventType.All;

or

newAlert.EventType = SPEventType.Add;

or

newAlert.EventType = SPEventType.Modify;

or

newAlert.EventType = SPEventType.Delete;

or

newAlert.EventType = SPEventType.Discussion;

The following part of the screen corresponds to the EventType property.



The next property to set is the Filter property. This property corresponds to the Send Alerts for These Changes section. In fact, this is string which must be a CAML query.

If Anything change is selected:

newAlert.Filter = string.Empty;

If Someone else changes an item is selected:

newAlert.Filter = string.Format("<Query><Neq><Value type=\"string\">{0}</Value><FieldRef Name=\"Editor/New\"/></Neq></Query>", myUser.Name.ToLower());

If Someone else changes an item created by me is selected:

newAlert.Filter = string.Format("<Query><And><Or><Eq><Value type=\"string\">{0}</Value><FieldRef Name=\"Author/New\"/></Eq><Eq><Value type=\"string\">{0}</Value><FieldRef Name=\"Author/Old\"/></Eq></Or><Neq><Value type=\"string\">{0}</Value><FieldRef Name=\"Editor/New\"/></Neq></And></Query>", myUser.Name.ToLower());

If Someone else changes an item last modified by me is selected:

newAlert.Filter = string.Format("<Query><And><Eq><Value type=\"string\">{0}</Value><FieldRef Name=\"Editor/Old\"/></Eq><Neq><Value type=\"string\">{0}</Value><FieldRef Name=\"Editor/New\"/></Neq></And></Query>", myUser.Name.ToLower());

The following part of the screen corresponds to the Filter property.



The last properties to set are the AlertFrequency and the AlertTime. Note that the AlertTime must be set only if the AlertFrequency is different than Immediate. The AlertTime property is the next time the alert will be executed and the AlertFrequency is the frequence (enumeration): Immediate, Daily or Weekly. These properties correspond to the When to Send Alerts section.

Immediate:

newAlert.AlertFrequency = SPAlertFrequency.Immediate;

Daily and the wanted hour (e.g. 10) is > hour of today:

newAlert.AlertFrequency = SPAlertFrequency.Daily;
newAlert.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 10, 0, 0);

Daily and the wanted hour (e.g. 10) is <= hour of today:

newAlert.AlertFrequency = SPAlertFrequency.Daily;
newAlert.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 10, 0, 0);
newAlert.AlertTime = newAlert.AlertTime.AddDays(1);

Weekly and the wanted day (e.g. friday) is = today and the wanted hour (e.g. 10) is > hour of today:

newAlert.AlertFrequency = SPAlertFrequency.Weekly;
newAlert.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 10, 0, 0);

Weekly and the wanted day (e.g. friday) is = today and the wanted hour (e.g. 10) is <= hour of today:

newAlert.AlertFrequency = SPAlertFrequency.Weekly;
newAlert.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 10, 0, 0);
newAlert.AlertTime = newAlert.AlertTime.AddDays(7);

Weekly and the wanted day (e.g. sunday) is > today (e.g. friday):

newAlert.AlertFrequency = SPAlertFrequency.Weekly;
newAlert.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 10, 0, 0);
newAlert.AlertTime = newAlert.AlertTime.AddDays(2);

Weekly and the wanted day (e.g. wednesday) is < today

newAlert.AlertFrequency = SPAlertFrequency.Weekly;
newAlert.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 10, 0, 0);
newAlert.AlertTime = newAlert.AlertTime.AddDays(5);

The following part of screen corresponds to these properties.



To finish, you have to call the Update method of the instance of SPAlert.

newAlert.Update(true);