0

I'm using SharePoint 2013 on-premise with Nintex 2013. I have a list (List A) that has a workflow attached which is associated to a 2010 Task list, (Task List A). I have a requirement to update an item in List A when the itemAdded or itemUpdated event fires on 'Task List A'. I need to copy the contents of the Task lists 'Assigned To' (person) column to the List A 'Assigned To' (text) Column. I've set the feature to attach to the Task List A list only by using "" in the Elements.xml file. I verified that the Event Receiver is attaching to the task list by using SharePoint Manager, however no update is being made. I have not written SP Code in a while so I'm likely missing something pretty simple.

My code for the itemUpdated is as follows:

        public override void ItemUpdated(SPItemEventProperties properties)
    {

        base.ItemUpdated(properties);       

        string itemRC = "";
        SPWeb web = properties.OpenWeb();

        //get item id from Task List field
        SPList Tasklist = web.Lists[properties.ListId];
        SPListItem item = properties.ListItem;
        itemRC = item["RelatedContent"].ToString();
        //get the current assigned to value            
        string itemAssignedTo = item["Assigned To"].ToString();

        //pull the ID value of the Item from the RelatedContent QS
        string stritemID = itemRC.Substring(itemRC.LastIndexOf("=") + 1);
        Int32 intItemID = Int32.Parse(stritemID);                            
        //get related item list
        SPList itemList = properties.Web.Lists["Lists/List A"];
        //get other list item to update with the value from the current list
        try {
            SPQuery query = new SPQuery();                
            query.Query = "<Where><Eq><FieldRef Name='Id'/><Value Type='Number'>" + intItemID + "</Value></Eq></Where>";
            SPListItemCollection items = itemList.GetItems(query);
            SPListItem itemListItem = items[0];
            string AssignedToInternalName = itemListItem.Fields["Assigned To"].InternalName;
            itemListItem[AssignedToInternalName] = itemAssignedTo;


            properties.Web.AllowUnsafeUpdates = true;
            EventFiringEnabled = false;
            itemListItem.Update();
            EventFiringEnabled = true;
            properties.Web.AllowUnsafeUpdates = false;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
2
  • its better to debug the code by hitting the breakpoints. you will get exact reason behind it. Commented Sep 12, 2016 at 19:55
  • Hi Vishal. For some reason I am not able to hit breakpoints. I'm using VS2015 Professional and enabling the feature after attaching to the w3wp processes. The message on the actual breakpoint bullet in VS is something like 'Breakpoint will not be hit'.
    – Arale
    Commented Sep 17, 2016 at 13:56

2 Answers 2

0

There are few issues in the code. Below updated code might work.

public override void ItemUpdated(SPItemEventProperties properties)
   {
       base.ItemAdded(properties);
       string itemRC = "";
       try
       {

           SPWeb web = properties.OpenWeb();

           //get item id from Task List field
           SPList Tasklist = web.Lists[properties.ListId];
           SPListItem item = properties.ListItem;
           string stritemID = item["Workflow Item ID"].ToString();
           //get the current assigned to value            
           string itemAssignedTo = item["Assigned To"].ToString();
           SPListItem spItem = properties.Web.GetListItem(itemRC);

           //pull the ID value of the Item from the RelatedContent QS
           Int32 intItemID = Int32.Parse(stritemID) - 1;
           //get related item list
           SPList itemList = properties.Web.Lists["WFTest"];

           SPListItem itemListItem = itemList.Items[intItemID];
           string AssignedToInternalName = itemListItem.Fields["Assigned To"].InternalName;
           itemListItem[AssignedToInternalName] = itemAssignedTo.Substring(itemAssignedTo.LastIndexOf("#") + 1);

           properties.Web.AllowUnsafeUpdates = true;
           EventFiringEnabled = false;
           itemListItem.Update();
           EventFiringEnabled = true;
           properties.Web.AllowUnsafeUpdates = false;
       }
       catch (Exception ex)
       {
           throw ex;
       }
   }
0

The first issue I was having was my code wouldn't break. I updated my VS2015 to update 3 and also insured that deployment wasn't activating the feature. This has to be disabled on the Project level (Active Deployment Configuration set to 'No Activation') AND on the Feature level (Activate on Default set to False). Once these things were done I was then able to hit my breakpoints. This was the crux of the issue and once resolved I was able to see that the field names I was using were incorrect.

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.