private ListItem SetValues(string[] keyValues, ListItem listItem, ref T sEntity, bool toLocalTime) { foreach (var keyValue in keyValues) { try { bool readOnly = false; var key = keyValue.Split('=')[0]; var value = WebUtility.UrlDecode(keyValue.Split('=')[1]); var property = SpNameUtility.GetProperty(key, typeof(T)); //typeof(T).GetProperty(SpNameUtility.GetPropertyName(key, typeof(T))); var customAttributes = property.GetCustomAttributes(typeof(SpproFieldAttribute), true); if (customAttributes.Count() > 0) { var attribute = (SpproFieldAttribute)customAttributes[0]; readOnly = attribute.ReadOnly; } if (!readOnly) { object finalValue = value; var targetType = IsNullableType(property.PropertyType) ? Nullable.GetUnderlyingType(property.PropertyType) : property.PropertyType; if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?)) { var format = value.Split('[', ']')[1]; value = value.Substring(0, value.IndexOf('[')); DateTime dateValue = new DateTime();; if (DateTime.TryParseExact(value, format, null, System.Globalization.DateTimeStyles.None, out dateValue)) { if (toLocalTime) { dateValue = dateValue.ToLocalTime(); } finalValue = dateValue; property.SetValue(sEntity, Convert.ChangeType(dateValue, targetType)); } else { continue; } } else if (property.PropertyType == typeof(GeoCoordinate)) { var latitude = Convert.ToDouble(value.Split(',')[0]); var longitude = Convert.ToDouble(value.Split(',')[1]); var geoValue = new FieldGeolocationValue(); geoValue.Latitude = latitude; geoValue.Longitude = longitude; finalValue = geoValue; var geoLocation = new GeoCoordinate(latitude, longitude); property.SetValue(sEntity, Convert.ChangeType(geoLocation, targetType)); } else if (property.PropertyType == typeof(Microsoft.SharePoint.Client.FieldLookupValue)) { finalValue = new FieldLookupValue(); ((FieldLookupValue)finalValue).LookupId = Convert.ToInt32(value); property.SetValue(sEntity, finalValue); } else if (property.PropertyType == typeof(Microsoft.SharePoint.Client.FieldUrlValue)) { finalValue = new FieldUrlValue() { Description = value.Split(',')[0], Url = WebUtility.UrlDecode(value.Split(',')[1]) }; property.SetValue(sEntity, finalValue); } else if (property.PropertyType == typeof(Microsoft.SharePoint.Client.FieldUserValue)) { int userId; if (int.TryParse(value, out userId)) { finalValue = new FieldUserValue() { LookupId = userId }; property.SetValue(sEntity, finalValue); } } else if (property.PropertyType == typeof(string[])) { finalValue = value.Split(new string[] { "<<,>>" }, StringSplitOptions.RemoveEmptyEntries); property.SetValue(sEntity, finalValue); } else { if (!(finalValue.ToString() == "" && TypeUtility.IsNumeric(targetType))) { if (finalValue.ToString() == "NaN") { finalValue = 0; } property.SetValue(sEntity, Convert.ChangeType(finalValue, targetType)); } } //Get SharePoint Field Name key = SpNameUtility.GetSPFieldName(key, typeof(T)); listItem[key] = finalValue; } } catch (Exception ex) { throw new Exception("error with " + keyValue, ex); } } return(listItem); }
/// <summary> /// Populate SharePoint Entity using Reflection /// Convert retreived data in NSW Eastern Standard Time. /// for QLD Standard time use "E. Australia Standard Time" /// </summary> /// <param name="entity"></param> /// <param name="item"></param> /// <returns></returns> internal async Task <T> PopulateSEntity(ListItem item) { var entity = CreateInstance(); entity.ID = item.Id; foreach (var field in item.FieldValues) { try { var property = SpNameUtility.GetProperty(field.Key, typeof(T)); //typeof(T).GetProperty(SpNameUtility.GetPropertyName(field.Key, typeof(T))); if (property != null) { var customAttributes = property.GetCustomAttributes(typeof(SpproNavigationAttribute), true); if (!(customAttributes.Length > 0 && ((SpproNavigationAttribute)customAttributes[0]).NavigationProperty)) { var targetType = property.PropertyType; //Nullable properties have to be treated differently, since we // use their underlying property to set the value in the object if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { //if it's null, just set the value from the reserved word null, and return if (field.Value == null) { property.SetValue(entity, null, null); continue; } //Get the underlying type property instead of the nullable generic targetType = new NullableConverter(property.PropertyType).UnderlyingType; } customAttributes = property.GetCustomAttributes(typeof(SpproFieldAttribute), true); if (customAttributes.Length > 0 && !string.IsNullOrWhiteSpace(((SpproFieldAttribute)customAttributes[0]).FieldType)) { var attribute = (SpproFieldAttribute)customAttributes[0]; switch (attribute.FieldType) { case "File": #region File Type Microsoft.SharePoint.Client.File file = item.File; SpproFile spproFile = new SpproFile(); var fileStream = file.OpenBinaryStream(); ClientContext.Load(file); ClientContext.ExecuteQuery(); using (var ms = new MemoryStream()) { fileStream.Value.CopyTo(ms); spproFile.Content = ms.ToArray(); spproFile.FileName = item.File.Name; property.SetValue(entity, spproFile); } #endregion break; case "Lookup": #region Lookup Type if (field.Value != null && !(IsObjectFalsy(field.Value))) { dynamic value; switch (attribute.FieldValue) { case "IdAndValue": property.SetValue(entity, field.Value); break; case "Value": value = ((FieldLookupValue)field.Value).LookupId; property.SetValue(entity, value); break; case "Id": case null: if (field.Value == null || field.Value.ToString() == "") { property.SetValue(entity, 0); } else { value = ((FieldLookupValue)field.Value).LookupId; property.SetValue(entity, value); } break; } } #endregion break; case "User": #region User Type if (field.Value != null) { dynamic value; switch (attribute.FieldValue) { case "Value": if (field.Value is FieldUserValue[]) { value = ((FieldUserValue[])field.Value).Select(a => a.LookupValue).ToList(); } else { value = ((FieldUserValue)field.Value).LookupValue; } property.SetValue(entity, value); break; case "IdAndValue": property.SetValue(entity, field.Value); break; default: case "Id": case null: if (field.Value is FieldUserValue[]) { value = ((FieldUserValue[])field.Value).Select(a => a.LookupId).ToList(); property.SetValue(entity, value); } else { value = ((FieldUserValue)field.Value).LookupId; property.SetValue(entity, value); } break; } } #endregion break; case "Multi-Choice": property.SetValue(entity, (string[])field.Value); break; } } else { //Deal with null SP Columns if (TypeUtility.IsNumeric(targetType) && field.Value == null) { property.SetValue(entity, Convert.ChangeType(0, targetType)); } else { //Handle unusal object types (ie GeoCoordinate = FieldGeolocation) switch (targetType.Name) { case "GeoCoordinate": if (field.Value != null) { GeoCoordinate GeoValue = new GeoCoordinate(); GeoValue.Longitude = ((FieldGeolocationValue)field.Value).Longitude; GeoValue.Latitude = ((FieldGeolocationValue)field.Value).Latitude; GeoValue.Altitude = 0; property.SetValue(entity, GeoValue); } break; case "Boolean": if (field.Value == null) { property.SetValue(entity, false); } else { property.SetValue(entity, field.Value.ToString().ToLower() == "yes" || field.Value.ToString().ToLower() == "true"); } break; case "DateTime": case "DateTime?": //DateTime localTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId((DateTime)field.Value, timeZone); var dateValue = new DateTime(); if (DateTime.TryParse(field.Value.ToString(), out dateValue)) { property.SetValue(entity, dateValue); } break; default: property.SetValue(entity, Convert.ChangeType(field.Value, targetType)); break; } } } } } } catch (Exception ex) { throw new Exception("Error with " + field, ex); } } //Populate Navigation Properties //Beware this requries calls to SharePoint via ClientContext and too many may slow down application if (LazyLoading) { foreach (var property in entity.GetType().GetProperties()) { var customAttributes = property.GetCustomAttributes(typeof(SpproNavigationAttribute), true); if (customAttributes.Length > 0 && ((SpproNavigationAttribute)customAttributes[0]).NavigationProperty) { var foriegnKey = ((SpproNavigationAttribute)customAttributes[0]).LookupField; var genericType = property.PropertyType.GetGenericArguments()[0]; var repo = typeof(SpproRepository <>); var constructedRepoType = repo.MakeGenericType(genericType); var listName = genericType.Name; var genericAttributes = genericType.GetCustomAttributes(typeof(SpproListAttribute), true); if (genericAttributes.Length > 0 && !string.IsNullOrWhiteSpace(((SpproListAttribute)genericAttributes[0]).ListName)) { listName = ((SpproListAttribute)genericAttributes[0]).ListName; } dynamic repoInstance = Activator.CreateInstance(constructedRepoType, listName, ClientContext); var values = await repoInstance.Query(string.Format("{0}={1}", foriegnKey, entity.ID)); property.SetValue(entity, values); } else if (customAttributes.Length > 0 && ((SpproNavigationAttribute)customAttributes[0]).ForeignKey) { var foriegnKey = ((SpproNavigationAttribute)customAttributes[0]).LookupField; try { var foriegnId = Convert.ToInt32(((FieldLookupValue)item.FieldValues[foriegnKey]).LookupId); if (foriegnId > 0) { var repo = typeof(SpproRepository <>); var constructedRepoType = repo.MakeGenericType(property.PropertyType); var listName = property.PropertyType.Name; var propertyAttributes = property.PropertyType.GetCustomAttributes(typeof(SpproListAttribute), true); if (propertyAttributes.Length > 0 && !string.IsNullOrWhiteSpace(((SpproListAttribute)propertyAttributes[0]).ListName)) { listName = ((SpproListAttribute)propertyAttributes[0]).ListName; } dynamic repoInstance = Activator.CreateInstance(constructedRepoType, listName, ClientContext); var values = await repoInstance.GetById(foriegnId); property.SetValue(entity, values); } } catch (Exception ex) { log.Error("Error with lazy loading foriegn key", ex); } } } } return(entity); }