zoukankan      html  css  js  c++  java
  • sharepoint gridview webpart

    http://blog.csdn.net/dxfswpi/archive/2009/04/03/4046871.aspx

    using System;
    using System.Runtime.InteropServices;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Serialization;
    using System.Xml;
    using System.Data;
    using System.Collections.Generic;
    using System.Drawing;

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

    namespace GetUsersDataList
    {
    [Guid(
    "d6e547b8-fdbd-4166-9b78-f73ff03c260a")]
    public class GetUsersListData : System.Web.UI.WebControls.WebParts.WebPart
    {
    //private GridView _gridview;
    //private SqlDataSource _sqldatasource;

    /// <summary>
    /// 显示当前所在的SharePoint用户组
    /// </summary>
    ///
    private List<string> currentUserGroup = null;

    private SPSite site = null;
    private SPWeb web = null;
    private SPList list = null;

    ///<summary>
    ///保存GridView 数据源
    /// </summary>
    ///
    private DataTable _gvtable;
    public DataTable GVTable
    {
    get
    {
    return _gvtable;
    }
    set
    {
    _gvtable
    = value;
    }
    }

    /// <summary>
    /// 显示条目的GridView控件
    /// </summary>
    ///
    private GridView gridView =null;

    public SPSite SP_Site
    {
    get
    {
    return SPContext.Current.Site;
    }
    }

    public SPWeb SP_Web
    {
    get
    {
    return SPContext.Current.Web;
    }
    }

    /// <summary>
    /// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
    /// </summary>
    ///
    private string _isAccessed = "2";

    [Personalizable(PersonalizationScope.Shared), WebBrowsable(
    true), WebDisplayName("是否对显示条目启用目标访问群体控制"), WebDescription("请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1")]
    public string IsAccessed
    {
    get
    {
    return _isAccessed;
    }
    set
    {
    if (value.Equals("1") || value.Equals("2"))
    {
    this._isAccessed = value;
    }
    else
    {
    throw new Exception("请注意[是否对显示条目启用目标访问群体控制]的输入格式:\n输入1为启用,输入2为禁用");
    }
    }
    }


    private string _caml;
    /// <summary>
    /// 请输入Caml
    /// </summary>
    ///
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Caml"), WebDescription("请输入Caml")]
    public string Caml
    {
    get
    {
    return _caml;
    }
    set
    {
    _caml
    = value;
    }
    }

    private string _siteName;
    /// <summary>
    /// 请输入网站集URL,格式形如:http://phosphor:8080/sites
    /// </summary>
    ///
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("网站集URL"), WebDescription("请输入网站集URL,形式格式如:http://phosphor:8080/sites")]
    public string SiteName
    {
    get
    {
    return _siteName;
    }
    set
    {
    _siteName
    = value;
    }
    }

    private string _webName;
    /// <summary>
    /// 请输入网站URL,格式形式如:umbrella
    /// </summary>
    ///
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("网站URL"), WebDescription("请输入网站URL,格式形式如:umbrella")]
    public string WebName
    {
    get
    {
    return _webName;
    }
    set
    {
    _webName
    = value;
    }
    }

    private string _listName;
    /// <summary>
    /// 请输入列表名称,格式形如:已发布动态
    /// </summary>
    ///
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("列表名称"), WebDescription("请输入列表名称,格式形式如:已发布动态")]
    public string ListName
    {
    get
    {
    return _listName;
    }
    set
    {
    _listName
    = value;
    }
    }

    public string _itemCount;
    /// <summary>
    /// 请输入要显示的条目数量,格式形如:10
    /// </summary>
    ///
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("条目数量"), WebDescription("请输入要显示的条目数量,格式形式如:10")]
    public string ItemCount
    {
    get
    {
    return _itemCount;
    }
    set
    {
    int count = 0;
    if (int.TryParse(value, out count) && count < 100)
    _itemCount
    = value;
    else
    throw new Exception("请输入小于100的整数");
    }
    }

    public GetUsersListData()
    {
    currentUserGroup
    = new List<string>();
    this.ExportMode = WebPartExportMode.All;
    }

    /// <summary>
    /// 获取当前用户所在的SharePoint组
    /// </summary>
    ///
    private void GetUserSPGroup()
    {
    SPGroupCollection groups
    = SPContext.Current.Web.CurrentUser.Groups;
    foreach (SPGroup group in groups)
    {
    currentUserGroup.Add(group.Name);
    }
    }

    protected override void CreateChildControls()
    {
    //_gridview = new GridView();
    //_gridview.ID = "gdv_User";
    //_gridview.AutoGenerateColumns = true;
    //_gridview.DataKeyNames = new string[] {"UserID"};
    //CommandField field = new CommandField();
    //_gridview.SelectedRowStyle.BackColor = Color.CadetBlue;
    //field.ShowCancelButton = true;
    //field.ShowDeleteButton = true;
    //field.ShowEditButton = true;
    //field.ShowHeader = true;
    //field.ShowInsertButton = true;
    //field.ShowSelectButton = true;
    //_gridview.Columns.Add(field);

    //_sqldatasource = new SqlDataSource();
    //_sqldatasource.ID = "sqlDS_Users";
    //_sqldatasource.ConnectionString = "server=PHOSPHOR;database=SharePoint_Config;uid=sa;pwd=Sa123456";

    //_gridview.DataSource = _sqldatasource.ID;

    //this.Controls.Add(_sqldatasource);
    //this.Controls.Add(_gridview);

    _siteName
    = "http://phosphor:8080";
    _webName
    = "umbrella";
    _itemCount
    = "10";
    _listName
    = "联系人";

    GetUserSPGroup();

    gridView
    = new GridView();
    if (!string.IsNullOrEmpty(SiteName) && !string.IsNullOrEmpty(WebName) && !string.IsNullOrEmpty(ListName) && !string.IsNullOrEmpty(ItemCount))
    using (site = new SPSite(SPContext.Current.Site.ID))
    {
    using (web = site.OpenWeb(SPContext.Current.Web.ID))
    {
    list
    = web.Lists[ListName];
    SPQuery query
    = new SPQuery();
    //query.ViewFields="<FieldRef Name='Title' />";

    int count = 0;
    int.TryParse(ItemCount, out count);
    query.RowLimit
    = (uint)count;
    if (IsAccessed.Equals("1"))
    {
    if (currentUserGroup.Count > 0)
    {
    query.Query
    = @"<OrderBy>
    <FieldRef Name='ID' Ascending='False' />
    </OrderBy>
    ";
    }
    else
    return;
    }
    else
    {
    query.Query
    = @"<OrderBy>
    <FieldRef Name='ID' Ascending='true' />
    </OrderBy>
    ";
    }

    SPListItemCollection items
    = web.Lists[ListName].Items;

    string tempStr = "";


    DataTable dt
    = new DataTable();
    dt.Columns.Add(
    new DataColumn("姓氏", typeof(string)));
    dt.Columns.Add(
    new DataColumn("名字", typeof(string)));
    dt.Columns.Add(
    new DataColumn("公司", typeof(string)));
    dt.Columns.Add(
    new DataColumn("商务电话", typeof(string)));
    dt.Columns.Add(
    new DataColumn("住宅电话", typeof(string)));
    dt.Columns.Add(
    new DataColumn("邮件地址", typeof(string)));
    dt.Columns.Add(
    new DataColumn("联系人代号",typeof(int)));


    //region 查询出类表中字段的内部名称(InternalName)
    foreach (SPField field in list.Fields)
    {
    tempStr
    = tempStr + "/" +field.ToString()+":"+ field.InternalName.ToString();
    }
    //endregion

    foreach (SPListItem item in items)
    {
    DataRow dr
    = dt.NewRow();
    dr[
    "姓氏"] = item.GetFormattedValue("Title");
    dr[
    "名字"] = item.GetFormattedValue("FirstName");
    dr[
    "公司"] = item.GetFormattedValue("Company");
    dr[
    "商务电话"] = item.GetFormattedValue("WorkPhone");
    dr[
    "住宅电话"] = item.GetFormattedValue("HomePhone");
    dr[
    "邮件地址"] = item.GetFormattedValue("Email");
    dr[
    "联系人代号"] = item.GetFormattedValue("_x8054__x7cfb__x4eba__x4ee3__x53");
    dt.Rows.Add(dr);
    }
    //设置ID
    gridView.ID = "gridView";

    gridView.Columns.Clear();
    //禁止自动生成列
    gridView.AutoGenerateColumns = false;

    //允许排序、分页
    gridView.AllowSorting = true;
    gridView.AllowPaging
    = true;

    //设置一些样式
    gridView.BackColor=Color.FromArgb(0xDE,0xBA,0x84);
    gridView.BorderColor
    = Color.FromArgb(0xDE,0xBA,0x84);
    gridView.BorderStyle
    = BorderStyle.None;
    gridView.BorderWidth
    = new Unit("1px");
    gridView.CellPadding
    = 3;
    gridView.CellSpacing
    = 3;
    gridView.DataKeyNames
    = new string[] { "联系人代号" };

    //设置一些样式
    gridView.RowStyle.BackColor = Color.FromArgb(0xFF,0xF7,0xE7);
    gridView.RowStyle.ForeColor
    = Color.FromArgb(0x8C,0x45,0x10);
    gridView.SelectedRowStyle.BackColor
    = Color.FromArgb(0x73,0x8A,0x9C);
    gridView.SelectedRowStyle.Font.Bold
    = true;
    gridView.SelectedRowStyle.ForeColor
    = Color.White;
    gridView.PagerStyle.ForeColor
    = Color.FromArgb(0x8C,0x45,0x10) ;
    gridView.PagerStyle.HorizontalAlign
    = HorizontalAlign.Center;
    gridView.HeaderStyle.BackColor
    = Color.FromArgb(0xA5,0x51,0x29);
    gridView.HeaderStyle.Font.Bold
    = true;
    gridView.HeaderStyle.ForeColor
    = Color.White;

    //添加一个编辑事件
    gridView.RowEditing += new GridViewEditEventHandler(gridView_RowEditing);

    //添加一个更新事件
    gridView.RowUpdating += new GridViewUpdateEventHandler(gridView_RowUpdating);

    //添加一个取消事件
    gridView.RowCancelingEdit += new GridViewCancelEditEventHandler(gridView_RowCancelingEdit);

    //添加一个命令列
    CommandField cmdColumn = new CommandField();
    cmdColumn.ShowDeleteButton
    = true;
    cmdColumn.ShowEditButton
    = true;
    cmdColumn.ShowSelectButton
    = true;
    gridView.Columns.Add(cmdColumn);

    //添加一个绑定列
    BoundField bndColumn = new BoundField();
    bndColumn.DataField
    = "姓氏";
    bndColumn.HeaderText
    = "姓氏";
    bndColumn.InsertVisible
    = false;
    bndColumn.ReadOnly
    = true;
    bndColumn.SortExpression
    = "姓氏";

    gridView.Columns.Add(bndColumn);

    //重新定义
    bndColumn = new BoundField();
    bndColumn.DataField
    = "名字";
    bndColumn.HeaderText
    = "名字";
    bndColumn.SortExpression
    = "名字";

    gridView.Columns.Add(bndColumn);


    //重新定义
    bndColumn = new BoundField();
    bndColumn.DataField
    = "公司";
    bndColumn.HeaderText
    = "公司";
    bndColumn.SortExpression
    = "公司";

    gridView.Columns.Add(bndColumn);

    //重新定义
    bndColumn = new BoundField();
    bndColumn.DataField
    = "商务电话";
    bndColumn.HeaderText
    = "商务电话";
    bndColumn.SortExpression
    = "商务电话";

    gridView.Columns.Add(bndColumn);

    //重新定义
    bndColumn = new BoundField();
    bndColumn.DataField
    = "住宅电话";
    bndColumn.HeaderText
    = "住宅电话";
    bndColumn.SortExpression
    = "住宅电话";

    gridView.Columns.Add(bndColumn);

    //重新定义
    bndColumn = new BoundField();
    bndColumn.DataField
    = "邮件地址";
    bndColumn.HeaderText
    = "电子邮件地址";
    bndColumn.SortExpression
    = "邮件地址";

    gridView.Columns.Add(bndColumn);

    //重新定义
    bndColumn = new BoundField();
    //TextField txtColumn = new TextField();
    //txtColumn.ID = "txt_Code";
    //txtColumn.ListId = list.ID;
    //txtColumn.FieldName = "联系人代号";
    //gridView.Columns.Add(txtColumn);
    bndColumn.DataField = "联系人代号";
    bndColumn.HeaderText
    = "联系人代号";
    bndColumn.Visible
    = false;

    gridView.Columns.Add(bndColumn);

    //gridView.ShowFooter = true;
    //gridView.ShowHeader = true;
    //gridView.AutoGenerateDeleteButton = true;
    //gridView.AutoGenerateEditButton = true;
    //gridView.AutoGenerateSelectButton = true;
    gridView.PageSize = 3;
    gridView.PagerSettings.Mode
    = PagerButtons.NumericFirstLast;
    gridView.PagerSettings.PageButtonCount
    = 5;
    gridView.PagerSettings.Position
    = PagerPosition.Bottom;

    gridView.PageIndexChanging
    += new GridViewPageEventHandler(gridView_PageIndexChanging);
    //gridView.RowDeleting += new GridViewDeleteEventHandler(gridView_RowDeleting);
    //gridView.RowCreated += new GridViewRowEventHandler(gridView_RowCreated);

    _gvtable
    = dt;
    //gridView.DataSource = dt;
    //gridView.DataBind();
    //DataBind(GVTable);
    }
    }
    Controls.Add(gridView);
    DataBind(GVTable);

    base.CreateChildControls();
    }

    void gridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    gridView.EditIndex
    = -1;
    DataBind(GVTable);
    }

    void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    int index = e.RowIndex;
    //string code=gridView.Rows[index]
    SPQuery query = new SPQuery();
    query.Query
    = @"<Where></Where>";

    }

    //void gridView_RowCreated(object sender, GridViewRowEventArgs e)
    //{
    // throw new Exception("The method or operation is not implemented.");
    //}

    void gridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
    gridView.EditIndex
    = e.NewEditIndex;
    DataBind(GVTable);
    }

    //void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    // throw new Exception("The method or operation is not implemented.");
    //}

    private void DataBind(DataTable dt )
    {
    try
    {
    this.gridView.DataSource = dt;
    this.gridView.DataBind();
    }
    catch (Exception ex)
    {
    throw new Exception(ex.ToString());
    }
    }

    void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    gridView.PageIndex
    = e.NewPageIndex;
    //gridView.DataSource = GVTable;
    //gridView.DataBind();
    this.DataBind(GVTable);
    }

    protected override void Render(HtmlTextWriter writer)
    {
    // TODO: add custom rendering code here.
    // writer.Write("Output HTML");
    EnsureChildControls();
    base.Render(writer);
    }
    }
    }

  • 相关阅读:
    Flask 框架下 Jinja2 模板引擎高层 API 类——Environment
    java中的URLEncoder.encode对应JS中用decodeURIComponent,js和java编码,解码
    SQL触发器(AFTER和INSTEAD OF)
    jquery对中文进行base64加密,后台用java进行base64解密
    org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [WebApp] in context with path关于数据库库的问题
    Mybatis-Plus代码生成器
    Swagger2-注解说明
    Maven详细配置
    JDK环境变量配置
    内存泄漏测试
  • 原文地址:https://www.cnblogs.com/icedog/p/1822890.html
Copyright © 2011-2022 走看看