Example #1
0
        public virtual void Add(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            SPListItem newitem;

            SPFolder folder;

            if (string.IsNullOrEmpty(entity.Folder))
            {
                folder = List.RootFolder;
            }
            else
            {
                folder = EnsureFolder(entity.Folder);
            }


            var             ct = GetContentType(entity, false);
            SPContentTypeId ctId;

            if (ct == null)
            {
                ctId = SPBuiltInContentTypeId.Item;
            }
            else
            {
                ctId = ct.Id;
            }

            if (entity is Document)
            {
                var doc = entity as Document;

                if (doc.Content == null || doc.Content.Length == 0)
                {
                    throw new SharepointCommonException("'Content' canot be null or empty");
                }
                if (string.IsNullOrEmpty(doc.Name))
                {
                    throw new SharepointCommonException("'Name' cannot be null or empty");
                }

                if (doc.RenameIfExists)
                {
                    using (var wf = WebFactory.Elevated(SiteId, WebId))
                    {
                        var elevatedList = wf.Web.GetList(Url);
                        doc.Name = FilenameOrganizer.AppendSuffix(doc.Name, newName => !FileExists(newName, elevatedList), 500);
                    }
                }


                var ht = FieldMapper.ToHashTable(entity, ParentWeb.Web);



                var file = folder.Files.Add(doc.Name, doc.Content, ht, true);
                newitem = file.Item;
            }
            else
            {
                newitem = List.AddItem(folder.Url, SPFileSystemObjectType.File, null);
                EntityMapper.ToItem(entity, newitem);

                newitem[SPBuiltInFieldId.ContentTypeId] = ctId;

                newitem.SystemUpdate(false);
            }


            entity.Id       = newitem.ID;
            entity.Guid     = new Guid(newitem[SPBuiltInFieldId.GUID].ToString());
            entity.ListItem = newitem;

            entity.ParentList         = new ListBase <Item>(List, ParentWeb);
            entity.ConcreteParentList = CommonHelper.MakeParentList(typeof(T), ParentWeb, List.ID);
        }