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

Update item List from Task List EventReceiver

$
0
0

I'm using VS 2012 Professional with SharePoint 2013 Enterprise. Our workflows are written in Nintex 2013 which leverages the 2010 Task content type.

I have a text column on a custom list that I need to update with the "Assigned To" value from its associated Task list. We have been handling this in the Nintex Workflow but when a task is automatically escalated to a user after a period of time has elapsed, it is not done by a workflow and so the logic to copy the value to the Item list is not executed. 

I am able to update the value with similar code as below in a console application, but when it is moved to an Event Receiver the item list is never updated. Below is the code from the ItemUpdated Event and I hope to do something similar on the ItemAdded event. This would all be much easier if I were able to get the ER code to break in debug mode but haven't had much luck

 public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);

            string itemRC = "";
            Int32 intItemID = 0;
            string stritemID = "";

            using (SPSite site = new SPSite(properties.WebUrl)) {
                using (SPWeb web = site.OpenWeb()){
                    SPList list = web.Lists["eInvoice Tasks"];
                    SPQuery query = new SPQuery();

                    string TaskID = "";
                    string strAssignedto = "";
                    string Assignee = "";
                    if (properties.AfterProperties["Assigned To"] != null) {
                        strAssignedto = properties.AfterProperties["Assigned To"].ToString();
                        Assignee = strAssignedto.Substring(strAssignedto.LastIndexOf('#'));
                    }


                    if (properties.AfterProperties["Related Content"] != null)
                    {
                        itemRC = properties.AfterProperties["Related Content"].ToString();
                        stritemID = itemRC.Substring(itemRC.LastIndexOf("=") + 1);
                        intItemID = Int32.Parse(stritemID);
                    }

                    query.Query = "<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>" + TaskID + "</Value></Eq></Where>";

                    string rc = "";
                    string strid = "";
                    Int32 equalsign;
                    Int32 comma;
                    Int32 hash;
                    string user = "";
                    string assignee = "";

                    do
                    {
                        SPListItemCollection items = list.GetItems(query);
                        SPList elist = web.Lists["eInvoice"];
                        SPQuery equery = new SPQuery();
                        foreach (SPListItem listItem in items)
                        {
                            rc = listItem["Related Content"].ToString();
                            /*Related Content consists of the following format
                             URL/Lists/eInvoice/DispForm.aspx?ID=1, 00001
                             The URL of the eInvoice Item and the Title of the eInvoice Item
                             */

                            // Parse the eInvoice ItemID
                            equalsign = rc.IndexOf("=");
                            comma = rc.IndexOf(",");
                            strid = rc.Substring(equalsign + 1, (comma - equalsign) - 1);

                            //Parse the Assignee
                            user = listItem["Assigned To"].ToString();
                            hash = user.LastIndexOf('#');
                            assignee = user.Substring(hash + 1);

                            equery.Query = "<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>" + strid.ToString() + "</Value></Eq></Where>";
                            equery.ViewFields = "<FieldRef Name='Assigned To'/>";
                            SPListItemCollection eitems = elist.GetItems(equery);
                            foreach (SPListItem item in eitems)
                            {
                                item["Assigned To"] = assignee;
                                EventFiringEnabled = false;
                                item.Update();
                                EventFiringEnabled = true;
                            }
                        }
                        query.ListItemCollectionPosition = items.ListItemCollectionPosition;
                    } while (query.ListItemCollectionPosition != null);
                }
            }
        }


Love them all...regardless. - Buddha


Viewing all articles
Browse latest Browse all 7589

Trending Articles



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