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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM