Bind list items or document library data to gridview using SharePoint server object model

This SharePoint tutorial, we will discuss how to bind the SharePoint list or library data to gridview using SharePoint server object model code. The same way we can bind SharePoint list items using the server object model code in SharePoint 2010/2013/2016/2019.

Here I have used a Gridview control and we will use SharePoint server object model code.

If you are new to the SharePoint server object model, you can check 25+ SharePoint server object model examples.

Bind sharepoint list item to gridview or Bind document library data in gridview

Below is the GridView code.

<asp:GridView ID=”grdDocs” runat=”server” AutoGenerateColumns=”true”
ForeColor=”Black” GridLines=”None” AllowPaging=”True” PageSize=”15″>
<AlternatingRowStyle BackColor=”#EFEFFE” />
<FooterStyle BackColor=”#000″ />
<HeaderStyle BackColor=”#efefef” Font-Bold=”True” ForeColor=”DarkSlateBlue” />
<PagerStyle BackColor=”PaleGoldenrod” ForeColor=”DarkSlateBlue”
HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#EFEFFE” ForeColor=”GhostWhite” />
</asp:GridView>

Below is the SharePoint server object model code. We can write the code on a button click event inside a visual web part in SharePoint 2010/2013/2016/2019.

public void BindDocuments()
{
try
{
SPSite mysite = SPContext.Current.Site;
SPWeb web = mysite.RootWeb;
SPList oList = web.Lists["Doc Lib Name"];
SPView oView = oList.Views["View Name"];
SPQuery oQuery = new SPQuery(oView);
oQuery.ViewAttributes = "Scope=\"RecursiveAll\"";
SPListItemCollection collListItemsAvailable = oList.GetItems(oQuery);
grdDocs.DataSource = collListItemsAvailable.GetDataTable();

grdDocs.DataBind();
}
catch(Exception ex)
{

}
}

You may like following SharePoint server object model tutorials:

Hope this SharePoint tutorial explain, how to Bind SharePoint list item to gridview or Bind document library data in gridview using SharePoint server object model code.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>