Exemplo n.º 1
0
        internal static T ToEntity <T>(int itemId, string title, SPList parentList, bool reloadLookupItem = true) where T : Item
        {
            if (itemId == 0)
            {
                return(null);
            }

            var itemType = typeof(T);
            var props    = itemType.GetProperties();

            foreach (var prop in props)
            {
                if (CommonHelper.IsPropertyNotMapped(prop))
                {
                    continue;
                }
                Assert.IsPropertyVirtual(prop);
            }

            var entity = _proxyGenerator.CreateClassProxy(
                itemType,
                new DocumentAccessInterceptor(itemId, title, parentList),
                new ItemAccessInterceptor(itemId, title, parentList, reloadLookupItem));

            return((T)entity);
        }
Exemplo n.º 2
0
        internal static T ToEntity <T>(SPListItem listItem, bool reloadLookupItem = true)
        {
            if (listItem == null)
            {
                return(default(T));
            }

            var itemType = typeof(T);
            var props    = itemType.GetProperties();

            foreach (var prop in props)
            {
                if (CommonHelper.IsPropertyNotMapped(prop))
                {
                    continue;
                }
                Assert.IsPropertyVirtual(prop);
            }

            var entity = _proxyGenerator.CreateClassProxy(
                itemType,
                new DocumentAccessInterceptor(listItem),
                new ItemAccessInterceptor(listItem, reloadLookupItem));

            return((T)entity);
        }
Exemplo n.º 3
0
        public void CheckThatPropertyVirtualTest_Throws_On_NoVirtual()
        {
            var noVirtualGetProp = typeof(CustomItemNoVirtualProperty).GetProperty("CustomField1");

            NuAssert.Throws <SharepointCommonException>(
                () => ShpcAssert.IsPropertyVirtual(noVirtualGetProp));
        }
Exemplo n.º 4
0
        public void CheckThatPropertyVirtualTest_Not_Throws_On_Virtual()
        {
            var virtualGetSetProp = typeof(CustomItem).GetProperty("CustomUser");

            NuAssert.DoesNotThrow(() => ShpcAssert.IsPropertyVirtual(virtualGetSetProp));

            var virtualGetProp = typeof(CustomItem).GetProperty("Author");

            NuAssert.DoesNotThrow(() => ShpcAssert.IsPropertyVirtual(virtualGetProp));
        }
Exemplo n.º 5
0
        internal static object ToEntity(Type entityType, Hashtable afterProperties, SPList list)
        {
            var props = entityType.GetProperties();

            foreach (var prop in props)
            {
                if (CommonHelper.IsPropertyNotMapped(prop))
                {
                    continue;
                }
                Assert.IsPropertyVirtual(prop);
            }

            var entity = _proxyGenerator.CreateClassProxy(
                entityType,

                new ItemEventReceiverAccessInterceptor(list, afterProperties));

            return(entity);
        }
