Example #1
0
        public override IEnumerable <ItemInfo> GetItems(ItemContainerInfo container)
        {
            // I wouldn't so aggressively store the child items but calling Get-ChildItem cmdlet
            // checks multiple times the existence of a "*" child. Because this triggers the
            // children retrieval I'd better cache them. The cache stores resolved SharePoint
            // objects by their path. I could adapt the cache to accommodate the collections of
            // children too but it was easier to utilize an internal property in the parent web
            // instance, which even performs better.
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            var list   = container as ListInfo;
            var folder = container as FolderInfo;
            var items  = list != null ? list.ChildItems : folder.ChildItems;

            if (items == null || !Cache.Check())
            {
                items = GetItemsDirectly(container);
                if (list != null)
                {
                    list.ChildItems = items;
                }
                else
                {
                    folder.ChildItems = items;
                }
            }
            return(items);
        }
Example #2
0
        protected override XmlElement RawAddItem(ItemContainerInfo container, string name)
        {
            var item = RawAddItem(container, name, ItemType.Common);

            SaveSite(item);
            return(item);
        }
Example #3
0
 void AddCachedItem(ItemInfo item, ItemContainerInfo container = null)
 {
     if (container == null)
     {
         var  path = PathUtility.GetParentPath(item.Path);
         Info parent;
         if (!path.IsEmpty() && Cache.TryGetObject(path, out parent))
         {
             container = (ItemContainerInfo)parent;
         }
     }
     if (container != null)
     {
         var list   = container as ListInfo;
         var folder = container as FolderInfo;
         var items  = list != null ? list.ChildItems : folder.ChildItems;
         if (items != null)
         {
             items = items.Concat(new[] { item }).ToList();
             if (list != null)
             {
                 list.ChildItems = items;
             }
             else
             {
                 folder.ChildItems = items;
             }
         }
     }
 }
Example #4
0
        protected override XmlElement RawAddItem(ItemContainerInfo container, string name)
        {
            Log.Verbose("Adding the item {0} to /{1}.", name, container.Path);
            var input   = new XmlDocument();
            var updates = AddCamlNewItem(input, container, name);
            var output  = (XmlElement)GetService <Lists>(container.List.Web.Path).UpdateListItems(
                container.List.StringID, updates);

            return(GetUpdateResult(output));
        }
Example #5
0
        XmlNode AddCamlNewItem(XmlDocument input, ItemContainerInfo container, string name)
        {
            var updates = input.CreateElement("Batch");

            updates.SetAttribute("DateInUtc", "TRUE");
            updates.SetAttribute("RootFolder", "/" + container.WebRelativePath);
            updates.InnerXml = string.Format(@"<Method ID=""1"" Cmd=""New"">
  <Field Name=""BaseName"">{0}</Field><Field Name=""Title"">{0}</Field>
</Method>", XmlUtility.EscapeXmlValue(name));
            return(updates);
        }
Example #6
0
        public ItemInfo MoveItem(ItemInfo item, ItemContainerInfo target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            var moved = MoveItemDirectly(item, target);

            RemoveCachedItem(item);
            AddCachedItem(moved, target);
            return(moved);
        }
Example #7
0
 public FolderInfo AddFolder(ItemContainerInfo container, string name)
 {
     if (container == null)
         throw new ArgumentNullException("container");
     if (name == null)
         throw new ArgumentNullException("name");
     if (string.IsNullOrEmpty(name))
         throw new ArgumentException("The name of a new folder must not be empty.");
     var folder = (FolderInfo) AddFolderDirectly(container, name);
     AddCachedItem(folder, container);
     return folder;
 }
Example #8
0
        public ItemInfo CopyItem(ItemInfo item, ItemContainerInfo target,
                                 bool recurse, string newName)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            var copy = CopyItemDirectly(item, target, recurse, newName);

            AddCachedItem(copy, target);
            return(copy);
        }
Example #9
0
        internal bool GetItemByIndex(int indx, out ItemContainerInfo container)
        {
            RealizedItemInfo realizedInfo;

            if (RealizedIndex.TryGetValue(indx, out realizedInfo))
            {
                container = realizedInfo.Container;
                return(true);
            }
            container = null;
            return(false);
        }
