刚学的时候在网上搜索sharepoint的分页,基本就找到2条代码,看不懂,还好有大神求救。
就把大神的代码贴上来了,直接调用分页,分享给大家
#region -------------------------------------------------------分页 #region 获取分页后的集合 /// <summary> /// 列表分页 /// </summary> /// <param name="listName">列表名称</param> /// <param name="pageSize">每页大小</param> /// <param name="curPage">当前页数</param> /// <param name="recourdCount">总共记录数</param> /// <param name="strWhere">查询条件</param> /// <param name="ViewField">查询的字段</param> /// <returns></returns> public static SPListItemCollection GetPageList(SPList list, int pageSize, int curPage,out int recourdCount, string strWhere) { try { //查询字段 //string viewFields = string.Empty; //查询 SPQuery query = new SPQuery(); query.Query = strWhere; ////查询的字段 //if (!string.IsNullOrEmpty(fKeyWords.ToLower())) //{ // query.ViewFields = "<Where><Eq><FieldRef Name=‘Title‘ /><Value Type=‘Text‘>" + fKeyWords + "</Value></Eq></Where>"; //} //总共记录数 recourdCount = list.GetItems(query).Count; //需要重新实例对象 query = new SPQuery(); //设置每页大小 query.RowLimit = (uint)pageSize; query.Query = strWhere; // query.ViewFields = viewFields; //分页信息 string pageinfo = GetListID(list, pageSize, curPage, strWhere); query.ListItemCollectionPosition = new SPListItemCollectionPosition(pageinfo); SPListItemCollection m_objListItemColl = list.GetItems(query); return m_objListItemColl; } catch { recourdCount = 0; return null; } } #endregion #region 返回分页的信息 /// <summary> /// 返回分页的信息 /// </summary> /// <param name="listName">列表名称</param> /// <param name="pageSize">页大小</param> /// <param name="strWhere">查询条件</param> /// <param name="ViewField">查询的字段</param> /// <returns></returns> public static string GetListID(SPList list, int pageSize, int curPage, string strWhere) { try { SPQuery query = new SPQuery(); //设置查询条件 query.Query = strWhere; //查询的字段 //query.ViewFields = viewFields; //条数限制 query.RowLimit = (uint)((curPage - 1) * pageSize); //查询 SPListItemCollection m_objListItemColl = list.GetItems(query); //得到分页信息 string strInfo = m_objListItemColl.ListItemCollectionPosition.PagingInfo; return strInfo; } catch { return string.Empty; } } #endregion #region//构建查询字段 /// <summary> /// 构建查询字段 /// </summary> /// <param name="fieldNames">字段</param> /// <returns>返回构造好的查询字段</returns> //public static string BuildViewFields(string[] fieldNames) //{ // if (fieldNames == null || fieldNames.Length == 0) // return ""; // string result = ""; // foreach (string fieldName in fieldNames) // { // result = String.Format("{0}<FieldRef Name=\"{1}\" />", result, fieldName); // } // return result; //} #endregion #endregion
原文:http://blog.csdn.net/qq873113580/article/details/21017833