private Status CreateContact(WorkflowInstance workflowInstance, Item item) { DateTime now = DateTime.UtcNow; FieldValue contactsField = item.GetFieldValue(TargetFieldName, true); Guid listID = contactsField.Value != null ? new Guid(contactsField.Value) : Guid.NewGuid(); // if the contacts sublist under the item doesn't exist, create it now if (contactsField.Value == null) { Item list = new Item() { ID = listID, Name = TargetFieldName, IsList = true, FolderID = item.FolderID, ItemTypeID = SystemItemTypes.Reference, ParentID = item.ID, UserID = item.UserID, Created = now, LastModified = now, }; contactsField.Value = listID.ToString(); try { UserContext.Items.Add(list); UserContext.SaveChanges(); } catch (Exception ex) { TraceLog.TraceException("Creating Contact sublist failed", ex); return(Status.Error); } } // get the subject out of the InstanceData bag Item contact = null; try { var contactString = GetInstanceData(workflowInstance, OutputParameterName); contact = JsonSerializer.Deserialize <Item>(contactString); } catch (Exception ex) { TraceLog.TraceException("Deserializing Contact failed", ex); return(Status.Error); } // update the contact if it already exists, otherwise add a new contact if (UserContext.Items.Any(c => c.ID == contact.ID)) { try { UserContext.SaveChanges(); Item dbContact = UserContext.Items.Include("FieldValues").Single(c => c.ID == contact.ID); foreach (var fv in contact.FieldValues) { // add or update each of the fieldvalues var dbfv = dbContact.GetFieldValue(fv.FieldName, true); dbfv.Copy(fv); } dbContact.LastModified = now; UserContext.SaveChanges(); } catch (Exception ex) { TraceLog.TraceException("Update Contact failed", ex); return(Status.Error); } } else { try { Folder folder = FindDefaultFolder(contact.UserID, contact.ItemTypeID); if (folder != null) { contact.FolderID = folder.ID; } UserContext.Items.Add(contact); UserContext.SaveChanges(); } catch (Exception ex) { TraceLog.TraceException("CreateContact: creating contact failed", ex); return(Status.Error); } User user = UserContext.GetUser(item.UserID); if (user == null) { TraceLog.TraceError("Could not find the user associated with Item " + item.Name); return(Status.Error); } // create an operation corresponding to the new contact creation var operation = UserContext.CreateOperation(user, "POST", (int?)System.Net.HttpStatusCode.Created, contact, null); if (operation == null) { TraceLog.TraceError("Failed to create operation"); return(Status.Error); } // kick off the New Contact workflow WorkflowHost.InvokeWorkflowForOperation(UserContext, SuggestionsContext, operation); } // add a contact reference to the contact list Guid refID = Guid.NewGuid(); var contactRef = new Item() { ID = refID, Name = contact.Name, ItemTypeID = SystemItemTypes.Reference, FolderID = item.FolderID, ParentID = listID, UserID = contact.UserID, Created = now, LastModified = now, FieldValues = new List <FieldValue>() { new FieldValue() { FieldName = FieldNames.EntityRef, ItemID = refID, Value = contact.ID.ToString() }, new FieldValue() { FieldName = FieldNames.EntityType, ItemID = refID, Value = EntityTypes.Item } } }; try { UserContext.Items.Add(contactRef); UserContext.SaveChanges(); } catch (Exception ex) { TraceLog.TraceException("Creating Contact reference failed", ex); return(Status.Error); } // add a Suggestion with a RefreshEntity FieldName to the list, to tell the UI that the // workflow changed the Item SignalEntityRefresh(workflowInstance, item); return(Status.Complete); }