# How to catch the feature activate time

I am trying to write one program such that, when ever a feature is activated then it should display the activated time in the sharepoint list. for that i written following code.

public class Feature1EventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.

{
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb;

SPList list = web.Lists["Features Tracker"];
if (list == null)
{
Guid listid = web.Lists.Add("Features Tracker", "Trakcs the feature activation time", SPListTemplateType.GenericList);
SPView myview = web.Lists[listid].DefaultView;
myview.Update();
}
listitem["Activated Time"] = DateTime.Now.TimeOfDay;

}
}


I created another feature with same functionality. Now there is no error. But, time stamp is not inserted into the "Features Tracker" list. Any solution??

• This code appears to compile fine, and you've not given any error. – TZHX Aug 7 '12 at 13:52
• Code is fine. But while deploying it is giving error – Mihir Aug 7 '12 at 13:54
• It's impossible to read the text in that image, can you copy the error text? – TZHX Aug 7 '12 at 13:55
• @TZHX Error message added – Mihir Aug 7 '12 at 13:59

You need to use the Update() method on the SPListItem object.

Also, you want the line:

listitem["Activated Time"] = DateTime.Now.TimeOfDay;


to be simply:

listitem["Activated Time"] = DateTime.Now;


otherwise you'll get an unhandled exception.

This is the code I have, which works (the method being the same one you've selected):

SPSite site = SPContext.Current.Site;
SPWeb web = site.RootWeb;
SPList list;
try
{

list = web.Lists["Features Tracker"];
}
catch (ArgumentException)
{
list = null;
}
if (list == null)
{
Guid listid = web.Lists.Add("Features Tracker", "Trakcs the feature activation time", SPListTemplateType.GenericList);
SPView myview = web.Lists[listid].DefaultView;
myview.Update();
list = web.Lists["Features Tracker"];
}

• Yeah I updated but not working. Even I removed that if condition... even it is not working – Mihir Aug 7 '12 at 14:12
listitem.Update();