2

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.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            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);
                web.Lists[listid].Fields.Add("Activated Time", SPFieldType.DateTime, true);
                SPView myview = web.Lists[listid].DefaultView;
                myview.ViewFields.Add("Activated Time");
                myview.Update();
            }
                SPListItem listitem = list.Items.Add();
                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??

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

2 Answers 2

3

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);
    web.Lists[listid].Fields.Add("Activated Time", SPFieldType.DateTime, true);
    SPView myview = web.Lists[listid].DefaultView;
    myview.ViewFields.Add("Activated Time");
    myview.Update();
    list = web.Lists["Features Tracker"];
}
SPListItem listitem = list.Items.Add();
listitem["Activated Time"] = DateTime.Now;
listitem.Update();
3
  • Yeah I updated but not working. Even I removed that if condition... even it is not working
    – Mihir
    Aug 7, 2012 at 14:12
  • @Mihir I've updated my answer with more details, hopefully this is helpful to you.
    – TZHX
    Aug 7, 2012 at 14:14
  • @TZHX... I too just done this and my program runs successfully. Thanks for your patience... :)
    – Mihir
    Aug 7, 2012 at 14:21
1

But at the end of all operations do you do?

listitem.Update();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.