Exemplo n.º 6
0
        internal static void ToItem <T>(T entity, SPListItem listItem, List <string> propertiesToSet = null)
        {
            var itemType = entity.GetType();
            var props    = itemType.GetProperties();

            foreach (PropertyInfo prop in props)
            {
                if (CommonHelper.IsPropertyNotMapped(prop))
                {
                    continue;
                }

                Assert.IsPropertyVirtual(prop);

                if (propertiesToSet != null && propertiesToSet.Count > 0)
                {
                    if (propertiesToSet.Contains(prop.Name) == false)
                    {
                        continue;
                    }
                }

                string spName;

                // var fieldAttrs = prop.GetCustomAttributes(typeof(FieldAttribute), false);
                var fieldAttrs = Attribute.GetCustomAttributes(prop, typeof(FieldAttribute));
                CustomFieldProvider customFieldProvider = null;
                if (fieldAttrs.Length != 0)
                {
                    spName = ((FieldAttribute)fieldAttrs[0]).Name;
                    if (spName == null)
                    {
                        spName = prop.Name;
                    }
                    customFieldProvider = ((FieldAttribute)fieldAttrs[0]).FieldProvider;
                }
                else
                {
                    spName = FieldMapper.TranslateToFieldName(prop.Name);
                }
                if (FieldMapper.IsReadOnlyField(spName) == false)
                {
                    continue;                                               // skip fields that cant be set
                }
                var propValue = prop.GetValue(entity, null);

                if (propValue == null)
                {
                    listItem[spName] = null;
                    continue;
                }

                if (prop.PropertyType == typeof(string))
                {
                    listItem[spName] = propValue;
                    continue;
                }

                if (prop.PropertyType == typeof(DateTime))
                {
                    // update DateTime field with empty value thrown exception
                    if (((DateTime)propValue) != DateTime.MinValue)
                    {
                        listItem[spName] = propValue;
                    }
                    continue;
                }

                if (prop.PropertyType == typeof(User))
                {
                    // domain user or group
                    Assert.IsPropertyVirtual(prop);

                    var user = (User)propValue;

                    SPFieldUserValue spUserValue = FieldMapper.ToUserValue(user, listItem.Web);

                    listItem[spName] = spUserValue;

                    continue;
                }

                // handle lookup fields
                if (typeof(Item).IsAssignableFrom(prop.PropertyType))
                {
                    Assert.IsPropertyVirtual(prop);

                    if (customFieldProvider == null)
                    {
                        var item   = (Item)propValue;
                        var lookup = new SPFieldLookupValue(item.Id, item.Title);
                        listItem[spName] = lookup;
                    }
                    else
                    {
                        var providerType = customFieldProvider.GetType();
                        var method       = providerType.GetMethod("SetLookupItem");

                        if (method.DeclaringType == typeof(CustomFieldProvider))
                        {
                            throw new SharepointCommonException(string.Format("Must override 'SetLookupItem' in {0} to get custom lookups field working.", providerType));
                        }

                        var value = customFieldProvider.SetLookupItem(propValue);
                        listItem[spName] = value;
                    }
                    continue;
                }

                //// handle multivalue fields
                if (CommonHelper.ImplementsOpenGenericInterface(prop.PropertyType, typeof(IEnumerable <>)))
                {
                    Assert.IsPropertyVirtual(prop);

                    Type argumentType = prop.PropertyType.GetGenericArguments()[0];

                    if (argumentType == typeof(User))
                    {
                        var users = propValue as IEnumerable <User>;
                        Assert.NotNull(users);

                        var values = new SPFieldUserValueCollection();

                        foreach (User user in users)
                        {
                            if (user is Person)
                            {   // domain user or group
                                var    person = (Person)user;
                                SPUser spUser = null;
                                try
                                {
                                    spUser = listItem.ParentList.ParentWeb.SiteUsers[person.Login];
                                }
                                catch (SPException)
                                {
                                    throw new SharepointCommonException(string.Format("User {0} not found.", user.Id));
                                }

                                var val = new SPFieldUserValue();
                                val.LookupId = spUser.ID;
                                values.Add(val);
                            }
                            else
                            {   // sharepoint group
                                SPGroup spGroup = null;
                                try
                                {
                                    spGroup = listItem.ParentList.ParentWeb.SiteGroups[user.Name];
                                }
                                catch (SPException)
                                {
                                    throw new SharepointCommonException(string.Format("Group {0} not found.", user.Name));
                                }

                                var val = new SPFieldUserValue();
                                val.LookupId = spGroup.ID;
                                values.Add(val);
                            }
                        }

                        listItem[spName] = values;
                    }

                    if (typeof(Item).IsAssignableFrom(argumentType))
                    {
                        var lookupvalues = propValue as IEnumerable;

                        if (customFieldProvider == null)
                        {
                            var spLookupValues = new SPFieldLookupValueCollection();

                            foreach (Item lookupvalue in lookupvalues)
                            {
                                var val = new SPFieldLookupValue();
                                val.LookupId = lookupvalue.Id;
                                spLookupValues.Add(val);
                            }

                            listItem[spName] = spLookupValues;
                        }
                        else
                        {
                            var providerType = customFieldProvider.GetType();
                            var method       = providerType.GetMethod("SetLookupItem");

                            if (method.DeclaringType == typeof(CustomFieldProvider))
                            {
                                throw new SharepointCommonException(string.Format("Must override 'SetLookupItem' in {0} to get custom lookups field working.", providerType));
                            }

                            var values = customFieldProvider.SetLookupItem(propValue);
                            listItem[spName] = values;
                        }
                    }
                    continue;
                }

                if (prop.PropertyType.IsEnum)
                {
                    listItem[spName] = EnumMapper.ToItem(prop.PropertyType, propValue);
                    continue;
                }

                var innerType = Nullable.GetUnderlyingType(prop.PropertyType);

                if (innerType != null && innerType.IsEnum)
                {
                    listItem[spName] = EnumMapper.ToItem(innerType, propValue);
                    continue;
                }

                if (prop.PropertyType == typeof(Person))
                {
                    throw new SharepointCommonException("Cannot use [Person] as mapped property. Use [User] instead.");
                }

                listItem[spName] = propValue;
            }
        }