提取不同網站下的列表內容

來源:互聯網
上載者:User

       多話不說了,還是來點實在的,以下代碼實現的是將大網站下的幾個小網站提取出來,並顯示在一個GridView中,前台代碼就省了.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Reflection;

public partial class Controls_NewHref : System.Web.UI.UserControl
...{
    private SPSite site;
    private SPWeb web;
    private SPList list;
    //private SPFolder folder;
    protected DataTable dt;
    protected string[] listUrl;
    protected string viewName;
    protected string[] webUrl;
    protected string[] itemName;
    protected DataView dv;
    int m = 1;

    protected void Page_Load(object sender, EventArgs e)
    ...{
        listUrl = new string[] ...{ "Lists/List7/", "Lists/List1/" };
        viewName = "ViewAllContent";
        webUrl = new string[] ...{ "", "RunInfo/" };
        itemName = new string[] ...{ "新聞標題" ,"會議名稱"};

        GridView1.Attributes.Add("class", "table");
        dt = new DataTable();
        dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
        dt.Columns.Add("Title", System.Type.GetType("System.String"));
        dt.Columns.Add("URL", System.Type.GetType("System.String"));
        dt.Columns.Add("CreateTime", System.Type.GetType("System.DateTime"));
        if (!IsPostBack)
        ...{
            MakeTable();
            BindGridView();
        }
    }

    private void BindGridView()
    ...{
        GridView1.DataSource = dv;
        GridView1.DataBind();
    }

    private void MakeTable()
    ...{
        dt.Rows.Clear();
        try
        ...{

            for (int i = 0; i < listUrl.Length; i++)
            ...{
                site = new SPSite("http://" + Request.Url.Host + "/" + webUrl[i]);
                web = site.OpenWeb();
                list = web.GetList(webUrl[i] + listUrl[i]);
                SPView view = list.Views[viewName];
                SPQuery q = new SPQuery(view);
                SPListItemCollection items = list.GetItems(q);
                for (int j = 0; j < items.Count; j++)
                ...{
                    dt.Rows.Add(m,items[j][itemName[i]], "http://" + Request.Url.Host + "/" + webUrl[i] + listUrl[i] + "DispForm.aspx?ID=" + items[j].ID,items[j]["建立時間"]);
                    m++;
                }
            }

            dv = new DataView(dt);
            dv.Sort = " CreateTime ASC";
            dv.RowFilter = " ID <= 4";
        }
        catch (Exception ex)
        ...{
            Response.Write(ex.Message);
        }
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.