public void ManageLists(OrganizationServiceContext context, Entity contact) { foreach (var item in MarketingListsListView.Items) { if (item == null) { continue; } var listViewItem = item; var hiddenListId = (HiddenField)listViewItem.FindControl("ListID"); if (hiddenListId == null) { continue; } var listId = new Guid(hiddenListId.Value); var ml = context.RetrieveSingle("list", FetchAttribute.All, new Condition("listid", ConditionOperator.Equal, listId)); var listCheckBox = (CheckBox)item.FindControl("ListCheckbox"); if (listCheckBox == null) { continue; } var contactLists = contact.GetRelatedEntities(XrmContext, new Relationship("listcontact_association")).ToList(); var inList = contactLists.Any(list => list.GetAttributeValue <Guid>("listid") == ml.Id); if (listCheckBox.Checked && !inList) { context.AddMemberList(ml.GetAttributeValue <Guid>("listid"), contact.GetAttributeValue <Guid>("contactid")); } else if (!listCheckBox.Checked && inList) { context.RemoveMemberList(ml.GetAttributeValue <Guid>("listid"), contact.GetAttributeValue <Guid>("contactid")); } } }