private static T GetLookupItem <T>(SPField field, object value, FieldAttribute attr) where T : Item, new() { if (field.Type == SPFieldType.Lookup) { var spfl = (SPFieldLookup)field; if (string.IsNullOrEmpty(spfl.LookupList)) { throw new SharepointCommonException(string.Format("It seems that {0} in [{1}] is broken", field.InternalName, field.ParentList.RootFolder.Url)); } var lookupList = field.ParentList.ParentWeb.Lists[new Guid(spfl.LookupList)]; // Lookup with picker (ilovesharepoint) returns SPFieldLookupValue var lkpValue = value as SPFieldLookupValue ?? (value is int ?new SPFieldLookupValue((int)value, string.Empty) : new SPFieldLookupValue((string)value ?? string.Empty)); if (lkpValue.LookupId == 0) { return(null); } return(ToEntity <T>(lkpValue.LookupId, lkpValue.LookupValue, lookupList)); } else if (field.Type == SPFieldType.Number) { var lookupList = field.ParentList.ParentWeb.TryGetListByNameOrUrlOrId(attr.LookupList); return(ToEntity <T>(Convert.ToInt32(value), null, lookupList)); } else if (field.Type == SPFieldType.Number) { var lookupList = field.ParentList.ParentWeb.TryGetListByNameOrUrlOrId(attr.LookupList); return(ToEntity <T>(lookupList.TryGetItemById(Convert.ToInt32(value)))); } else if (attr.FieldProvider != null) { var providerType = attr.FieldProvider.GetType(); var method = providerType.GetMethod("GetLookupItem"); if (method.DeclaringType == typeof(CustomFieldProvider)) { throw new SharepointCommonException(string.Format("Must override GetLookupItem in {0} to get custom lookups field working.", providerType)); } //todo: implement GetLookupItemId return(ToEntity <T>(attr.FieldProvider.GetLookupItem(field, value))); } else { Assert.Inconsistent(); return(null); } }
internal static string ToItem(Type enumType, object value) { if (value == null) { return(null); } var members = enumType.GetMembers(BindingFlags.Public | BindingFlags.Static); var member = members.FirstOrDefault(m => m.Name.Equals(value.ToString(), StringComparison.InvariantCultureIgnoreCase)); if (member == null) { Assert.Inconsistent(); } var attrs = member.GetCustomAttributes(typeof(FieldAttribute), false); if (attrs.Length != 0) { return(((FieldAttribute)attrs[0]).Name); } return(value.ToString()); }
internal static Field ToFieldType(MemberInfo member) { var propertyInfo = member as PropertyInfo; if (propertyInfo == null) { var methodInfo = member as MethodInfo; if (methodInfo == null) { Assert.Inconsistent(); } var declaringType = methodInfo.DeclaringType; string trimGet = member.Name.Substring(4); propertyInfo = declaringType.GetProperty(trimGet); } Type propType = propertyInfo.PropertyType; string spName = TranslateToFieldName(propertyInfo.Name); var fieldAttrs = propertyInfo.GetCustomAttributes(typeof(FieldAttribute), true); var customAttrs = (CustomPropertyAttribute[])propertyInfo.GetCustomAttributes(typeof(CustomPropertyAttribute), true); string dispName = null; bool isMultilineText = false; object defaultValue = null; bool required = false; FieldAttribute attr = null; if (fieldAttrs.Length != 0) { attr = (FieldAttribute)fieldAttrs[0]; var spPropName = attr.Name; if (spPropName != null) { spName = spPropName; } dispName = attr.DisplayName; isMultilineText = attr.IsMultilineText; required = attr.Required; defaultValue = attr.DefaultValue; } var field = new Field { Name = spName, Property = propertyInfo, DisplayName = dispName, Required = required, DefaultValue = defaultValue, FieldAttribute = attr, }; if (customAttrs.Length != 0) { var typeAttr = customAttrs.FirstOrDefault(ca => ca.Name == "Type"); if (typeAttr != null) { field.Type = (SPFieldType)Enum.Parse(typeof(SPFieldType), typeAttr.Value); return(field); } } if (attr != null && attr.FieldProvider != null) { field.Type = SPFieldType.Invalid; return(field); } if (propType == typeof(string)) { field.Type = isMultilineText ? SPFieldType.Note : SPFieldType.Text; return(field); } if (isMultilineText) { throw new SharepointCommonException("[IsMultilineText] can be used only for text fields."); } if (propType == typeof(Version) || propType == typeof(Guid)) { field.Type = SPFieldType.Text; return(field); } if (propType == typeof(DateTime) || propType == typeof(DateTime?)) { field.Type = SPFieldType.DateTime; return(field); } if (propType == typeof(User)) { field.Type = SPFieldType.User; return(field); } if (propType == typeof(double) || propType == typeof(double?) || propType == typeof(int) || propType == typeof(int?)) { field.Type = SPFieldType.Number; return(field); } if (propType == typeof(decimal) || propType == typeof(decimal?)) { field.Type = SPFieldType.Currency; return(field); } if (propType == typeof(bool) || propType == typeof(bool?)) { field.Type = SPFieldType.Boolean; return(field); } // lookup fields if (typeof(Item).IsAssignableFrom(propType)) { // lookup single value if (attr == null) { throw new SharepointCommonException("Lookups must be marked with [SharepointCommon.Attributes.FieldAttribute]"); } field.Type = SPFieldType.Lookup; field.LookupList = attr.LookupList; field.LookupField = attr.LookupField ?? "Title"; return(field); } if (CommonHelper.ImplementsOpenGenericInterface(propType, typeof(IEnumerable <>))) { Type argumentType = propType.GetGenericArguments()[0]; //// user multi value if (argumentType == typeof(User)) { field.Type = SPFieldType.User; field.IsMultiValue = true; return(field); //return new Field {Type = SPFieldType.User, Name = propertyInfo.Name, IsMultiValue = true,}; } //// lookup multi value if (attr == null) { throw new SharepointCommonException("Lookups must be marked with [SharepointCommon.Attributes.FieldAttribute]"); } if (typeof(Item).IsAssignableFrom(argumentType)) { field.Type = SPFieldType.Lookup; field.LookupList = attr.LookupList; field.LookupField = attr.LookupField; field.IsMultiValue = true; return(field); } } if (propType.IsEnum) { field.Type = SPFieldType.Choice; field.Choices = EnumMapper.GetEnumMemberTitles(propType); if (field.Choices.Any() == false) { throw new SharepointCommonException("enum must have at least one field"); } return(field); } if (CommonHelper.ImplementsOpenGenericInterface(propType, typeof(Nullable <>))) { Type argumentType = propType.GetGenericArguments()[0]; if (argumentType.IsEnum) { field.Type = SPFieldType.Choice; field.Choices = Enum.GetNames(argumentType); if (field.Choices.Any() == false) { throw new SharepointCommonException("enum must have at least one field"); } return(field); } } if (propType == typeof(Person)) { throw new SharepointCommonException("Cannot use [Person] as mapped property. Use [User] instead."); } throw new SharepointCommonException("no field type mapping found"); }
private static IEnumerable <T> GetLookupItems <T>(SPField field, object value, FieldAttribute attr, int?itemId, bool reloadLookupItem) where T : Item { if (field.Type == SPFieldType.Lookup) { var spfl = (SPFieldLookup)field; var web = field.ParentList.ParentWeb; var list = field.ParentList; // Reload item, because it may been changed before lazy load requested if (itemId != null && reloadLookupItem) { var item = list.GetItemById((int)itemId); value = item[spfl.InternalName] != null ? item[spfl.InternalName].ToString() : string.Empty; } var lkplist = web.Lists[new Guid(spfl.LookupList)]; var lkpValues = new SPFieldLookupValueCollection( value != null ? value.ToString() : string.Empty); foreach (var lkpValue in lkpValues) { if (lkpValue.LookupId == 0) { yield return(null); } yield return(ToEntity <T>(lkpValue.LookupId, lkpValue.LookupValue, lkplist)); } } else if (attr.FieldProvider != null) { var providerType = attr.FieldProvider.GetType(); var method = providerType.GetMethod("GetLookupItem"); if (method.DeclaringType == typeof(CustomFieldProvider)) { throw new SharepointCommonException(string.Format("Must override GetLookupItem in {0} to get custom lookups field work.", providerType)); } //var web = listItem.Web; // var list = web.Lists[listItem.ParentList.ID]; // var item = list.GetItemById(listItem.ID); //var lkpValues = new SPFieldLookupValueCollection(value != null ? value.ToString() : string.Empty); //var val = item[field.InternalName]; if (value == null) { yield break; } var method2 = providerType.GetMethod("ParseFieldValue"); if (method2.DeclaringType != typeof(CustomFieldProvider)) { value = attr.FieldProvider.ParseFieldValue(value); } if (value is IEnumerable) { foreach (var lkpValue in (IEnumerable)value) { //todo: implement GetLookupItemId var lookupItem = attr.FieldProvider.GetLookupItem(field, lkpValue); yield return(ToEntity <T>(lookupItem)); } } else { throw new SharepointCommonException("custom multilookup value not enumerable!"); } } else { Assert.Inconsistent(); } }