private Item MakeContact(WorkflowInstance workflowInstance, Item item, ADQueryResult subject) { DateTime now = DateTime.UtcNow; /* FieldValue contactsField = GetFieldValue(item, 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(); UserContext.Items.Add(list); UserContext.SaveChanges(); // add a Suggestion with a RefreshEntity FieldName to the list, to tell the UI that the // workflow changed the Item SignalEntityRefresh(workflowInstance, item); } */ // try to find an existing contact using matching heuristic var contact = GetContact(item.UserID, subject); // if the contact wasn't found, create the new contact (detached) - it will be JSON-serialized and placed into // the suggestion value field if (contact == null) { contact = new Item() { ID = Guid.NewGuid(), Name = subject.Name, FolderID = item.FolderID, ItemTypeID = SystemItemTypes.Contact, ParentID = null /*listID*/, UserID = item.UserID, FieldValues = new List<FieldValue>(), Created = now, LastModified = now, }; } // add various FieldValues to the contact if available try { // add sources string sources = String.Join(",", subject.IDs.Select(id => id.Source)); contact.GetFieldValue(FieldNames.Sources, true).Value = sources; // add birthday if (subject.Birthday != null) contact.GetFieldValue(FieldNames.Birthday, true).Value = ((DateTime)subject.Birthday).ToString("d"); // add facebook ID string fbID = subject.IDs.Single(id => id.Source == ADQueryResultValue.FacebookSource).Value; if (fbID != null) contact.GetFieldValue(FieldNames.FacebookID, true).Value = fbID; } catch (Exception) { } return contact; }
private Item GetContact(Guid userid, ADQueryResult subject) { try { // try to get an existing contact by name var contact = UserContext.Items. Include("FieldValues"). Single(i => i.UserID == userid && i.ItemTypeID == SystemItemTypes.Contact && i.Name == subject.Name); // ensure that if a facebook ID exists, it matches the FBID of the subject just retrieved var fbid = contact.GetFieldValue(FieldNames.FacebookID); var ids = subject.IDs.Where(id => id.Source == Sources.Facebook).ToList(); if (ids.Count > 0 && fbid != null && fbid.Value != ids[0].Value) return null; return contact; } catch (Exception) { // contact not found return null; } }