none
how to display a sharepoint list in a custom webpart? RRS feed

  • Question

  • I want to create a webpart in visual studio that will connct to a list that I create in the site and display the list (in html table).

    how can I do that?

    Monday, November 8, 2010 1:47 PM

Answers

  • Just go into your sharepoint designer ,select an area where you can insert a webpart then from the 'Insert' click on the 'Data View' icon and select your list you want to display.. this will display it and you can customize its xslt etc
    we
    Tuesday, November 9, 2010 1:37 AM
  • Here's the basics for reading from a sharepoint list:

     

    http://msdn.microsoft.com/en-us/library/ms456030.aspx

     

     


    mike gauntlett
    Monday, November 8, 2010 3:06 PM
  • If your goal is simply to display the list in an html table, then why create a custom webpart?

    Simply use an XSLTListView webpart, and customize the XSL to render the HTML any way you like.

    • Proposed as answer by samdv1982 Tuesday, November 9, 2010 5:28 AM
    • Marked as answer by Aaron Han - MSFT Thursday, November 18, 2010 3:30 AM
    Monday, November 8, 2010 8:47 PM
  • Or, if you need another option that gives you a bit more flexibility still, an SPQuery that returns a DataTable that you bind to an SPGridView might be what you need:

    private ObjectDataSource gridDataSource;
    private SPGridView grid;
    
    protected override void CreateChildControls()
    {
      gridDataSource = new ObjectDataSource();
      gridDataSource.ID = DATASOURCEID;
      gridDataSource.SelectMethod = "SelectData";
      gridDataSource.TypeName = this.GetType().AssemblyQualifiedName;
      this.Controls.Add(gridDataSource);
    
      grid = new SPGridView();
      grid.ID = GRIDID;
      grid.DataSourceID = gridDataSource.ID;
      grid.AutoGenerateColumns = false;
      grid.FilteredDataSourcePropertyName = "FilterExpression";
      grid.FilteredDataSourcePropertyFormat = "{1}='{0}'";
      grid.DataKeyNames = new string[] { "WebId", "ListId", "ID" };
      grid.AllowPaging = true;
      grid.PageSize = 5;
      grid.AllowSorting = true;
      grid.AllowFiltering = true;
    
      this.Controls.Add(grid);
    
      SPGridViewPager pager = new SPGridViewPager();
      pager.GridViewId = grid.ID;
    
      this.Controls.Add(pager);
    }
    
    public DataTable SelectData()
    {
      SPQuery query = CreateWhateverQueryIsRelevantToYou();
      SPList list = GetTheListThatsRelevantToYou();
      return list.GetDataTable(query);
    }
    
    protected sealed override void Render(HtmlTextWriter writer)
    {  
      SPBoundField someField = new SPBoundField()
      {
        HeaderText = "Field Display Name",
        DataField = "FieldInternalName",
        SortExpression = "FieldInternalName",
      };
      
      // add other bound fields that come from the query
      //........................................
      //........................................
    
      grid.Columns.Add(someField);
      
      grid.FilterDataFields = "comma,separated,list,of,fields";
      grid.DataBind();
      
      base.Render(writer);
    }
    
    • Proposed as answer by samdv1982 Tuesday, November 9, 2010 5:28 AM
    • Marked as answer by Aaron Han - MSFT Thursday, November 18, 2010 3:30 AM
    Tuesday, November 9, 2010 5:26 AM

All replies

  • Here's the basics for reading from a sharepoint list:

     

    http://msdn.microsoft.com/en-us/library/ms456030.aspx

     

     


    mike gauntlett
    Monday, November 8, 2010 3:06 PM
  • If your goal is simply to display the list in an html table, then why create a custom webpart?

    Simply use an XSLTListView webpart, and customize the XSL to render the HTML any way you like.

    • Proposed as answer by samdv1982 Tuesday, November 9, 2010 5:28 AM
    • Marked as answer by Aaron Han - MSFT Thursday, November 18, 2010 3:30 AM
    Monday, November 8, 2010 8:47 PM
  • Just go into your sharepoint designer ,select an area where you can insert a webpart then from the 'Insert' click on the 'Data View' icon and select your list you want to display.. this will display it and you can customize its xslt etc
    we
    Tuesday, November 9, 2010 1:37 AM
  • Or, if you need another option that gives you a bit more flexibility still, an SPQuery that returns a DataTable that you bind to an SPGridView might be what you need:

    private ObjectDataSource gridDataSource;
    private SPGridView grid;
    
    protected override void CreateChildControls()
    {
      gridDataSource = new ObjectDataSource();
      gridDataSource.ID = DATASOURCEID;
      gridDataSource.SelectMethod = "SelectData";
      gridDataSource.TypeName = this.GetType().AssemblyQualifiedName;
      this.Controls.Add(gridDataSource);
    
      grid = new SPGridView();
      grid.ID = GRIDID;
      grid.DataSourceID = gridDataSource.ID;
      grid.AutoGenerateColumns = false;
      grid.FilteredDataSourcePropertyName = "FilterExpression";
      grid.FilteredDataSourcePropertyFormat = "{1}='{0}'";
      grid.DataKeyNames = new string[] { "WebId", "ListId", "ID" };
      grid.AllowPaging = true;
      grid.PageSize = 5;
      grid.AllowSorting = true;
      grid.AllowFiltering = true;
    
      this.Controls.Add(grid);
    
      SPGridViewPager pager = new SPGridViewPager();
      pager.GridViewId = grid.ID;
    
      this.Controls.Add(pager);
    }
    
    public DataTable SelectData()
    {
      SPQuery query = CreateWhateverQueryIsRelevantToYou();
      SPList list = GetTheListThatsRelevantToYou();
      return list.GetDataTable(query);
    }
    
    protected sealed override void Render(HtmlTextWriter writer)
    {  
      SPBoundField someField = new SPBoundField()
      {
        HeaderText = "Field Display Name",
        DataField = "FieldInternalName",
        SortExpression = "FieldInternalName",
      };
      
      // add other bound fields that come from the query
      //........................................
      //........................................
    
      grid.Columns.Add(someField);
      
      grid.FilterDataFields = "comma,separated,list,of,fields";
      grid.DataBind();
      
      base.Render(writer);
    }
    
    • Proposed as answer by samdv1982 Tuesday, November 9, 2010 5:28 AM
    • Marked as answer by Aaron Han - MSFT Thursday, November 18, 2010 3:30 AM
    Tuesday, November 9, 2010 5:26 AM