Example #1
0
        public virtual void CheckFields()
        {
            var fields = FieldMapper.ToFields <T>();

            foreach (var fieldInfo in fields)
            {
                if (List.Fields.ContainsFieldWithStaticName(fieldInfo.Name) == false)
                {
                    throw new SharepointCommonException(string.Format("List '{0}' does not contain field '{1}'", List.Title, fieldInfo.Name));
                }
            }
        }
Example #2
0
        public virtual Field GetField(Expression <Func <T, object> > selector)
        {
            var propName = CommonHelper.GetFieldInnerName(selector);

            var fieldInfo = FieldMapper.ToFields <T>().FirstOrDefault(f => f.Name.Equals(propName));

            if (fieldInfo == null)
            {
                throw new SharepointCommonException(string.Format("Field {0} not found", propName));
            }

            return(fieldInfo);
        }
Example #3
0
        public virtual void EnsureFields()
        {
            var fields = FieldMapper.ToFields <T>();

            foreach (var fieldInfo in fields)
            {
                if (FieldMapper.IsReadOnlyField(fieldInfo.Name) == false)
                {
                    continue;                                                        // skip fields that cant be set
                }
                if (FieldMapper.IsFieldCanBeAdded(fieldInfo.Name) == false)
                {
                    continue;
                }

                EnsureFieldImpl(fieldInfo);
            }
        }
Example #4
0
 public virtual IEnumerable <Field> GetFields(bool onlyCustom)
 {
     return(FieldMapper.ToFields(List, onlyCustom));
 }
Example #5
0
        public virtual IEnumerable <T> ByField <TR>(Expression <Func <T, TR> > selector, TR value)
        {
            var    memberAccessor = new MemberAccessVisitor();
            string fieldName      = memberAccessor.GetMemberName(selector);


            var fieldInfo = FieldMapper.ToFields <T>().FirstOrDefault(f => f.Name.Equals(fieldName));

            if (fieldInfo == null && fieldName == "Name")
            {
                fieldInfo = new Field {
                    Type = SPFieldType.Text, Name = "FileLeafRef",
                };
            }

            if (fieldInfo == null)
            {
                throw new SharepointCommonException(string.Format("Field '{0}' not exist in '{1}'", fieldName, List.Title));
            }

            string fieldType = fieldInfo.Type.ToString();

            var camlByField = string.Empty;

            fieldName = fieldInfo.Name;

#pragma warning disable 612,618
            if (CommonHelper.IsNullOrDefault(value))
            {
                if (value is ValueType)
                {
                    camlByField = Q.Where(Q.Eq(Q.FieldRef(fieldName), Q.Value(default(TR).ToString())));
                }
                else
                {
                    camlByField = Q.Where(Q.IsNull(Q.FieldRef(fieldName)));
                }
            }
            else if (fieldInfo.Type == SPFieldType.User)
            {
                var user = value as User;
                int userId;

                if (user.Id != 0)
                {
                    userId = user.Id;
                }
                else
                {
                    var person = user as Person;
                    if (person != null)
                    {
                        try
                        {
                            var spUser = ParentWeb.Web.SiteUsers[person.Login];
                            userId = spUser.ID;
                        }
                        catch (SPException)
                        {
                            throw new SharepointCommonException(string.Format("Person {0} not found.", person.Login));
                        }
                    }
                    else
                    {
                        try
                        {
                            var group = ParentWeb.Web.SiteGroups[user.Name];
                            userId = @group.ID;
                        }
                        catch (SPException)
                        {
                            throw new SharepointCommonException(string.Format("Group {0} not found.", user.Name));
                        }
                    }
                }

                camlByField = Q.Where(Q.Eq(Q.FieldRef(fieldName, true), Q.Value(fieldType, userId.ToString())));
            }
            else if (fieldInfo.Type == SPFieldType.Lookup)
            {
                var item = value as Item;

                if (item.Id != 0)
                {
                    camlByField = Q.Where(Q.Eq(Q.FieldRef(fieldName, true), Q.Value(fieldType, item.Id.ToString())));
                }
                else if (item.Title != null)
                {
                    camlByField = Q.Where(Q.Eq(Q.FieldRef(fieldName), Q.Value(fieldType, item.Title)));
                }
                else
                {
                    throw new SharepointCommonException("Both Id and Title are null in search value");
                }
            }
            else
            {
                camlByField = Q.Where(Q.Eq(Q.FieldRef(fieldName), Q.Value(fieldType, value.ToString())));
            }
#pragma warning restore 612,618
            var itemsByField = ByCaml(List, camlByField);
            return(EntityMapper.ToEntities <T>(itemsByField));
        }