Sharepoint文档的CAML分页及相关筛选记录

写这篇文章的初衷是因为其他的业务系统要调用sharepoint的文档库信息,使其他的系统也可以获取sharepoint文档库的信息列表。在这个过程中尝试过用linq to sharepoint来获取文档列表,不过看了其他人对linq在sharepoint的分页效率的评价,不是很好,详情请戳这里。所以尝试用CAML来分页,在此记录以备忘。测试了一下,两万条分页毫无压力。

 

代码如下:

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CAMLDemo.Layouts.CAMLDemo
{
    public partial class ApplicationPage1 : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            SPList docLib = web.Lists["文档"];

            SPQuery query = new SPQuery();

            #region 1.文档库下所有文档及文件夹
            //递归查询,加了下面的参数的话会将文档库的所有文档及文件夹查询出来
            //否则只查询根目录下的文档及文件夹
            //query.ViewAttributes = "Scope='RecursiveAll'";
            #endregion

            #region 2.文档库下所有的文档
            //查询文档库下的文档
            //query.ViewAttributes = "Scope='Recursive'";

            ////或者

            //query.ViewAttributes = "Scope='RecursiveAll'";
            //query.Query =
            //    @"<Where>
            //        <Eq>
            //            <FieldRef Name='FSObjType' />
            //            <Value Type='Integer'>0</Value>
            //        </Eq>
            //    </Where>";  
            #endregion

            #region 3.文档库下的所有文件夹
            //查询文档库下的所有文件夹
            //query.ViewAttributes = "Scope='RecursiveAll'";
            //query.Query = 
            //    @"<Where>
            //        <Eq>
            //            <FieldRef Name='FSObjType' />
            //            <Value Type='Integer'>1</Value>
            //        </Eq>
            //    </Where>";            
            #endregion

            #region 4.模糊查询指定名称的项目
            //模糊查询指定名称的项目
            //query.ViewAttributes = "Scope='RecursiveAll'";
            //query.Query =@"
            //<Where>
            //    <Contains>
            //        <FieldRef Name='FileLeafRef'/>
            //        <Value Type='Text'>测试</Value>
            //    </Contains>
            //</Where>";
            #endregion

            #region 5.查询指定文件夹下的项目
            //查询指定文件夹下的项目
            query.Folder = docLib.RootFolder.SubFolders["system"];
            #endregion

            #region 翻页逻辑,请自行修改
            //每页的文档数
            query.RowLimit = 10;

            //用ViewState来存放上次翻页后,下一页第一条记录的ID号,
            //将此ID号赋值给p_ID.作为本次翻页的第一条记录ID。
            string p_ID = (string)ViewState["itemIndex"];

            SPListItemCollectionPosition position;
            if (!string.IsNullOrEmpty(p_ID))
                position = new SPListItemCollectionPosition(string.Format("Paged=TRUE&p_ID={0}", p_ID));
            else
                position = new SPListItemCollectionPosition(string.Format("Paged=TRUE&p_ID=0"));//从第一页开始
            query.ListItemCollectionPosition = position;

            SPListItemCollection items = docLib.GetItems(query);

            ViewState["itemIndex"] = GetPageItemID(items);
            #endregion

            #region 显示
            string html = "";
            foreach (SPListItem item in items)
            {
                html += item.Name + "<br>";
            }

            lblText.Text = html;
            #endregion
        }

        string GetPageItemID(SPListItemCollection items)
        {
            try
            {
                string page = items.ListItemCollectionPosition.PagingInfo.Split(new string[] { "&p_ID=" }, StringSplitOptions.RemoveEmptyEntries)[1].Split('&')[0];
                return page;
            }
            catch
            {
                return "0";
            }
        }
    }
}

  

批量添加记录代码

            SPWeb web = SPContext.Current.Web;

            web.AllowUnsafeUpdates = true;

            SPList list = web.Lists["test"];

            //for (int i = 1; i < 20000;i++ )
            //{ 
            //    SPListItem oItem1 = list.Items.Add(); 
            //    oItem1["Title"] = i;
            //    oItem1.Update();
            //}

            Guid id = list.ID;

            StringBuilder sbDelete = new StringBuilder();
            sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

            for (int i = 10001; i <= 20000; i++)
            {
                sbDelete.Append("<Method>");
                sbDelete.Append("<SetList Scope=\"Request\">" + id + "</SetList>");
                sbDelete.Append("<SetVar Name=\"ID\">New</SetVar>");
                sbDelete.Append("<SetVar Name=\"Cmd\">Save</SetVar>");
                sbDelete.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">item" + i + "</SetVar>");
                sbDelete.Append("</Method>");
            }

            sbDelete.Append("</Batch>");

            try
            {
                web.ProcessBatchData(sbDelete.ToString());
            }
            catch
            {

                throw;
            }

            web.AllowUnsafeUpdates = false;

 

posted @ 2013-12-27 16:49  poisson_notes  阅读(3166)  评论(0编辑  收藏  举报