public TEntity MapEntity(TEntity entity, ListItem item) { if (_args == null || entity == null || item == null) { return(entity); } foreach (var fieldMap in _args.FieldMappings) { PropertyInfo prop = entity.GetType().GetProperty(fieldMap.Key, BindingFlags.Public | BindingFlags.Instance); if (null != prop) { if (item.FieldValues.ContainsKey(fieldMap.Value.Name)) { object value = item[fieldMap.Value.Name]; if (!SetEntityLookup(prop.PropertyType, prop.GetValue(entity), value)) { if (prop.CanWrite) { value = GetFieldValue(fieldMap.Value, prop.PropertyType, value); value = SpConverter.ConvertValue(value, prop.PropertyType); prop.SetValue(entity, value); } } } } FieldInfo field = entity.GetType().GetField(fieldMap.Key, BindingFlags.Public | BindingFlags.Instance); if (null != field) { if (item.FieldValues.ContainsKey(fieldMap.Value.Name)) { object value = item[fieldMap.Value.Name]; value = GetFieldValue(fieldMap.Value, field.FieldType, value); value = SpConverter.ConvertValue(value, field.FieldType); field.SetValue(entity, value); } } } if (_args.IncludeItemPermissions) { if (item.IsPropertyAvailable("EffectiveBasePermissions")) { if (entity is ListItemEntity) { (entity as ListItemEntity).EffectiveBasePermissions = item.EffectiveBasePermissions; } } } return(entity); }
protected override void VisitBodyClauses(ObservableCollection <IBodyClause> bodyClauses, QueryModel queryModel) { //base.VisitBodyClauses(bodyClauses, queryModel); if (_expression != null && _args != null) { if (_args != _expression.Args) { return; } var item = _expression.Item; if (item != null) { StringBuilder sb = new StringBuilder(); bool isIdSet = false; foreach (var orderClause in bodyClauses.OfType <OrderByClause>()) { if (orderClause != null) { foreach (var ordering in orderClause.Orderings) { string sortFieldKey = (ordering.Expression as MemberExpression).Member.Name; if (_args.FieldMappings.ContainsKey(sortFieldKey)) { var fieldMap = _args.FieldMappings[sortFieldKey]; if (fieldMap.Sortable == false) { continue; } object value = item[fieldMap.Name]; if (fieldMap.DataType == FieldType.MultiChoice) { continue; } else if (fieldMap is ChoiceFieldAttribute && (fieldMap as ChoiceFieldAttribute).IsMultiple) { continue; } else if (fieldMap is NoteFieldAttribute) { continue; } if (value is DateTime) { value = Convert.ToDateTime(value).ToUniversalTime().ToString("yyyyMMdd HH:mm:ss"); } else if (value is FieldLookupValue) { value = (value as FieldLookupValue).LookupValue; } else if (value is ICollection <FieldLookupValue> ) { continue; } else if (value is FieldUrlValue) { value = string.IsNullOrEmpty((value as FieldUrlValue).Description) ? (value as FieldUrlValue).Url : (value as FieldUrlValue).Description; } else if (value is bool) { value = Convert.ToBoolean(value) == true ? 1 : 0; } else { if (value != null && value.GetType().IsArray) { continue; } value = SpConverter.ConvertValue(value, typeof(string)); } if (sb.Length > 0) { sb.Append("&"); } sb.Append($"p_{fieldMap.Name}={HttpUtility.UrlKeyValueEncode(Convert.ToString(value))}"); if (fieldMap.Name == "ID") { isIdSet = true; } } } } } if (!isIdSet) { if (sb.Length > 0) { sb.Append("&"); } sb.Append($"p_ID={item.Id}"); } PagingInfo = _expression.IsPrev ? $"Paged=TRUE&PagedPrev=TRUE&{sb.ToString()}" : $"Paged=TRUE&{sb.ToString()}"; } } }
private static object GetFieldValue(FieldAttribute fieldAttr, Type valueType, object value) { if (value != null) { if (fieldAttr.DataType == FieldType.Choice || fieldAttr.DataType == FieldType.MultiChoice) { value = EnumExtensions.ParseChoiceValue(valueType, value.ToString()); } else if (fieldAttr.DataType == FieldType.Lookup && (typeof(LookupFieldAttribute).IsAssignableFrom(fieldAttr.GetType()) || fieldAttr.GetType().IsSubclassOf(typeof(LookupFieldAttribute)))) { var lookupFieldMap = fieldAttr as LookupFieldAttribute; if (lookupFieldMap.Result == LookupItemResult.None) { return(value); } if (value is FieldLookupValue) { if (!typeof(FieldLookupValue).IsAssignableFrom(valueType) && !valueType.IsSubclassOf(typeof(FieldLookupValue))) { value = lookupFieldMap.Result == LookupItemResult.Id ? (object)(value as FieldLookupValue).LookupId : (value as FieldLookupValue).LookupValue; if (valueType.IsArray) { var elType = (valueType.GetElementType() ?? valueType.GenericTypeArguments.FirstOrDefault()) ?? typeof(object); value = new[] { SpConverter.ConvertValue(value, elType) }.ToArray(elType); } } } else if (value is FieldLookupValue[]) { if (!lookupFieldMap.IsMultiple) { var lookupValue = (value as FieldLookupValue[]).FirstOrDefault(); if (lookupValue != null) { if (!typeof(FieldLookupValue).IsAssignableFrom(valueType) && !valueType.IsSubclassOf(typeof(FieldLookupValue))) { value = lookupFieldMap.Result == LookupItemResult.Id ? (object)lookupValue.LookupId : lookupValue.LookupValue; } else { value = lookupValue; } } else { value = null; } } else { var elType = (valueType.GetElementType() ?? valueType.GenericTypeArguments.FirstOrDefault()) ?? typeof(object); if (!typeof(FieldLookupValue).IsAssignableFrom(elType) && !elType.IsSubclassOf(typeof(FieldLookupValue))) { var result = lookupFieldMap.Result == LookupItemResult.Id ? (value as FieldLookupValue[]).Select(v => SpConverter.ConvertValue(v.LookupId, elType)) : (value as FieldLookupValue[]).Select(v => SpConverter.ConvertValue(v.LookupValue, elType)); if (valueType.IsArray) { value = result.ToArray(elType); } else { value = result.ToList(elType); } } } } } } return(value); }
public TEntity ToEntity(ListItem item) { var entity = new TEntity(); if (_args != null) { if (item != null) { foreach (var fieldMap in _args.FieldMappings) { PropertyInfo prop = entity.GetType().GetProperty(fieldMap.Key, BindingFlags.Public | BindingFlags.Instance); if (null != prop) { if (prop.CustomAttributes.Any(att => att.AttributeType == typeof(RemovedFieldAttribute))) { continue; } if (fieldMap.Value.Name == "ID" && item.IsPropertyAvailable("Id")) { if (prop.CanWrite) { prop.SetValue(entity, SpConverter.ConvertValue(item.Id, prop.PropertyType)); } continue; } if (item.FieldValues.ContainsKey(fieldMap.Value.Name)) { object value = item[fieldMap.Value.Name]; if (!SetEntityLookup(prop.PropertyType, prop.GetValue(entity), value)) { if (typeof(IListItemEntity).IsAssignableFrom(prop.PropertyType)) { continue; } if (!prop.CanWrite) { continue; } value = GetFieldValue(fieldMap.Value, prop.PropertyType, value); value = SpConverter.ConvertValue(value, prop.PropertyType); prop.SetValue(entity, value); } } } FieldInfo field = entity.GetType().GetField(fieldMap.Key, BindingFlags.Public | BindingFlags.Instance); if (null != field) { if (field.CustomAttributes.Any(att => att.AttributeType == typeof(RemovedFieldAttribute))) { continue; } if (fieldMap.Value.Name == "ID" && item.IsPropertyAvailable("Id")) { field.SetValue(entity, SpConverter.ConvertValue(item.Id, field.FieldType)); continue; } if (item.FieldValues.ContainsKey(fieldMap.Value.Name)) { object value = item[fieldMap.Value.Name]; value = GetFieldValue(fieldMap.Value, field.FieldType, value); value = SpConverter.ConvertValue(value, field.FieldType); field.SetValue(entity, value); } } } if (_args.IncludeItemPermissions) { if (item.IsPropertyAvailable("EffectiveBasePermissions")) { if (entity is ListItemEntity) { (entity as ListItemEntity).EffectiveBasePermissions = item.EffectiveBasePermissions; } } } if (typeof(ICustomMapping).IsAssignableFrom(entity.GetType())) { (entity as ICustomMapping).MapFrom(item); } } foreach (var entitySet in GetEntitySets(entity)) { if (entitySet != null && entitySet.SpQueryArgs != null) { entitySet.SpQueryArgs.Context = _args.Context; } } } return(entity); }