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); }
protected override XmlElement RawAddItem(ItemContainerInfo container, string name) { var item = RawAddItem(container, name, ItemType.Common); SaveSite(item); return(item); }
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; } } } }
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)); }
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); }
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); }
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; }
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); }
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); }
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>()); }
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); }
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)); }
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); }
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)); }
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); }
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); }
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); }
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); }
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); }
void PlaceItemXml(XmlElement item, ItemContainerInfo container, string newName = null) { PlaceItemXml(item, GetItemContainerXml(container), newName); }
public abstract IEnumerable<ItemInfo> GetItems(ItemContainerInfo container);
protected override IEnumerable <ItemInfo> GetItemsDirectly(ItemContainerInfo container) { return(QueryItems(container).Select(source => CreateItemInfo(container, source)).ToList()); }
protected abstract XmlElement RawMoveItem(ItemInfo item, ItemContainerInfo target);
protected abstract ItemInfo AddItemDirectly(ItemContainerInfo container, string name);
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>(); }
protected override ItemInfo CopyItemDirectly(ItemInfo item, ItemContainerInfo target, bool recurse, string newName) { return CreateItemInfo(item.List, RawCopyItem(item, target, recurse, newName)); }
protected override ItemInfo AddItemDirectly(ItemContainerInfo container, string name) { return CreateItemInfo(container, RawAddItem(container, name)); }
protected override FolderInfo AddFolderDirectly(ItemContainerInfo container, string name) { return (FolderInfo) CreateItemInfo(container, RawAddFolder(container, name)); }
ItemInfo CreateItemInfo(ItemContainerInfo container, XmlElement source) { var path = PathUtility.JoinPath(container.Path, GetItemName(source)); return CreateItemInfo(container.List, path, source); }
protected abstract XmlElement RawMoveItem(ItemInfo item, ItemContainerInfo target);
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; }
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; }
protected override XmlElement RawAddItem(ItemContainerInfo container, string name) { var item = RawAddItem(container, name, ItemType.Common); SaveSite(item); return item; }
protected abstract XmlElement RawCopyItem(ItemInfo item, ItemContainerInfo target, bool recurse, string newName);
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; }
protected abstract XmlElement RawAddItem(ItemContainerInfo container, string name);
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; }
protected abstract IEnumerable <XmlElement> QueryItems(ItemContainerInfo container);
XmlElement GetItemContainerXml(ItemContainerInfo container) { return container == container.List ? GetListXml((ListInfo) container) : GetItemXml((FolderInfo) container); }
protected override ItemInfo MoveItemDirectly(ItemInfo item, ItemContainerInfo target) { return CreateItemInfo(item.List, RawMoveItem(item, target)); }
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; }
protected abstract IEnumerable<XmlElement> QueryItems(ItemContainerInfo container);
protected abstract IEnumerable<ItemInfo> GetItemsDirectly(ItemContainerInfo container);
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>()); }
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; } } }
XmlElement GetItemContainerXml(ItemContainerInfo container) { return(container == container.List ? GetListXml((ListInfo)container) : GetItemXml((FolderInfo)container)); }
protected override IEnumerable<ItemInfo> GetItemsDirectly(ItemContainerInfo container) { return QueryItems(container).Select(source => CreateItemInfo(container, source)).ToList(); }
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; }
protected override ItemInfo AddItemDirectly(ItemContainerInfo container, string name) { return(CreateItemInfo(container, RawAddItem(container, name))); }
protected abstract FolderInfo AddFolderDirectly(ItemContainerInfo container, string name);
protected abstract XmlElement RawCopyItem(ItemInfo item, ItemContainerInfo target, bool recurse, string newName);
protected abstract ItemInfo CopyItemDirectly(ItemInfo item, ItemContainerInfo target, bool recurse, string newName);
protected abstract XmlElement RawAddItem(ItemContainerInfo container, string name);
protected abstract ItemInfo MoveItemDirectly(ItemInfo item, ItemContainerInfo target);
protected override ItemInfo MoveItemDirectly(ItemInfo item, ItemContainerInfo target) { return(CreateItemInfo(item.List, RawMoveItem(item, target))); }
void PlaceItemXml(XmlElement item, ItemContainerInfo container, string newName = null) { PlaceItemXml(item, GetItemContainerXml(container), newName); }
protected override FolderInfo AddFolderDirectly(ItemContainerInfo container, string name) { return((FolderInfo)CreateItemInfo(container, RawAddFolder(container, name))); }
public abstract IEnumerable <ItemInfo> GetItems(ItemContainerInfo container);
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; }