SharePoint Pals
 | Sign In
Displaying SharePoint list items in MVC web grid of a Provider Hosted App
by Krishna KV 26 Oct 2016
Team Leader, Aspire Systems
Today  :   10     Total  :    1311

In this blog post lets see how to bind SharePoint List items to a MVC web grid  in a Provider Hosted App.To create a provided hosted app, we need a client id and secret. Lets start with creating the client id and key.

Open the site



Stored the ID and key to add the information in the visual studio project.



Open the appmanifest.xml in any text editor and update the Id.

     <RemoteWebApplication ClientId="f1593283-77ac-4f62-bef4-0bd793f7d612" />

Update the ID and key in the web.config (web project).

 <add key="ClientId" value="f1593283-77ac-4f62-bef4-xxxxxxx" />
 <add key="ClientSecret" value="slnWBFfe4jUBxl+rkyppCmxxxxxxx=" />

Provide the permission for the app.



 public class Employee
         public string Title { get; set; }
         public string FirstName { get; set; }
         public int Id { get; set; }
         public string LastName { get; set; }
         public string Email { get; set; }


 public ActionResult Index()
             gen.List<Employee> lstEmployees = null;
             var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
             using (var clientContext = spContext.CreateUserClientContextForSPHost())
                 if (clientContext != null)
                     List listEmployee = clientContext.Web.Lists.GetByTitle("Employees");
                     var items = listEmployee.GetItems(new CamlQuery());
                     clientContext.Load(items, item => item.Include(itm => itm.Id, itm => itm["Title"], itm => itm["FirstName"], itm => itm["LastName"], itm => itm["Email"]));
                     lstEmployees = items.Select(item => new Employee() {Id=item.Id, Title = item["Title"].ToString(),
                     FirstName=item["FirstName"].ToString(), LastName=item["LastName"].ToString(),Email=item["Email"].ToString()}).ToList();
             return View(lstEmployees);


 @model IEnumerable<MVCAPP.WebGridViewWeb.Controllers.Employee>
     ViewBag.Title = "grid";
     WebGrid grid = new WebGrid(Model, rowsPerPage: 5,ajaxUpdateContainerId: "gridContent");
 <div id="gridContent" style="font-family: Arial; padding: 20px;">
     @grid.GetHtml(tableStyle: "webgrid-table",
     headerStyle: "webgrid-header",
     footerStyle: "webgrid-footer",
     alternatingRowStyle: "webgrid-alternating-row",
     selectedRowStyle: "webgrid-selected-row",
     rowStyle: "webgrid-row-style",
     columns: grid.Columns(
          grid.Column(columnName: "Id", header: "View", format:@<text>@item.GetSelectLink("View")</text>),
                 grid.Column(columnName: "Title", header: "Title"),
                  grid.Column(columnName: "FirstName", header: "FirstName"),
                 grid.Column(columnName: "LastName", header: "LastName"),
                 grid.Column(columnName: "Email", header: "Email")
     <h2>Selected Item</h2>
     @if (grid.HasSelection)
         var emp = (MVCAPP.WebGridViewWeb.Controllers.Employee)grid.Rows[grid.SelectedIndex].Value;
         <p><b>First Name:</b> @emp.FirstName</p>
         <p><b>Last Name:</b> @emp.LastName</p>
         <p><b>Email:</b> @emp.Email</p>
 <style type="text/css">
     .webgrid-table {
         font-family: Arial,Helvetica,sans-serif;font-size: 14px;font-weight: normal; width: 650px;display: table;
         border-collapse: collapse; border: solid 1px #C5C5C5; background-color: white;
     .webgrid-table td, th { border: 1px solid #C5C5C5;padding: 3px 7px 2px; }
     .webgrid-header, .webgrid-header a { background-color: #0094ff; color: #ffffff; text-align: left; text-decoration: none; }
        .webgrid-row-style {  padding: 3px 7px 2px;    }
     .webgrid-alternating-row {background-color: azure;padding: 3px 7px 2px; }   
blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter November 21, 21:08
Implementing Multi-Threading with MaxDegreeOfParallelism in SharePoint using CSOM – An Insight -

Twitter November 21, 12:48
Configure Windows Firewall for SQL Server Analysis Service (SSAS) -

Twitter November 21, 12:46
Implementing Multi-Threading in SharePoint To Upload Files using CSOM C# – An Insight -

Twitter November 20, 21:48
How to Get the Client Context Using App Access token, by passing Client ID using CSOM in SharePoint Office 365 -

Twitter November 18, 21:30
How to Enable Auto Approval For Permission Requests in Community Site Template – Office 2013 using Client Side Object Model (CSOM) C#

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation