zoukankan      html  css  js  c++  java
  • SharePoint List 查看器

     

    using Microsoft.SharePoint;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SharePointListViewer
    {
        public partial class frmMain : Form
        {
            public class HourGlass : IDisposable
            {
                public HourGlass()
                {
                    Enabled = true;
                }
                public void Dispose()
                {
                    Enabled = false;
                }
                public static bool Enabled
                {
                    get { return Application.UseWaitCursor; }
                    set
                    {
                        if (value == Application.UseWaitCursor) return;
                        Application.UseWaitCursor = value;
                        Form f = Form.ActiveForm;
                        if (f != null && f.Handle != null)   // Send WM_SETCURSOR
                            SendMessage(f.Handle, 0x20, f.Handle, (IntPtr)1);
                    }
                }
                [System.Runtime.InteropServices.DllImport("user32.dll")]
                private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
            }
    
            public frmMain()
            {
                InitializeComponent();
            }
    
            private SPSite _Site = null;
            private SPWeb _Web = null;
            private SPList _List = null;
    
            public SPSite Site
            {
                get
                {
                    return _Site;
                }
                set
                {
                    if (_Site != null)
                    {
                        _Site.Dispose();
                        _Site = null;
                    }
                    _Site = value;
                    siteChanged();
                    Web = null;
                    List = null;
                }
            }
    
            public SPWeb Web
            {
                get
                {
                    return _Web;
                }
                set
                {
                    if (_Web != null)
                    {
                        _Web.Dispose();
                        _Web = null;
                    }
                    _Web = value;
                    webChanged();
                    List = null;
                }
            }
    
            public SPList List
            {
                get
                {
                    return _List;
                }
                set
                {
                    _List = value;
                    listChanged();
                }
            }
    
            private void siteChanged()
            {
                if (this.Site == null)
                    return;
                var webs = this.Site.AllWebs.Select(w => new KeyValuePair<string, SPWeb>(w.Name, w)).ToList();
                webs.Insert(0, new KeyValuePair<string, SPWeb>("-- Please Select --", null));
                cmbWebs.SelectedIndexChanged -= cmbWebs_SelectedIndexChanged;
                cmbWebs.DataSource = webs;
                cmbWebs.DisplayMember = "Key";
                cmbWebs.ValueMember = "Value";
                cmbWebs.SelectedIndexChanged += cmbWebs_SelectedIndexChanged;
            }
    
            private void webChanged()
            {
                if (this.Web == null)
                    return;
                var lists = this.Web.Lists.Cast<SPList>().Select(l => new KeyValuePair<string, SPList>(l.Title, l)).ToList();
                lists.Insert(0, new KeyValuePair<string, SPList>("-- Please Select --", null));
                cmbLists.SelectedIndexChanged -= cmbLists_SelectedIndexChanged;
                cmbLists.DataSource = lists;
                cmbLists.DisplayMember = "Key";
                cmbLists.ValueMember = "Value";
                cmbLists.SelectedIndexChanged += cmbLists_SelectedIndexChanged;
            }
    
            private void listChanged()
            {
                if (this.List == null)
                {
                    dgvListItems.DataSource = null;
                    return;
                }
    
                var result = getListDataSource(this.List);
                dgvListItems.DataSource = result;
                dgvListItems.AutoGenerateColumns = true;
            }
    
            private void btnOpenWebSite_Click(object sender, EventArgs e)
            {
                cmbLists.Items.Clear();
                cmbWebs.Items.Clear();
    
                using (new HourGlass())
                {
                    try
                    {
                        this.Enabled = false;
                        this.Site = new SPSite(txtSiteUrl.Text);
                        this.Enabled = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
    
                }
            }
    
            private void cmbWebs_SelectedIndexChanged(object sender, EventArgs e)
            {
                using (new HourGlass())
                {
                    if (cmbWebs.SelectedItem != null)
                    {
                        var item = (KeyValuePair<string, SPWeb>)cmbWebs.SelectedItem;
                        if (item.Value != null)
                            this.Web = item.Value;
                    }
                }
            }
    
            private void cmbLists_SelectedIndexChanged(object sender, EventArgs e)
            {
                using (new HourGlass())
                {
                    if (cmbLists.SelectedItem != null)
                    {
                        var item = (KeyValuePair<string, SPList>)cmbLists.SelectedItem;
                        if (item.Value != null)
                            this.List = item.Value;
                        txtSearchKey.Clear();
                    }
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                var dt = new DataTable();
                dt.Columns.Add("Key", typeof(string));
                dt.Columns.Add("Value", typeof(string));
                var items = new List<KeyValuePair<string, string>> 
                {
                    new KeyValuePair<string,string>("NET Winform gridview 实现多行表头NET Winform Datagridview 实现多行表头", ""),
                    new KeyValuePair<string,string>("审计署:6家国企违规发放1.7亿元职工福利", ""),
                    new KeyValuePair<string,string>("电商人为制造的购物伪高潮该停了", ""),
                    new KeyValuePair<string,string>("Get Started with Microsoft SharePoint Foundation!", "")
                };
                foreach (var item in items)
                {
                    var r = dt.NewRow();
                    r["Key"] = item.Key;
                    r["Value"] = item.Value;
                    dt.Rows.Add(r);
                }
                dgvListItems.DataSource = dt;
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.Site != null)
                {
                    this.Site.Close();
                    this.Site = null;
                }
            }
    
            private DataTable getListDataSource(SPList list)
            {
                var dt = new DataTable();
                foreach (SPField f in list.Fields)
                    dt.Columns.Add(f.StaticName, typeof(string));
    
                foreach (SPListItem listRow in list.Items)
                {
                    if (listRow.Versions.Cast<SPListItemVersion>().FirstOrDefault(v => v.Level == SPFileLevel.Published) != null)
                    {
                        var r = dt.NewRow();
                        foreach (DataColumn c in dt.Columns)
                        {
                            if (listRow[c.ColumnName] != null)
                                r[c.ColumnName] = listRow[c.ColumnName].ToString();
                        }
                        dt.Rows.Add(r);
                    }
                }
                return dt;
            }
    
            private void filterGridView(DataGridView dgv, string keyword)
            {
                if (dgv.DataSource == null)
                    return;
    
                dgv.CurrentCell = null;
    
                var dt = dgv.DataSource as DataTable;
                foreach (DataGridViewRow gRow in dgv.Rows)
                {
                    if (string.IsNullOrEmpty(keyword))
                    {
                        gRow.Visible = true;
                        continue;
                    }
    
                    if (gRow.DataBoundItem == null
                        || gRow.DataBoundItem as DataRowView == null)
                    {
                        gRow.Visible = true;
                        continue;
                    }
    
                    gRow.Visible = false;
    
                    var s = gRow.Cells[0].Value;
    
                    var row = (gRow.DataBoundItem as DataRowView).Row;
                    foreach (DataColumn column in dt.Columns)
                    {
                        if (row[column.ColumnName] != null
                            && row[column.ColumnName].ToString().ToUpper().Contains(keyword.ToUpper()))
                        {
                            gRow.Visible = true;
                            break;
                        }
                    }
                }
            }
    
            private void txtSearchKey_TextChanged(object sender, EventArgs e)
            {
                timer1.Stop();
                timer1.Start();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                timer1.Enabled = false;
                try
                {
                    Debug.Print("Filter start.");
                    filterGridView(dgvListItems, txtSearchKey.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
    
            }
    
            private void dgvListItems_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                var searchKey = txtSearchKey.Text;
    
                if (e.Value != null
                    && !string.IsNullOrEmpty(searchKey)
                    && e.Value.ToString().ToUpper().Contains(searchKey.ToUpper()))
                {
                    var cellText = e.Value.ToString();
                    var backGroundColor = Color.Yellow;
                    var foreColor = e.CellStyle.ForeColor;
                    var hilightKeyWordColor = Color.Red;
    
                    // 绘制背景 
                    e.PaintBackground(e.ClipBounds, false);
    
                    //绘制自定义背景
                    var bounds = e.CellBounds;
                    bounds.Inflate(new Size(-1, -1));
    
                    // 绘制背景(被选中时) 
                    if (e.State == (DataGridViewElementStates.Selected | DataGridViewElementStates.Displayed | DataGridViewElementStates.Visible))
                    {
                        e.PaintBackground(e.ClipBounds, true);
                        hilightKeyWordColor = Color.Yellow;
                        backGroundColor = e.CellStyle.SelectionBackColor;
                    }
                    else
                    {
                        e.Graphics.FillRectangle(new SolidBrush(backGroundColor), bounds);
                        hilightKeyWordColor = Color.Red;
                    }
    
                    using (Brush foreColorBrush = new SolidBrush(foreColor), keywordColorBrush = new SolidBrush(hilightKeyWordColor))
                    {
                        // 绘制原文本 
                        e.Graphics.DrawString(cellText, e.CellStyle.Font, foreColorBrush, e.CellBounds, StringFormat.GenericDefault);
    
                        //获取关键字之前的文字
                        var searchKeyIndex = cellText.ToUpper().IndexOf(searchKey.ToUpper());
                        var textBeforeSearchKey = cellText.Substring(0,searchKeyIndex);
                        var textsearchKey = cellText.Substring(searchKeyIndex, searchKey.Length);
                        
    
                        //绘制文字
                        e.Graphics.DrawString(textBeforeSearchKey + textsearchKey, new Font(e.CellStyle.Font, FontStyle.Underline), keywordColorBrush, e.CellBounds, StringFormat.GenericDefault);
                        e.Graphics.DrawString(textBeforeSearchKey, new Font(e.CellStyle.Font, FontStyle.Underline), new SolidBrush(backGroundColor), e.CellBounds, StringFormat.GenericDefault);
                        e.Graphics.DrawString(textBeforeSearchKey, new Font(e.CellStyle.Font, FontStyle.Regular), foreColorBrush, e.CellBounds, StringFormat.GenericDefault);
    
                        // 已完成事件处理,继续本身处理 
                        e.Handled = true;
                    }
    
                }
            }
        }
    }
    namespace SharePointListViewer
    {
        partial class frmMain
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.btnOpenWebSite = new System.Windows.Forms.Button();
                this.txtSiteUrl = new System.Windows.Forms.TextBox();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.cmbWebs = new System.Windows.Forms.ComboBox();
                this.label3 = new System.Windows.Forms.Label();
                this.cmbLists = new System.Windows.Forms.ComboBox();
                this.dgvListItems = new System.Windows.Forms.DataGridView();
                this.splitContainer1 = new System.Windows.Forms.SplitContainer();
                this.label4 = new System.Windows.Forms.Label();
                this.txtSearchKey = new System.Windows.Forms.TextBox();
                this.timer1 = new System.Windows.Forms.Timer(this.components);
                ((System.ComponentModel.ISupportInitialize)(this.dgvListItems)).BeginInit();
                this.splitContainer1.Panel1.SuspendLayout();
                this.splitContainer1.Panel2.SuspendLayout();
                this.splitContainer1.SuspendLayout();
                this.SuspendLayout();
                // 
                // btnOpenWebSite
                // 
                this.btnOpenWebSite.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
                this.btnOpenWebSite.Location = new System.Drawing.Point(732, 12);
                this.btnOpenWebSite.Name = "btnOpenWebSite";
                this.btnOpenWebSite.Size = new System.Drawing.Size(75, 22);
                this.btnOpenWebSite.TabIndex = 0;
                this.btnOpenWebSite.Text = "Open Site";
                this.btnOpenWebSite.UseVisualStyleBackColor = true;
                this.btnOpenWebSite.Click += new System.EventHandler(this.btnOpenWebSite_Click);
                // 
                // txtSiteUrl
                // 
                this.txtSiteUrl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right)));
                this.txtSiteUrl.Location = new System.Drawing.Point(75, 14);
                this.txtSiteUrl.Name = "txtSiteUrl";
                this.txtSiteUrl.Size = new System.Drawing.Size(651, 20);
                this.txtSiteUrl.TabIndex = 1;
                this.txtSiteUrl.Text = "http://bmsserver:9000/sites/BPM/";
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(12, 16);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(44, 13);
                this.label1.TabIndex = 2;
                this.label1.Text = "Site Url:";
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(15, 50);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(38, 13);
                this.label2.TabIndex = 3;
                this.label2.Text = "Webs:";
                // 
                // cmbWebs
                // 
                this.cmbWebs.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right)));
                this.cmbWebs.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.cmbWebs.FormattingEnabled = true;
                this.cmbWebs.Location = new System.Drawing.Point(16, 3);
                this.cmbWebs.Name = "cmbWebs";
                this.cmbWebs.Size = new System.Drawing.Size(295, 21);
                this.cmbWebs.TabIndex = 4;
                this.cmbWebs.SelectedIndexChanged += new System.EventHandler(this.cmbWebs_SelectedIndexChanged);
                // 
                // label3
                // 
                this.label3.AutoSize = true;
                this.label3.Location = new System.Drawing.Point(7, 7);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(31, 13);
                this.label3.TabIndex = 3;
                this.label3.Text = "Lists:";
                // 
                // cmbLists
                // 
                this.cmbLists.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right)));
                this.cmbLists.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.cmbLists.FormattingEnabled = true;
                this.cmbLists.Location = new System.Drawing.Point(44, 3);
                this.cmbLists.Name = "cmbLists";
                this.cmbLists.Size = new System.Drawing.Size(281, 21);
                this.cmbLists.TabIndex = 4;
                this.cmbLists.SelectedIndexChanged += new System.EventHandler(this.cmbLists_SelectedIndexChanged);
                // 
                // dgvListItems
                // 
                this.dgvListItems.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right)));
                this.dgvListItems.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dgvListItems.Location = new System.Drawing.Point(18, 83);
                this.dgvListItems.Name = "dgvListItems";
                this.dgvListItems.Size = new System.Drawing.Size(789, 378);
                this.dgvListItems.TabIndex = 5;
                this.dgvListItems.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dgvListItems_CellPainting);
                // 
                // splitContainer1
                // 
                this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right)));
                this.splitContainer1.Location = new System.Drawing.Point(59, 39);
                this.splitContainer1.Name = "splitContainer1";
                // 
                // splitContainer1.Panel1
                // 
                this.splitContainer1.Panel1.Controls.Add(this.cmbWebs);
                // 
                // splitContainer1.Panel2
                // 
                this.splitContainer1.Panel2.Controls.Add(this.cmbLists);
                this.splitContainer1.Panel2.Controls.Add(this.label3);
                this.splitContainer1.Size = new System.Drawing.Size(680, 38);
                this.splitContainer1.SplitterDistance = 338;
                this.splitContainer1.TabIndex = 6;
                // 
                // label4
                // 
                this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
                this.label4.AutoSize = true;
                this.label4.Location = new System.Drawing.Point(18, 476);
                this.label4.Name = "label4";
                this.label4.Size = new System.Drawing.Size(73, 13);
                this.label4.TabIndex = 7;
                this.label4.Text = "搜索关键字:";
                // 
                // txtSearchKey
                // 
                this.txtSearchKey.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right)));
                this.txtSearchKey.Location = new System.Drawing.Point(97, 473);
                this.txtSearchKey.Name = "txtSearchKey";
                this.txtSearchKey.Size = new System.Drawing.Size(710, 20);
                this.txtSearchKey.TabIndex = 8;
                this.txtSearchKey.TextChanged += new System.EventHandler(this.txtSearchKey_TextChanged);
                // 
                // timer1
                // 
                this.timer1.Interval = 1000;
                this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                // 
                // frmMain
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(824, 503);
                this.Controls.Add(this.txtSearchKey);
                this.Controls.Add(this.label4);
                this.Controls.Add(this.splitContainer1);
                this.Controls.Add(this.dgvListItems);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.txtSiteUrl);
                this.Controls.Add(this.btnOpenWebSite);
                this.Name = "frmMain";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "SharePoint List Viewer";
                this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dgvListItems)).EndInit();
                this.splitContainer1.Panel1.ResumeLayout(false);
                this.splitContainer1.Panel2.ResumeLayout(false);
                this.splitContainer1.Panel2.PerformLayout();
                this.splitContainer1.ResumeLayout(false);
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button btnOpenWebSite;
            private System.Windows.Forms.TextBox txtSiteUrl;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.ComboBox cmbWebs;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.ComboBox cmbLists;
            private System.Windows.Forms.DataGridView dgvListItems;
            private System.Windows.Forms.SplitContainer splitContainer1;
            private System.Windows.Forms.Label label4;
            private System.Windows.Forms.TextBox txtSearchKey;
            private System.Windows.Forms.Timer timer1;
        }
    }
  • 相关阅读:
    介绍 Spring 3.1 M1 中的缓存功能
    第四章:模板系统篇
    第一章 Django介紹篇
    maven 打包jar
    python+PyQT+Eric安装配置
    413 Request Entity Too Large
    Django web : CSRF verification failed. Request aborted.
    CentOS 查看IP,端口 修改IP,打开端口生效
    django web Cookie 和 Sessions 应用
    Python:通过执行100万次打印来比较C和python的性能,以及用C和python结合来解决性能问题的方法 .
  • 原文地址:https://www.cnblogs.com/nanfei/p/3799501.html
Copyright © 2011-2022 走看看