Exemple #1
0
        private static void GetImageValue(T entity, PropertyInfo property, SPListItem item, string fieldName)
        {
            if (property.PropertyType == typeof(PublishingImage))
            {
                var imageFieldValue = item[fieldName] as ImageFieldValue;

                if (imageFieldValue == null)
                {
                    return;
                }

                var image = new PublishingImage
                {
                    AlternateText = imageFieldValue.AlternateText,
                    Hyperlink     = imageFieldValue.Hyperlink,
                    ImageUrl      = imageFieldValue.ImageUrl
                };

                property.SetValue(entity, image, null);
            }
            else
            {
                property.SetValue(entity, item[fieldName] as string, null);
            }
        }
        private void SetPropertyValue <TType>(PropertyInfo propInfo, TType obj, ListItem item) where TType : SharePointDomainModel
        {
            // Don't map ignored properties
            var ignoredPropertyAttribute = propInfo.GetCustomAttribute <IgnoredPropertyAttribute>();

            if (ignoredPropertyAttribute != null)
            {
                return;
            }

            var attribute = propInfo.GetCustomAttribute <LookupListNameAttribute>();

            if (attribute == null)
            {
                var underlyingType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;

                if (underlyingType.FullName == "Amt.SharePoint.Integration.Models.User")
                {
                    var fieldUserValue = (FieldUserValue)item[propInfo.PropertyName()];
                    var user           = _ctx.Web.SiteUsers.GetById(fieldUserValue.LookupId);
                    _ctx.Load(user);
                    _ctx.ExecuteQuery();

                    var sharePointUser = new Models.User
                    {
                        Email     = user.Email,
                        ID        = user.Id,
                        LoginName = user.LoginName,
                        Title     = user.Title
                    };

                    propInfo.SetValue(obj, sharePointUser, null);
                }
                if (underlyingType.FullName == "Amt.SharePoint.Integration.Models.Hyperlink")
                {
                    var fieldUrlValue = (FieldUrlValue)item[propInfo.PropertyName()];

                    if (fieldUrlValue == null)
                    {
                        propInfo.SetValue(obj, null, null);
                        return;
                    }

                    //TODO: Downlaod content if downloadable.
                    var url = fieldUrlValue.Url;

                    var downloadableContent = propInfo.GetCustomAttribute <DownloadableContentAttribute>();
                    if (downloadableContent != null)
                    {
                        var rootUrl = string.Join("/", _sharepointUrl.Split('/').Take(3).ToArray());
                        url = url.Replace(rootUrl + "/:i:/r", "");
                        url = url.Replace(rootUrl, "");
                        url = url.Split('?')[0];
                        DownloadImage(url);
                    }

                    var hyperlink = new Hyperlink
                    {
                        Url         = url,
                        Description = fieldUrlValue.Description,
                        TypeId      = fieldUrlValue.TypeId
                    };

                    propInfo.SetValue(obj, hyperlink, null);
                }
                if (underlyingType.FullName == "Amt.SharePoint.Integration.Models.PublishingImage")
                {
                    var fieldUrlValue = (string)item[propInfo.PropertyName()];

                    if (fieldUrlValue == null)
                    {
                        propInfo.SetValue(obj, null, null);
                        return;
                    }

                    Match matches = Regex.Match(fieldUrlValue, "alt=\"(?<AltTag>.*?)\".* src=\"(?<SrcTag>.*?)\"");

                    DownloadImage(matches.Groups["SrcTag"].Value);

                    var image = new PublishingImage
                    {
                        Alt = matches.Groups["AltTag"].Value,
                        Src = matches.Groups["SrcTag"].Value
                    };

                    propInfo.SetValue(obj, image, null);
                }
                else
                {
                    propInfo.SetValue(obj, Convert.ChangeType(item[propInfo.PropertyName()], underlyingType), null);
                }
            }
            else
            {
                if (item[propInfo.PropertyName()] == null)
                {
                    return;
                }

                if (propInfo.PropertyType.IsArray)
                {
                    Type arrayType = propInfo.PropertyType.GetElementType();

                    var array = Array.CreateInstance(arrayType, ((FieldLookupValue[])(item[propInfo.PropertyName()])).Count());

                    for (var index = 0;
                         index < ((FieldLookupValue[])(item[propInfo.PropertyName()])).Length;
                         index++)
                    {
                        var lookupId = ((FieldLookupValue[])(item[propInfo.PropertyName()]))[index].LookupId;

                        GenericInvoker invoker = DynamicMethods.
                                                 GenericMethodInvokerMethod(typeof(SharePointRepository <T>),
                                                                            "GetById", new[] { arrayType },
                                                                            new[] { arrayType });

                        var lookupItem = invoker(this, lookupId);

                        array.SetValue(lookupItem, index);
                    }

                    propInfo.SetValue(obj, array, null);
                }
                else
                {
                    var lookupId = ((FieldLookupValue)(item[propInfo.PropertyName()])).LookupId;

                    GenericInvoker invoker = DynamicMethods.
                                             GenericMethodInvokerMethod(typeof(SharePointRepository <T>),
                                                                        "GetById", new[] { propInfo.PropertyType },
                                                                        new[] { propInfo.PropertyType });

                    var lookupItem = invoker(this, lookupId);

                    propInfo.SetValue(obj, Convert.ChangeType(lookupItem, propInfo.PropertyType), null);
                }
            }
        }