سؤال

For some reason I was working on an event receiver when it stopped working. I am updating it and deploying from Visual Studio 2010 and it was previously working but it stopped when I was doing lookup fields. It is not working any more even if I remove the portion I just added. any ideas. thank you.

    `/// An item was added.
    /// </summary>
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        string fname = Convert.ToString(properties.AfterProperties["Title"]);
        string Cdate = Convert.ToString(properties.AfterProperties["CDate"]).Substring(2, 2);
        // ---- Lookup fields ----- 
        String lookupZF = "Office";
        String ZF = Convert.ToString(properties.ListItem[lookupZF]);
        SPFieldLookupValue ZFValue = new SPFieldLookupValue(ZF);
        // ------ End of lookup fields ----- 
        string FName = fname + Cdate; // + ZFValue;

        SPSecurity.RunWithElevatedPrivileges(delegate()
                       {
                           using (SPSite site = new SPSite(properties.Web.Site.ID))
                           {
                               using (SPWeb web = site.OpenWeb())
                               {
                                   web.AllowUnsafeUpdates = true;
                                   SPList CurrentList = web.Lists[properties.ListId];
                             SPListItem Litem = CurrentList.GetItemById(properties.ListItemId);

                                   Litem["Description"] = "update working ...!";
                                   Litem["Prop No."] = FName;

                                   Litem.Update();
                                   CurrentList.Update();

                                   web.AllowUnsafeUpdates = false;
                               }
                           }
                       });
    }`
هل كانت مفيدة؟

المحلول 2

I had to use dateTime then get the year. for some reason I was getting empty or null string that stopped the excution of the code. here is what worked:

DateTime Cdate = DateTime.Parse(properties.ListItem["Created"].ToString());
string SDate = Cdate.Year.ToString().Substring(2, 2);

نصائح أخرى

I would suggest to check if the feature is activated in Manage site features after you deploy your solution.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top