List Item not updating in event receiver
-
Thursday, May 03, 2012 8:33 AM
Hello all,
I have created an event receiver and a workflow(WF_LeaveApplication). I try to update task property in event receiver by the following way but i faced an error.
"Invalid field name. {1c5518e2-1e99-49fe-bfc6-1a8de3ba16e2} /sites/hr/LeaveApplication"
I have a field "Location" in the list but it still gives an above error. Any help will be highly appreciated, thanks.
Dim myHashTable As New Hashtable()
myHashTable("Location") = "California"
Dim selectedTaskList As SPList = web.Lists("LeaveApplication")
Dim newTask As SPListItem = selectedTaskList.Items.Add() ' Turn the new task item into a Workflow
Dim newTaskContentType As Microsoft.SharePoint.SPContentType = web.AvailableContentTypes("Workflow Task")
newTask("ContentTypeId") = newTaskContentType.Id ' Now the AlterTask method will work. (assume you've
Microsoft.SharePoint.Workflow.SPWorkflowTask.AlterTask(newTask, myHashTable, True)
All Replies
-
Thursday, May 03, 2012 8:46 AM
Hello,
Alter task is usually used to make changes to workflow task items, that will send notifications to workflow. From your case I don't see that the task is connected to workflow (since you are just creating it).
You could do the normal approach with .Update() method on newly created item, e.g. :
SPListItem newTaskItem = selectedTaskList.AddItem();
newTaskItem["ContentTypeId"] = newTaskContentType.Id; newTaskItem["Location"] = "California"; newTaskItem.Update();
P.S. I don't understand the updating part from the event receiver, in your code you are creating completely new item. Where is the item that this was triggered on?
Please don't forget to mark the post as Helpful if you find my comment useful and as Answer if it solved your problem.
- Edited by Mladen Tisaj, 'Ferovac' Thursday, May 03, 2012 8:47 AM added P.S. question
-
Thursday, May 03, 2012 9:05 AM
Thanks for the reply Mladen,
Scanario is that i have created some task properties (extended properties) like one of its extended property name is "Leavedays".
I want to update this extended property by using event receiver. Now you can say why dont i update directly from workflow. My logic does not fit there. So i have to change this task property from event receiver.
My code may be incorredt. If you understand my scanario you can please share with me the code, thanks.
-
Friday, May 04, 2012 12:36 PM
I would simply read the Extended properties and make sure that it returns the field and check the spelling as well. Use the following code to get all the extended properties of the task and see if you can read the Location property.
Hashtable workflowReadTaskProp = SPWorkflowTask.GetExtendedPropertiesAsHashtable(SPLISTITEM); String strComments = workflowReadTaskProp["comments"].ToString(); String strInstructions = workflowReadTaskProp["instructions"].ToString();
Replace SPLISTITEM with your ListItem object and replace "comments" and "instructions" with your properties.
If that code returns the same error then debug your code and once you execute line 1 hover your mouse over Hastable variable to check what fields it returns.
Edit : After double checking your code I am not sure what exactly you are doing. If I understand correctly you want to update the workflow task from event receiver. Then why are you creating a task yourself? I believe workflow will create a task in the task list and it will be of type Workflow Task. You simply have to grab that task item by using Properties.listitem in your event receiver and update the extended properties.
Amit
-
Friday, May 04, 2012 4:49 PM
Thanks Amit,
First of all i appreciate your accurate understanding :). And please don't look at my code as i am a newbie in SharePoint developing. I know you read my other threads also and now i think you understand well that what i am going to achieve and trying for different ways to achieve the same thing. I summarize what i am going to achieve.
I created a workflow for form library which simply creates a task plus some extended properties. I need to update these extended properties when a workflow item change (means if user make change/update on form after it uploaded). For that i needed two activities "OnTaskChanged" and "OnWorkflowItemChanged", but the problem was that only "OnTaskChanged" activity worked and "OnWorkflowItemChanged" not, and "OnWorkflowItemChanged" activity i needed to update extended property. So i decided to update extended property also through event receiver . And that's why i was creating parallel activity to achieve this which is not successful yet.
That's the whole story. I didn't test your code in previous thread on server. I will check it tomorrow in office. If you further assist me after understanding my complete scenario till testing, I shall be again thankful to you :).
-
Friday, May 04, 2012 7:03 PMI hear you.
I believe there are couple of issues for you here.
1) Using OnTaskChanged and OnWorkflowItemChanged activities in same workflow and be able to respond to both. I believe this is discussed in separate thread so please keep that thread updated in case someone else wants to share some insight they have full context of the problem.
2) Changing Task extended properties when someone updates the Form.
Now for this I think you will need to create ItemUpdated event receiver on your Form Library and from there you will need to find the task item(s) for the running workflow on that item. Once you have the task item(s) you will need to update extended properties. Is this how you are using your event receiver?
Can you please provide details as to how you are trying to update the extended properties from event receiver?Amit
-
Saturday, May 05, 2012 9:54 AM
Thanks amit,
I am sorry for late reply because of two days off at office, so replying at home. Both issues you discussed are my real problems. I failed to solve the first way that you describe. After that i came on the second way to update task item in the event receiver. My event receiver exactly debug and read the "ItemUpdating" event when anyone updates form. The task associates when i upload form in library. So whenever a user come and open this form again and make any change, "ItemUpdating" event receiver works perfectly. But here i dont know how to update the task item (extended property). So i need code to write in event receiver to update this task item. Hope you understand which i mentioned above and sorry i still did'nt test your code to verify the task items from following line of code below. may be then i can get the better understanding. I will be glad to here you more on this issue :). Hashtable workflowReadTaskProp = SPWorkflowTask.GetExtendedPropertiesAsHashtable(SPLISTITEM);
-
Monday, May 07, 2012 4:40 AM
Hello Amit,
I test the code to verify the extended properties of "LeaveApplication" list just now. But it is still giving the same error. Please see image below and please give me the solution to get rid this problem as soon as possible. My so much work stopped because of this. Just waiting for your reply.
- Edited by Marreena Monday, May 07, 2012 4:41 AM
-
Monday, May 07, 2012 7:04 PM
I think you are still doing it wrong... You are trying to get extended properties on your Form Library.. which is not right. You will need to get extended properties on your Task List...
I am assuming you have ItemUpdated event Receiver added to your Form Library. Try the following code and make necessary adjustments as needed.
public override void ItemUpdated(SPItemEventProperties properties) { // Create Workflow Manager object SPWorkflowManager workflowMgr = properties.Web.Site.WorkflowManager; // Get the active workflow on your current updated form (SPListItem) SPWorkflowCollection SPWFCollection = workflowMgr.GetItemActiveWorkflows(properties.ListItem); ///Ideally you will get only one workflow but you may have multiple and hence loop through them all foreach (SPWorkflow workflow in SPWFCollection) { // Check if the workflow state is running. if (workflow.InternalState == SPWorkflowState.Running) { // Get all the tasks associated with this running workflow foreach (SPWorkflowTask task in workflow.Tasks) { // Now you need to pick the specific task that you want to update and hence I have used e.g. Title Property. // If you have any other unique property to indentify your task then feel free to update the code. if (task.Title.Contains("YOUR TASK TITLE")) { // Now use Hashtable to update the task. // Replace Property1 and Property1 with your actual property name and update the values as well. // Add or remove fields as needed. Hashtable workflowUpdateTaskProp = new Hashtable(); workflowUpdateTaskProp["Property1"] = "Property1 Value"; workflowUpdateTaskProp["Property2"] = "Property2 Value"; properties.Web.AllowUnsafeUpdates = true; SPWorkflowTask.AlterTask(task, workflowUpdateTaskProp, true); properties.Web.AllowUnsafeUpdates = false; } } } } }
Amit
- Marked As Answer by Marreena Tuesday, May 08, 2012 11:10 AM
-
Tuesday, May 08, 2012 11:18 AM
Thanks amit,
You saved me. Your code works perfectly. I got it what i needed. But your code through me in another problem. Please look at the new thread, really thankful for your every help and support.
Problem is that when looping on each tasks i need to get only those tasks which status is "In Progress". How can i get the task status where you write if (task.Title.Contains("YOUR TASK TITLE")).
Please look this matter in the following thread.
http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/4303ed58-d0f7-4db1-a3cc-d1649a928079
http://social.technet.microsoft.com/Forums/en-us/sharepoint2010programming/thread/75654107-9ff9-4a64-b477-b08046b2bcaa
-
Monday, June 18, 2012 6:48 AM
Hello Amit,
I need your kind help regarding your last post. I also want to update the task whom it was assigned. For example Task was assigned to user ABC. Now i want to update this "AssignedTo" value to XYZ user. Can i update exactly in the code in your last post ??? Please help i stuck here and dont know what to do, Thanks. You can also know what iam trying to achive in the following code......
workflowUpdateTaskProp["Property1"] = "Property1 Value";
I want to convert the above line of code like following:-
workflowUpdateTaskProp["AssignedTo"] = "AnyUserValue"
I Need your Immediate help, I also open a new thread you can also answer here, thanks in advance.

