public SPListItemCollectionPositionInstance Construct(SPListItemCollectionPosition position) { if (position == null) { throw new ArgumentNullException("position"); } return(new SPListItemCollectionPositionInstance(this.InstancePrototype, position)); }
public List <T> ExecuteListQuery(string CAML, int rowLimit, string pagingInfo, out SPListItemCollection collection) { collection = null; List <T> results = new List <T>(); SPQuery query = new SPQuery(); query.Query = CAML; if (rowLimit != 0) { query.RowLimit = (uint)rowLimit; } //use for paging if (!string.IsNullOrEmpty(pagingInfo)) { SPListItemCollectionPosition position = new SPListItemCollectionPosition(pagingInfo); query.ListItemCollectionPosition = position; } if (Source == null) { return(results); } var data = Source.GetItems(query); //use for paging collection = data; foreach (SPListItem item in data) { var resolvedItem = CreateInstance(item); if (resolvedItem != null) { results.Add(resolvedItem); } } return(results); }
protected override void Render(HtmlTextWriter writer) { base.Render(writer); System.Text.StringBuilder sb = new System.Text.StringBuilder(); string CAMLQuery = null; try { if (this._listId != null && this._viewName != null) { using (SPSite site = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb web = site.OpenWeb(this.ListWebId)) { SPList selectedList = web.Lists[this._listId]; SPListItemCollection items = selectedList.Items; SPView defaultView = selectedList.Views[this._viewName]; uint itemsPerPage = defaultView.RowLimit; SPViewFieldCollection fields = defaultView.ViewFields; System.Collections.Specialized.StringCollection stringCol = fields.ToStringCollection(); if (Page.Request.QueryString != null && Page.Request.QueryString.Count > 0) { FilterEngine filterEngine = new FilterEngine(Page.Request.QueryString, selectedList); CAMLQuery = filterEngine.CAMLQuery; } // Nik20121105 - Write the table headers; sb.AppendLine("<table class=\"wet-boew-zebra\">"); sb.Append("<tr>"); foreach (string field in stringCol) { sb.Append("<th>" + selectedList.Fields.GetFieldByInternalName(field).Title + "</th>"); } sb.Append("</tr>"); if (CAMLQuery == null) { CAMLQuery = ""; } SPQuery query = new SPQuery(); query.Query = CAMLQuery; query.RowLimit = itemsPerPage; items = selectedList.GetItems(query); if (Page.Request.QueryString["p_ID"] != null) { string prev = ""; if (Page.Request.QueryString["PagedPrev"] == "TRUE") { prev = "&PagedPrev=TRUE"; } SPListItemCollectionPosition position = new SPListItemCollectionPosition("Paged=TRUE&p_ID=" + Page.Request.QueryString["p_ID"] + prev); query.ListItemCollectionPosition = position; } string lastId = ""; foreach (SPListItem item in items) { sb.AppendLine("<tr>"); bool firstCol = true; foreach (string field in stringCol) { if (firstCol) { firstCol = false; string itemUrl = string.Empty; SPField test = item.Fields.TryGetFieldByStaticName("BdcIdentity"); if (test != null) { itemUrl = HttpUtility.UrlEncode(this._listWeb + "/_layouts/listform.aspx?PageType=4&ListId={" + this._listId.ToString() + "}&ID=" + item["BdcIdentity"].ToString()); } else { itemUrl = HttpUtility.UrlEncode(this._listWeb + "/_layouts/listform.aspx?PageType=4&ListId={" + this._listId.ToString() + "}&ID=" + item.ID.ToString()); } string renderedField = string.Empty; if (item[field] != null) { renderedField = item[field].ToString(); } sb.AppendLine("<td><a href=\"" + this.ItemViewerUrl + "?ItemUrl=" + itemUrl + "\">" + renderedField.Replace("string;#", "").Replace("datetime;#", "").Replace("number;#", "") + "</a></td>"); } else { string renderedField = string.Empty; if (item[field] != null) { renderedField = FieldRenderer.RenderField(item[field].ToString(), item.Fields.GetFieldByInternalName(field)); } sb.AppendLine("<td>" + renderedField + "</td>"); } } sb.AppendLine("</tr>"); lastId = item.ID.ToString(); } sb.AppendLine("</table>"); string curUrl = Page.Request.Url.OriginalString; string forwardUrl = curUrl.Replace("p_ID=" + Page.Request.QueryString["p_ID"], "p_ID=" + lastId).Replace("&PagedPrev=TRUE", ""); string prevUrl = curUrl.Replace("p_ID=" + Page.Request.QueryString["p_ID"], "p_ID=" + items[0].ID.ToString()); if (forwardUrl.IndexOf("p_ID") < 0) { if (!forwardUrl.Contains("?")) { forwardUrl += "?p_ID=" + lastId; } else { forwardUrl += "&p_ID=" + lastId; } } if (prevUrl.IndexOf("p_ID") < 0) { if (!prevUrl.Contains("?")) { prevUrl += "?p_ID=" + items[0].ID.ToString(); } else { prevUrl += "&p_ID=" + items[0].ID.ToString(); } } if (!prevUrl.Contains("PagedPrev")) { prevUrl += "&PagedPrev=TRUE"; } if (CAMLQuery != null) { SPQuery newQuery = new SPQuery(); newQuery.Query = CAMLQuery; if (items[0].ID != selectedList.GetItems(newQuery)[0].ID) { sb.AppendLine("<a href=\"" + prevUrl + "\">< Previous</a> "); } } else if (items[0].ID != selectedList.Items[0].ID) { sb.AppendLine("<a href=\"" + prevUrl + "\">< Previous</a> "); } if ((int.Parse(lastId) + itemsPerPage) < selectedList.Items.Count) { sb.AppendLine("<a href=\"" + forwardUrl + "\">Next ></a>"); } writer.Write(sb.ToString()); } } } } catch (Exception ex) { LogEngine.Log(ex, "Accessible Lists"); } }
public DataTable GetItemDataTableDocument(string ListURL, int pageSize, int pageIndex, string strSortFieldName, string strDataType, bool blAscendingTrueFalse, string strViewFields, string strQuery, string Folder) { if (ListURL == string.Empty) return new DataTable(); DataTable dt = new DataTable(); SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(ListURL)) { using (SPWeb web = site.OpenWeb()) { SPList objList = web.GetList(ListURL); SPQuery spQry = null; pageIndex = pageIndex / pageSize; if (pageIndex < 1) { spQry = new SPQuery(); spQry.RowLimit = (uint)pageSize; spQry.ViewFields = strViewFields; if (Folder != string.Empty) spQry.Folder = objList.ParentWeb.GetFolder(Folder); spQry.ViewAttributes = "Scope=\"Recursive\""; spQry.Query = strQuery + "<OrderBy><FieldRef Name=\"" + strSortFieldName + "\" Ascending=\"" + blAscendingTrueFalse + "\" /></OrderBy>"; } else { spQry = new SPQuery(); spQry.RowLimit = (uint)(pageIndex * pageSize); if (Folder != string.Empty) spQry.Folder = objList.ParentWeb.GetFolder(Folder); spQry.ViewFields = "<FieldRef Name='Id'/><FieldRef Name='" + strSortFieldName + "'/>"; spQry.ViewAttributes = "Scope=\"Recursive\""; spQry.Query = strQuery + "<OrderBy><FieldRef Name=\"" + strSortFieldName + "\" Ascending=\"" + blAscendingTrueFalse + "\" /></OrderBy>"; SPListItemCollection objItemCollection = objList.GetItems(spQry); spQry = new SPQuery(); spQry.RowLimit = (uint)pageSize; spQry.ViewFields = strViewFields; if (Folder != string.Empty) spQry.Folder = objList.ParentWeb.GetFolder(Folder); spQry.ViewAttributes = "Scope=\"Recursive\""; spQry.Query = strQuery + "<OrderBy><FieldRef Name=\"" + strSortFieldName + "\" Ascending=\"" + blAscendingTrueFalse + "\" /></OrderBy>"; ; SPListItemCollectionPosition objSPListColPos = null; if (strDataType.ToUpper() == "DATETIME") { string DateTime = objItemCollection[objItemCollection.Count - 1][strSortFieldName].ToString(); //.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1]; objSPListColPos = new SPListItemCollectionPosition("Paged=TRUE" + "&p_" + strSortFieldName + "=" + SPEncode.UrlEncode(System.DateTime.Parse(DateTime).ToUniversalTime().ToString("yyyyMMdd hh:mm:ss")) + "&p_ID=" + objItemCollection[objItemCollection.Count - 1]["ID"].ToString()); } else { objSPListColPos = new SPListItemCollectionPosition("Paged=TRUE" + "&p_" + strSortFieldName + "=" + objItemCollection[objItemCollection.Count - 1][strSortFieldName].ToString() + "&p_ID=" + objItemCollection[objItemCollection.Count - 1]["ID"].ToString()); } spQry.ListItemCollectionPosition = objSPListColPos; } dt = objList.GetItems(spQry).GetDataTable(); } } }); return dt; }
static void Main(string[] args) { #region Using Client Object Model ClientContext clientContext = new ClientContext(strSiteURL); List lst = clientContext.Web.Lists.GetByTitle(strList); ListItemCollectionPosition itemPosition = null; Console.WriteLine("Displaying items in a batch manner..."); Console.WriteLine("-------------------------------------"); while (true) { CamlQuery camlQuery = new CamlQuery(); camlQuery.ListItemCollectionPosition = itemPosition; camlQuery.ViewXml = "<View><ViewFields><FieldRef Name='Title'/>" + "</ViewFields><RowLimit>" + itemsPerPage + "</RowLimit></View>"; ListItemCollection collListItem = lst.GetItems(camlQuery); clientContext.Load(collListItem); clientContext.ExecuteQuery(); itemPosition = collListItem.ListItemCollectionPosition; foreach (ListItem oListItem in collListItem) { Console.WriteLine("Title: {0}", oListItem["Title"]); } if (itemPosition == null) { break; } Console.WriteLine("\n" + itemPosition.PagingInfo + "\n"); Console.WriteLine("Next page..."); Console.ReadLine(); } Console.WriteLine(""); Console.WriteLine("Press enter to exit"); Console.ReadLine(); #endregion #region Using Server Object Model using (SPSite SiteCollection = new SPSite(strSiteURL)) { using (SPWeb Site = SiteCollection.OpenWeb()) { SPList TargetList = Site.Lists.TryGetList(strList); SPListItemCollectionPosition splistitemPosition = null; Console.WriteLine("Displaying items in a batch manner..."); Console.WriteLine("-------------------------------------"); while (true) { SPQuery query = new SPQuery(); query.ListItemCollectionPosition = splistitemPosition; query.ViewXml = "<View><ViewFields><FieldRef Name='Title'/>" + "</ViewFields><RowLimit>" + itemsPerPage + "</RowLimit></View>"; SPListItemCollection apcollListItem = TargetList.GetItems(query); splistitemPosition = apcollListItem.ListItemCollectionPosition; foreach (SPListItem oListItem in apcollListItem) { Console.WriteLine("Title: {0}", oListItem["Title"]); } if (splistitemPosition == null) { break; } Console.WriteLine("\n" + splistitemPosition.PagingInfo + "\n"); Console.WriteLine("Next page..."); Console.ReadLine(); } } } Console.WriteLine(""); Console.WriteLine("Press enter to exit"); Console.ReadLine(); #endregion }
public SPListItemCollectionPositionInstance(ObjectInstance prototype, SPListItemCollectionPosition position) : this(prototype) { this.m_position = position; }
protected override void Render(HtmlTextWriter writer) { base.Render(writer); System.Text.StringBuilder sb = new System.Text.StringBuilder(); string CAMLQuery = null; try { if (this._listId != null && this._viewName != null) { using (SPSite site = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb web = site.OpenWeb(this.ListWebId)) { SPList selectedList = web.Lists[this._listId]; SPListItemCollection items = selectedList.Items; SPView defaultView = selectedList.Views[this._viewName]; uint itemsPerPage = defaultView.RowLimit; SPViewFieldCollection fields = defaultView.ViewFields; System.Collections.Specialized.StringCollection stringCol = fields.ToStringCollection(); if (Page.Request.QueryString != null && Page.Request.QueryString.Count > 0) { FilterEngine filterEngine = new FilterEngine(Page.Request.QueryString, selectedList); CAMLQuery = filterEngine.CAMLQuery; } // Nik20121105 - Write the table headers; sb.AppendLine("<table class=\"wet-boew-zebra\">"); sb.Append("<tr>"); foreach (string field in stringCol) { sb.Append("<th>" + selectedList.Fields.GetFieldByInternalName(field).Title + "</th>"); } sb.Append("</tr>"); if (CAMLQuery == null) { CAMLQuery = ""; } SPQuery query = new SPQuery(); query.Query = CAMLQuery; query.RowLimit = itemsPerPage; items = selectedList.GetItems(query); if (Page.Request.QueryString["p_ID"] != null) { string prev = ""; if (Page.Request.QueryString["PagedPrev"] == "TRUE") prev = "&PagedPrev=TRUE"; SPListItemCollectionPosition position = new SPListItemCollectionPosition("Paged=TRUE&p_ID=" + Page.Request.QueryString["p_ID"] + prev); query.ListItemCollectionPosition = position; } string lastId = ""; foreach (SPListItem item in items) { sb.AppendLine("<tr>"); bool firstCol = true; foreach (string field in stringCol) { if (firstCol) { firstCol = false; string itemUrl = string.Empty; SPField test = item.Fields.TryGetFieldByStaticName("BdcIdentity"); if(test != null) itemUrl = HttpUtility.UrlEncode(this._listWeb + "/_layouts/listform.aspx?PageType=4&ListId={" + this._listId.ToString() + "}&ID=" + item["BdcIdentity"].ToString()); else itemUrl = HttpUtility.UrlEncode(this._listWeb + "/_layouts/listform.aspx?PageType=4&ListId={" + this._listId.ToString() + "}&ID=" + item.ID.ToString()); string renderedField = string.Empty; if (item[field] != null) renderedField = item[field].ToString(); sb.AppendLine("<td><a href=\"" + this.ItemViewerUrl + "?ItemUrl=" + itemUrl + "\">" + renderedField.Replace("string;#", "").Replace("datetime;#", "").Replace("number;#", "") + "</a></td>"); } else { string renderedField = string.Empty; if(item[field] != null) renderedField = FieldRenderer.RenderField(item[field].ToString(), item.Fields.GetFieldByInternalName(field)); sb.AppendLine("<td>" + renderedField + "</td>"); } } sb.AppendLine("</tr>"); lastId = item.ID.ToString(); } sb.AppendLine("</table>"); string curUrl = Page.Request.Url.OriginalString; string forwardUrl = curUrl.Replace("p_ID=" + Page.Request.QueryString["p_ID"], "p_ID=" + lastId).Replace("&PagedPrev=TRUE", ""); string prevUrl = curUrl.Replace("p_ID=" + Page.Request.QueryString["p_ID"], "p_ID=" + items[0].ID.ToString()); if(forwardUrl.IndexOf("p_ID") < 0) { if(!forwardUrl.Contains("?")) forwardUrl += "?p_ID=" + lastId; else forwardUrl += "&p_ID=" + lastId; } if (prevUrl.IndexOf("p_ID") < 0) { if (!prevUrl.Contains("?")) prevUrl += "?p_ID=" + items[0].ID.ToString(); else prevUrl += "&p_ID=" + items[0].ID.ToString(); } if (!prevUrl.Contains("PagedPrev")) prevUrl += "&PagedPrev=TRUE"; if (CAMLQuery != null) { SPQuery newQuery = new SPQuery(); newQuery.Query = CAMLQuery; if(items[0].ID != selectedList.GetItems(newQuery)[0].ID) sb.AppendLine("<a href=\"" + prevUrl + "\">< Previous</a> "); } else if(items[0].ID != selectedList.Items[0].ID) sb.AppendLine("<a href=\"" + prevUrl + "\">< Previous</a> "); if((int.Parse(lastId) + itemsPerPage) < selectedList.Items.Count) sb.AppendLine("<a href=\"" + forwardUrl + "\">Next ></a>"); writer.Write(sb.ToString()); } } } } catch (Exception ex) { LogEngine.Log(ex, "Accessible Lists"); } }