Example #10
0
        protected override IEnumerable <XmlElement> QueryItems(ItemContainerInfo container)
        {
            Log.Verbose("Querying items of /{0}.", container.Path);
            var list    = container.List;
            var input   = new XmlDocument();
            var view    = AddCamlFields(input, list);
            var folder  = container == list ? null : container.WebRelativePath;
            var options = AddCamlOptions(input, folder);
            var output  = (XmlElement)GetService <Lists>(list.Web.Path).GetListItems(
                list.StringID, null, null, view, null, options, null);

            // Pick all /listitems/data/row. Avoiding XPath with namespaces.
            return(output.ChildNodes.OfType <XmlElement>().First().ChildNodes.OfType <XmlElement>());
        }
Example #11
0
        XmlNode AddCamlMove(XmlDocument input, ItemInfo item, ItemContainerInfo target)
        {
            var updates = input.CreateElement("Batch");
            var fields  = new StringBuilder();
            var newUrl  = PathUtility.JoinPath(target.ServerRelativePath, item.Name);

            fields.AppendFormat(@"<Method ID=""1"" Cmd=""Move"">
  <Field Name=""ID"">{0}</Field><Field Name=""FileRef"">{1}</Field>
  <Field Name=""MoveNewUrl"">{2}</Field>", item.ID,
                                XmlUtility.EscapeXmlValue(item.ServerRelativePath), XmlUtility.EscapeXmlValue(newUrl));
            fields.Append(@"</Method>");
            updates.InnerXml = fields.ToString();
            return(updates);
        }
Example #12
0
        XmlElement GetCopiedItem(ItemContainerInfo container, CopyResult[] results)
        {
            var result = results[0];

            if (result.ErrorCode != CopyErrorCode.Success)
            {
                throw new ApplicationException(result.ErrorMessage +
                                               " (" + result.ErrorCode + ")");
            }
            var name         = PathUtility.GetChildName(result.DestinationUrl);
            var relativePath = PathUtility.JoinPath(container.ListRelativePath, name);

            return(QueryItem(container.List, relativePath));
        }
Example #13
0
        protected override XmlElement RawCopyItem(ItemInfo item, ItemContainerInfo target,
                                                  bool recurse, string newName)
        {
            // Common items have no children and files can have versions as children; we should
            // always enable deep cloning for those terminal objects not to corrupt them.
            var deeply = recurse | !(item is FolderInfo);
            var source = (XmlElement)GetItemXml(item).CloneNode(deeply);
            var lastID = GetLastItemID(item.List);

            InitializeItemClones(source, ref lastID);
            PlaceItemXml(source, target, newName);
            SaveSite(source);
            return(source);
        }
Example #14
0
        protected override XmlElement RawCopyItem(ItemInfo item, ItemContainerInfo target,
                                                  bool recurse, string newName)
        {
            if (string.IsNullOrEmpty(newName))
            {
                newName = item.Name;
            }
            Log.Verbose("Copying item at /{0} to /{1} as {2}.", item.Path, target.Path, newName);
            CopyResult[] results;
            var          originalUrl = PathUtility.JoinPath(Drive.WebUrl, item.Path);
            var          copyUrl     = PathUtility.JoinPath(Drive.WebUrl, target.Path, newName);

            GetService <Copy>(item.List.Web.Path).CopyIntoItemsLocal(
                originalUrl, new[] { copyUrl }, out results);
            return(GetCopiedItem(target, results));
        }
Example #15
0
        protected override XmlElement RawMoveItem(ItemInfo item, ItemContainerInfo target)
        {
            // If the item is moved to other list it must be copied and the original deleted.
            if (item.List.Path.EqualsCI(target.List.Path))
            {
                Log.Verbose("Moving item at /{0} to /{1}.", item.Path, target.Path);
                var input   = new XmlDocument();
                var updates = AddCamlMove(input, item, target);
                var output  = (XmlElement)GetService <Lists>(item.List.Web.Path).UpdateListItems(
                    item.List.StringID, updates);
                CheckUpdateResult(output);
                var relativePath = PathUtility.JoinPath(target.ListRelativePath, item.Name);
                return(QueryItem(target.List, relativePath));
            }
            var copy = RawCopyItem(item, target, true, null);

            RemoveItem(item);
            return(copy);
        }
