public ListItem Update(int itemId, Dictionary <string, object> properties, int version) { if (properties == null || _args == null) { return(null); } List list = GetList(); ListItem listItem = itemId > 0 ? list.GetItemById(itemId) : list.AddItem(new ListItemCreationInformation()); var fieldMappings = _args.FieldMappings; bool fUpdate = false; foreach (var property in properties) { if (!fieldMappings.ContainsKey(property.Key)) { continue; } var fieldMapping = fieldMappings[property.Key]; if (fieldMapping.IsReadOnly || typeof(DependentLookupFieldAttribute).IsAssignableFrom(fieldMapping.GetType()) || typeof(CalculatedFieldAttribute).IsAssignableFrom(fieldMapping.GetType()) || fieldMapping.DataType == FieldType.Calculated) { continue; } var value = property.Value; if (itemId > 0 || (itemId <= 0 && !Equals(value, default))) { if (value != null) { if (fieldMapping.DataType == FieldType.Choice || fieldMapping.DataType == FieldType.MultiChoice) { Type valueType = value.GetType(); value = EnumExtensions.GetChoiceValue(valueType, value); } else if (fieldMapping.DataType == FieldType.Lookup) { if (typeof(LookupFieldAttribute).IsAssignableFrom(fieldMapping.GetType())) { if ((fieldMapping as LookupFieldAttribute).Result == LookupItemResult.Value) { continue; } else if ((fieldMapping as LookupFieldAttribute).IsMultiple) { //TODO: update lookup field with multiple values. } } } } listItem[fieldMapping.Name] = value; fUpdate = true; } } if (fUpdate) { if (version > 0) { listItem["owshiddenversion"] = version; } listItem.Update(); listItem.Context.Load(listItem); return(listItem); } return(null); }
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 ListItem Update(int itemId, Dictionary <string, object> properties, int version, bool systemUpdate = false, Func <ListItem, bool> onUpdating = null, bool reloadAllValues = false) { if (properties == null || _args == null) { return(null); } List list = GetList(); if (list == null) { Check.NotNull(list, nameof(List)); } ListItem listItem = itemId > 0 ? list.GetItemById(itemId) : list.AddItem(new ListItemCreationInformation()); var fieldMappings = _args.FieldMappings; bool fUpdate = false; foreach (var property in properties) { if (!fieldMappings.ContainsKey(property.Key)) { continue; } var fieldMapping = fieldMappings[property.Key]; if (fieldMapping.IsReadOnly || typeof(DependentLookupFieldAttribute).IsAssignableFrom(fieldMapping.GetType()) || typeof(CalculatedFieldAttribute).IsAssignableFrom(fieldMapping.GetType()) || fieldMapping.DataType == FieldType.Calculated) { continue; } var value = property.Value; if (itemId > 0 || (itemId <= 0 && !Equals(value, default))) { if (value != null) { if (fieldMapping.DataType == FieldType.Choice) { Type valueType = value.GetType(); if (valueType.IsEnum) { value = EnumExtensions.GetChoiceValueString(valueType, value); } } if (fieldMapping.DataType == FieldType.MultiChoice) { Type valueType = value.GetType(); if (valueType.IsEnum) { value = EnumExtensions.GetChoiceValuesString(valueType, value).ToArray(); } } else if (fieldMapping.DataType == FieldType.Lookup) { if (typeof(LookupFieldAttribute).IsAssignableFrom(fieldMapping.GetType())) { if ((fieldMapping as LookupFieldAttribute).Result == LookupItemResult.Value) { continue; } else if ((fieldMapping as LookupFieldAttribute).IsMultiple) { if (!(value is ICollection <FieldLookupValue>)) { value = value is ICollection <int>?(value as ICollection <int>).Select(id => new FieldLookupValue() { LookupId = id }).ToArray() : null; } } else { if (value is IListItemEntity) { } } } } } listItem[fieldMapping.Name] = value; fUpdate = true; } } if (onUpdating != null) { fUpdate = onUpdating(listItem) || fUpdate; } if (fUpdate) { if (version > 0) { listItem["owshiddenversion"] = version; } reloadAllValues = true; var fieldNamesToLoad = (reloadAllValues ? fieldMappings.Values.Where( field => !typeof(DependentLookupFieldAttribute).IsAssignableFrom(field.GetType())) .Select(field => field.Name) : listItem.FieldValues.Keys).Concat(new[] { "ID", "ContentTypeId", "owshiddenversion" }).Distinct(); var retrievals = fieldNamesToLoad.Select(fieldName => { return((Expression <Func <ListItem, object> >)(item => item[fieldName])); }).ToArray(); if (systemUpdate) { #if !SP2013 && !SP2016 listItem.SystemUpdate(); #else throw new NotSupportedException("ListItem.SystemUpdate()"); #endif } else { listItem.Update(); } listItem.Context.Load(listItem, retrievals); if (_args.IncludeItemPermissions) { listItem.Context.Load(listItem, item => item.EffectiveBasePermissions); } return(listItem); } return(null); }