Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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??

share|improve this question
    
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
add comment

2 Answers

up vote 3 down vote accepted

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();
share|improve this answer
    
Yeah I updated but not working. Even I removed that if condition... even it is not working –  Mihir Aug 7 '12 at 14:12
    
@Mihir I've updated my answer with more details, hopefully this is helpful to you. –  TZHX Aug 7 '12 at 14:14
    
@TZHX... I too just done this and my program runs successfully. Thanks for your patience... :) –  Mihir Aug 7 '12 at 14:21
add comment

But at the end of all operations do you do?

listitem.Update();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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