Example #16
0
        public ItemInfo AddItem(ItemContainerInfo container, string name)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.IsEmpty())
            {
                throw new ArgumentException("The name of a new item must not be empty.");
            }
            var item = AddItemDirectly(container, name);

            AddCachedItem(item, container);
            return(item);
        }
Example #17
0
        public FolderInfo AddFolder(ItemContainerInfo container, string name)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("The name of a new folder must not be empty.");
            }
            var folder = (FolderInfo)AddFolderDirectly(container, name);

            AddCachedItem(folder, container);
            return(folder);
        }
Example #18
0
        XmlElement RawAddItem(ItemContainerInfo container, string name, ItemType type)
        {
            var target = GetItemContainerXml(container);

            if (HasItemXml(target, name))
            {
                throw new ApplicationException("Item with the same name found.");
            }
            var source = target.OwnerDocument.CreateElement("Item");
            var id     = GetLastItemID(container.List) + 1;

            if (type != ItemType.Common)
            {
                source.SetAttribute("Type", type.ToString());
            }
            source.SetAttribute("ID", id.ToStringI());
            source.SetAttribute("UniqueID", Guid.NewGuid().ToString("D"));
            source.SetAttribute("Name", name);
            source.SetAttribute("Created", DateForNow);
            target.AppendChild(source);
            return(source);
        }
Example #19
0
        protected override XmlElement RawMoveItem(ItemInfo item, ItemContainerInfo container)
        {
            // Item move modifies both old and new parent elements but not the moved item itself.
            // the parent is touched after the item is placed to the new container because that
            // operation can fail if an item of the same name has been already there. If the item
            // is moved to other list it needs a new ID unique in the new list.
            var source = GetItemXml(item);
            var parent = (XmlElement)source.ParentNode;

            source.Remove();
            var target = GetItemContainerXml(container);

            PlaceItemXml(source, target);
            if (!item.List.Path.EqualsCI(container.List.Path))
            {
                var lastID = GetLastItemID(container.List);
                source.SetAttribute("ID", (++lastID).ToStringI());
            }
            TouchItemXml(parent);
            TouchItemXml(target);
            SaveSite(target);
            return(source);
        }
Example #20
0
 void PlaceItemXml(XmlElement item, ItemContainerInfo container, string newName = null)
 {
     PlaceItemXml(item, GetItemContainerXml(container), newName);
 }
Example #21
0
 public abstract IEnumerable<ItemInfo> GetItems(ItemContainerInfo container);
Example #22
0
 protected override IEnumerable <ItemInfo> GetItemsDirectly(ItemContainerInfo container)
 {
     return(QueryItems(container).Select(source =>
                                         CreateItemInfo(container, source)).ToList());
 }
Example #23
0
 protected abstract XmlElement RawMoveItem(ItemInfo item, ItemContainerInfo target);
Example #24
0
 protected abstract ItemInfo AddItemDirectly(ItemContainerInfo container, string name);
Example #25
0
 protected override IEnumerable<XmlElement> QueryItems(ItemContainerInfo container)
 {
     // Item container can contain only items - Item elements - no need for XPath here.
     return GetItemContainerXml(container).ChildNodes.OfType<XmlElement>();
 }
Example #26
0
 protected override ItemInfo CopyItemDirectly(ItemInfo item, ItemContainerInfo target,
                                              bool recurse, string newName)
 {
     return CreateItemInfo(item.List, RawCopyItem(item, target, recurse, newName));
 }
Example #27
0
 protected override ItemInfo AddItemDirectly(ItemContainerInfo container, string name)
 {
     return CreateItemInfo(container, RawAddItem(container, name));
 }
Example #28
0
 protected override FolderInfo AddFolderDirectly(ItemContainerInfo container, string name)
 {
     return (FolderInfo) CreateItemInfo(container, RawAddFolder(container, name));
 }
Example #29
0
 ItemInfo CreateItemInfo(ItemContainerInfo container, XmlElement source)
 {
     var path = PathUtility.JoinPath(container.Path, GetItemName(source));
     return CreateItemInfo(container.List, path, source);
 }
Example #30
0
 protected abstract XmlElement RawMoveItem(ItemInfo item, ItemContainerInfo target);
Example #31
0
 public ItemInfo CopyItem(ItemInfo item, ItemContainerInfo target,
                                  bool recurse, string newName)
 {
     if (target == null)
         throw new ArgumentNullException("target");
     var copy = CopyItemDirectly(item, target, recurse, newName);
     AddCachedItem(copy, target);
     return copy;
 }
Example #32
0
 public ItemInfo MoveItem(ItemInfo item, ItemContainerInfo target)
 {
     if (target == null)
         throw new ArgumentNullException("target");
     var moved = MoveItemDirectly(item, target);
     RemoveCachedItem(item);
     AddCachedItem(moved, target);
     return moved;
 }
Example #33
0
 protected override XmlElement RawAddItem(ItemContainerInfo container, string name)
 {
     var item = RawAddItem(container, name, ItemType.Common);
     SaveSite(item);
     return item;
 }
Example #34
0
 protected abstract XmlElement RawCopyItem(ItemInfo item, ItemContainerInfo target,
                                           bool recurse, string newName);
Example #35
0
 protected override XmlElement RawCopyItem(ItemInfo item, ItemContainerInfo target,
                                           bool recurse, string newName)
 {
     // Common items have no children and files can have versions as children; we should
     // always enable deep cloning for those terminal objects not to corrupt them.
     var deeply = recurse | !(item is FolderInfo);
     var source = (XmlElement) GetItemXml(item).CloneNode(deeply);
     var lastID = GetLastItemID(item.List);
     InitializeItemClones(source, ref lastID);
     PlaceItemXml(source, target, newName);
     SaveSite(source);
     return source;
 }
Example #36
0
 protected abstract XmlElement RawAddItem(ItemContainerInfo container, string name);
Example #37
0
 protected override XmlElement RawMoveItem(ItemInfo item, ItemContainerInfo container)
 {
     // Item move modifies both old and new parent elements but not the moved item itself.
     // the parent is touched after the item is placed to the new container because that
     // operation can fail if an item of the same name has been already there. If the item
     // is moved to other list it needs a new ID unique in the new list.
     var source = GetItemXml(item);
     var parent = (XmlElement) source.ParentNode;
     source.Remove();
     var target = GetItemContainerXml(container);
     PlaceItemXml(source, target);
     if (!item.List.Path.EqualsCI(container.List.Path)) {
         var lastID = GetLastItemID(container.List);
         source.SetAttribute("ID", (++lastID).ToStringI());
     }
     TouchItemXml(parent);
     TouchItemXml(target);
     SaveSite(target);
     return source;
 }
Example #38
0
 protected abstract IEnumerable <XmlElement> QueryItems(ItemContainerInfo container);
Example #39
0
 XmlElement GetItemContainerXml(ItemContainerInfo container)
 {
     return container == container.List ? GetListXml((ListInfo) container) :
         GetItemXml((FolderInfo) container);
 }
Example #40
0
 protected override ItemInfo MoveItemDirectly(ItemInfo item, ItemContainerInfo target)
 {
     return CreateItemInfo(item.List, RawMoveItem(item, target));
 }
Example #41
0
 public ItemInfo AddItem(ItemContainerInfo container, string name)
 {
     if (container == null)
         throw new ArgumentNullException("container");
     if (name == null)
         throw new ArgumentNullException("name");
     if (name.IsEmpty())
         throw new ArgumentException("The name of a new item must not be empty.");
     var item = AddItemDirectly(container, name);
     AddCachedItem(item, container);
     return item;
 }
Example #42
0
 protected abstract IEnumerable<XmlElement> QueryItems(ItemContainerInfo container);
Example #43
0
 protected abstract IEnumerable<ItemInfo> GetItemsDirectly(ItemContainerInfo container);
Example #44
0
 protected override IEnumerable <XmlElement> QueryItems(ItemContainerInfo container)
 {
     // Item container can contain only items - Item elements - no need for XPath here.
     return(GetItemContainerXml(container).ChildNodes.OfType <XmlElement>());
 }
