Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Itemadded event reciever of list not firing when item added through code

$
0
0

I am adding item to a list when one metadata in a document library is changed. THis is achieved using itemupdating event in the doc library. here in doc library the item is changed using the internet explorer and working fine.

public override void ItemUpdating(SPItemEventProperties properties)
        {
          
            try
            {
                this.DisableEventFiring();
               
                liDok = properties.ListItem;
                //Checks whether the event is fired for ISO document library else event will do nothing
                if (liDok.ParentList.Title == "ISO Documents")
                {
                    web = properties.OpenWeb();
                    site = web.Site;

                    //Checks the library has column named documentretired
                    if (liDok.Fields["DocumentRetired"] != null)
                    {
                        //Checks whether the field changed is the DocumentRetired column
                        if (properties.BeforeProperties["DocumentRetired"].ToString() !=
                            properties.AfterProperties["DocumentRetired"].ToString() &&
                            properties.AfterProperties["DocumentRetired"].ToString().ToLower()=="yes")
                        {
                            CreateListItem(properties, "Retire", "DocumentRetired");
                        }
                    }
}
}
}

private void CreateListItem(SPItemEventProperties properties,string sActivity,string sFieldName)
       {
           try
           {
               web.AllowUnsafeUpdates = true;
               site.AllowUnsafeUpdates = true;

               //Assigns the changed value to the column
               liDok[sFieldName] = properties.AfterProperties[sFieldName];
               // Fetch the List
               SPList list = web.Lists["TrackActivities"];

               //Add a new item in the List
               SPListItem itemToAdd = list.Items.Add();
               itemToAdd["Title"] = liDok.File.Name;
               itemToAdd["DocumentID"] = properties.ListItemId.ToString();
               itemToAdd["Activity"] = sActivity;
               itemToAdd.SystemUpdate();
           }

But the itemtoadd.systemupdate was supposed to fire the event receiver atached to the list "Taskactivities", but it is not firing. THe same code is getting fired when list items are added manually using IE

public override void ItemAdded(SPItemEventProperties properties)
        {
            SPWeb web = null;
            SPSite site = null;
            try
            {
                if (properties.ListTitle.ToString().ToLower() == "trackactivities")
                {
                    this.DisableEventFiring();
                    web = properties.OpenWeb();
                    site = web.Site;
                    web.AllowUnsafeUpdates = true;
                    site.AllowUnsafeUpdates = true;
                    SPList dlISODok = web.Lists["ISO Documents"];
                    SPListItem liDokItem = dlISODok.Items.GetItemById(Convert.ToInt32(properties.ListItem["DocumentID"].ToString()));
                    if (liDokItem != null)
                    {


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>