Tuesday, May 26, 2009

Difference between methods SPListItem.Update() and SPListItem.SystemUpdate()

There are two methods provided by SharePoint object model to update a list item.
i) Update()
ii) SystemUpdate().
Both are working fine to update a list but there is some difference between both methods. Let’s see

SPListItem.Update () : Updates the database with changes made to the list item.

SPList list = web.Lists["myList"];
SPListItem item = list.Items[0];
item["myField"] = "my value";
item.Update();


This works fine and update particular list item. Basically this is creating new version of that list item. It update ‘Modified’ and ‘Modified by’ value as well.
But in some cases where no need to create new version of list item like during migration or some where there is no need to update modified date and modified by. For that situation function ‘SystemUpdate()’ works fine.

SPListItem.SystemUpdate () : Updates the database with changes made to the list item, without effecting changes in the Modified or Modified By fields.

SPList list = web.Lists["myList"];

SPListItem item = list.Items[0];
item["myField"] = "my value";
item.SystemUpdate();

SystemUpdate method ignore changing modified date and modifier fields.
item.SystemUpdate(false);
If Argument 'false' use with this method than it tells that no new versions are expected.

Monday, May 18, 2009

Sending mail in SharePoint [c# ]

We can send mail to any where with the help of c# custom code. Below given code use setting from ‘Outgoing e-mail setting’. This is out of box functionality of SharePoint. Therefore before using this code, configure this from Central Administration.

Libraries which you have to add above the code.
using System.Net.Mail;
using Microsoft.SharePoint;

Below code shows functionality to send mail.

public void SendMail()
{
try
{
Microsoft.SharePoint.Administration.SPGlobalAdmin globAdmin = new Microsoft.SharePoint.Administration.SPGlobalAdmin();
MailMessage objEmail = new MailMessage(FromMail, ToMail,Subject,Body);
objEmail.IsBodyHtml = true;
objEmail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient(globAdmin.OutboundSmtpServer);
smtp.Send(objEmail);
}
catch
{
throw;
}
}