Example #45
0
 void AddCachedItem(ItemInfo item, ItemContainerInfo container = null)
 {
     if (container == null) {
         var path = PathUtility.GetParentPath(item.Path);
         Info parent;
         if (!path.IsEmpty() && Cache.TryGetObject(path, out parent))
             container = (ItemContainerInfo) parent;
     }
     if (container != null) {
         var list = container as ListInfo;
         var folder = container as FolderInfo;
         var items = list != null ? list.ChildItems : folder.ChildItems;
         if (items != null) {
             items = items.Concat(new[] { item }).ToList();
             if (list != null)
                 list.ChildItems = items;
             else
                 folder.ChildItems = items;
         }
     }
 }
Example #46
0
 XmlElement GetItemContainerXml(ItemContainerInfo container)
 {
     return(container == container.List ? GetListXml((ListInfo)container) :
            GetItemXml((FolderInfo)container));
 }
Example #47
0
 protected override IEnumerable<ItemInfo> GetItemsDirectly(ItemContainerInfo container)
 {
     return QueryItems(container).Select(source =>
                 CreateItemInfo(container, source)).ToList();
 }
Example #48
0
 public override IEnumerable<ItemInfo> GetItems(ItemContainerInfo container)
 {
     // I wouldn't so aggressively store the child items but calling Get-ChildItem cmdlet
     // checks multiple times the existence of a "*" child. Because this triggers the
     // children retrieval I'd better cache them. The cache stores resolved SharePoint
     // objects by their path. I could adapt the cache to accommodate the collections of
     // children too but it was easier to utilize an internal property in the parent web
     // instance, which even performs better.
     if (container == null)
         throw new ArgumentNullException("container");
     var list = container as ListInfo;
     var folder = container as FolderInfo;
     var items = list != null ? list.ChildItems : folder.ChildItems;
     if (items == null || !Cache.Check()) {
         items = GetItemsDirectly(container);
         if (list != null)
             list.ChildItems = items;
         else
             folder.ChildItems = items;
     }
     return items;
 }
Example #49
0
 protected override ItemInfo AddItemDirectly(ItemContainerInfo container, string name)
 {
     return(CreateItemInfo(container, RawAddItem(container, name)));
 }
Example #50
0
 protected abstract FolderInfo AddFolderDirectly(ItemContainerInfo container, string name);
Example #51
0
 protected abstract XmlElement RawCopyItem(ItemInfo item, ItemContainerInfo target,
                                           bool recurse, string newName);
Example #52
0
 protected abstract ItemInfo CopyItemDirectly(ItemInfo item, ItemContainerInfo target,
                                              bool recurse, string newName);
Example #53
0
 protected abstract XmlElement RawAddItem(ItemContainerInfo container, string name);
Example #54
0
 protected abstract ItemInfo MoveItemDirectly(ItemInfo item, ItemContainerInfo target);
Example #55
0
 protected override ItemInfo MoveItemDirectly(ItemInfo item, ItemContainerInfo target)
 {
     return(CreateItemInfo(item.List, RawMoveItem(item, target)));
 }
Example #56
0
 void PlaceItemXml(XmlElement item, ItemContainerInfo container, string newName = null)
 {
     PlaceItemXml(item, GetItemContainerXml(container), newName);
 }
Example #57
0
 protected override FolderInfo AddFolderDirectly(ItemContainerInfo container, string name)
 {
     return((FolderInfo)CreateItemInfo(container, RawAddFolder(container, name)));
 }
Example #58
0
 public abstract IEnumerable <ItemInfo> GetItems(ItemContainerInfo container);
Example #59
0
 XmlElement RawAddItem(ItemContainerInfo container, string name, ItemType type)
 {
     var target = GetItemContainerXml(container);
     if (HasItemXml(target, name))
         throw new ApplicationException("Item with the same name found.");
     var source = target.OwnerDocument.CreateElement("Item");
     var id = GetLastItemID(container.List) + 1;
     if (type != ItemType.Common)
         source.SetAttribute("Type", type.ToString());
     source.SetAttribute("ID", id.ToStringI());
     source.SetAttribute("UniqueID", Guid.NewGuid().ToString("D"));
     source.SetAttribute("Name", name);
     source.SetAttribute("Created", DateForNow);
     target.AppendChild(source);
     return source;
 }