Search This Blog

Friday, June 25, 2010

Paging for SPList in Sharepoint

Many times we want to display data present in SPList into datagrid. However we don't show all record at any time, instead we display limited records. Here is the code that fetches only required number of records from list.

private SPListItemCollection getItemCollection(SPList objList, int pageSize, int pageIndex, string strSortFiedName, string strdatatype, bool blAscendingTrueFalse, string strViewFilds, string strQuery)
{
SPQuery spQry = new SPQuery();
spQry.Query = strQuery;
totalRecords = objList.GetItems(spQry).Count;

if (pageIndex < spqry =" new" rowlimit =" (uint)pageSize;" viewfields =" strViewFilds;" query =" strQuery">"; ;

PgPreviousBottom.Enabled = false;
PgPreviousTop.Enabled = false;
}
else
{
spQry = new SPQuery();
spQry.RowLimit = (uint)((pageIndex - 1) * pageSize);
spQry.ViewFields = "";

spQry.Query = strQuery + "";

SPListItemCollection objItemCollection = objList.GetItems(spQry);


spQry = new SPQuery();
spQry.RowLimit = (uint)pageSize;
spQry.ViewFields = strViewFilds;

spQry.Query = strQuery + ""; ;

SPListItemCollectionPosition objSPListColPos = null;

if (strdatatype.ToUpper() == "DATETIME")
{
objSPListColPos = new SPListItemCollectionPosition("Paged=TRUE"
+ "&p_" + strSortFiedName + "=" + SPEncode.UrlEncode(System.DateTime.Parse(objItemCollection[objItemCollection.Count - 1][strSortFiedName].ToString()).ToUniversalTime().ToString("yyyyMMdd hh:mm:ss"))
+ "&p_ID=" + objItemCollection[objItemCollection.Count - 1]["ID"].ToString());
}
else
{
objSPListColPos = new SPListItemCollectionPosition("Paged=TRUE"
+ "&p_" + strSortFiedName + "=" + objItemCollection[objItemCollection.Count - 1][strSortFiedName].ToString()
+ "&p_ID=" + objItemCollection[objItemCollection.Count - 1]["ID"].ToString());
}


spQry.ListItemCollectionPosition = objSPListColPos;
}


if (objList.GetItems(spQry).Count < 2)
ddlSortItem.Enabled = false;

return objList.GetItems(spQry);
}

No comments:

Post a Comment