using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SPSEDUproject.BookListWebPart
{
    /// <summary>
    /// WebPart로 SPQuery 클래스를 사용하여 책제목(Title)이 같은 것만 리스트에 출력하기.
    /// </summary>
    [ToolboxItemAttribute(false)]
    public class BookListWebPart : WebPart
    {
        string strBookList = String.Empty;

        protected override void CreateChildControls()
        {
            strBookList = makeBookList();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            writer.Write(strBookList);
        }

        private string makeBookList()
        {
            string tempSTR = String.Empty;

            // 사이트모음/사이트를 잡는다.
            SPWeb web = SPContext.Current.Web;    // SPSite를 생략하고 코딩하는 방법.

            // 리스트를 잡는다.
            SPList books = web.GetList("Lists/edulist");

            // 북리스트를 뿌린다. --> SPQuery 클래스 사용
            SPQuery query = new SPQuery();
            query.RowLimit = 10;    // 상위 10개 행만 필터링한다.(= Top 10)
            query.Query = "<Where><Eq><FieldRef Name='Title'/>" +
                            "<Value Type='Text'>나의책</Value></Eq></Where><OrderBy><FieldRef Name='Title' Ascending='FALSE'/></OrderBy>";

            SPListItemCollection bookcol = books.GetItems(query);

            foreach (SPListItem book in bookcol)
            {
                tempSTR += book["Title"].ToString();
                tempSTR += "<br />";
            }

            return tempSTR;
        }
    }
}



Posted by holland14
: