public EnsureUserResult EnsureUser(EnsureUserQuery query)
        {
            DisableCaching();
            var result = new EnsureUserResult {
                RosterLookupId = -1,
                Key            = "",
                DisplayText    = ""
            };

            try {
                RosterConfigService configProvider = new RosterConfigService();
                ListMetadataField   field          = configProvider.GetField(query.FieldMetedataId.ToGuid());

                DbFieldUser dbFld = field.GetDbField() as DbFieldUser;
                var         user  = dbFld.EnsureUser(XDocument.Parse(query.XmlText));

                result.DisplayText    = user.DisplayText;
                result.Key            = user.Key;
                result.RosterLookupId = user.RosterLookupId;
            }
            catch (Exception ex) {
                HandleException(ex);
            }

            return(result);
        }
        private List <Tuple <string, string> > GetCriteria(LookupQuery query)
        {
            var whereCriteria = new List <Tuple <string, string> >();

            if (query.ParentKeyValue.ToInt() != 0)
            {
                var rosterConfigService = new RosterConfigService();
                var field = rosterConfigService.GetField(query.MetadataId.ToGuid());
                if (field == null)
                {
                    throw new Exception(string.Format("Field '{0}' not found", query.MetadataId));
                }
                var dbfield = field.GetDbField() as DbFieldLookup;
                if (dbfield != null)
                {
                    var list = rosterConfigService.GetList(field.ListMetadataId);
                    if (list == null)
                    {
                        throw new Exception(string.Format("List '{0}' not found", field.ListMetadataId));
                    }
                    var parentField =
                        list.ListMetadataFields.FirstOrDefault(item => item.InternalName == dbfield.DependentParent);
                    if (parentField != null)
                    {
                        if (dbfield.DependentParentField == parentField.DataSourceKey)
                        {
                            whereCriteria.Add(new Tuple <string, string>(dbfield.FilterByField, query.ParentKeyValue.ToSafeString()));
                        }
                        else
                        {
                            var parentResults = BLExtensions.SourceContent(parentField.DataSource,
                                                                           parentField.DataSourceKey.ToSafeString(),
                                                                           parentField.DataSourceField, parentField.DataSourceType,
                                                                           new List <Tuple <string, string> >
                            {
                                new Tuple <string, string>(parentField.DataSourceKey.ToSafeString(),
                                                           query.ParentKeyValue.ToSafeString())
                            });
                            if (!parentResults.IsEmpty())
                            {
                                var firstOrDefault = parentResults.FirstOrDefault();
                                if (firstOrDefault != null)
                                {
                                    var parentResult = (firstOrDefault.Item2 as IDictionary <string, object>);
                                    if (parentResult.ContainsKey(dbfield.DependentParentField))
                                    {
                                        whereCriteria.Add(new Tuple <string, string>(dbfield.FilterByField, parentResult[dbfield.DependentParentField].ToSafeString()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(whereCriteria);
        }