Provider Hosted App For SharePoint Online

Create a new project in visual studio using “App for SharePoint” template, here I’m using visual studio 2015 version. After selected project can see a wizard

2016-01-24_10-20-52

Enter SharePoint online URL and select “Provider-Hosted” option next it will ask for user name and password, then wizard will automatically select “SharePoint Online” Option

2016-01-24_10-26-11

Next select ASP.Net MVC Web Application in the wizard

2016-01-24_10-29-44

Next leave by default selection of “Use Windows Azure Access Control Service (for SharePoint Cloud Apps)” then finish the wizard.

Create new razor view under home view folder

2016-01-24_12-02-36

Add new ActionResult in HomeController.cs, below code will retrieve list items from the “Shared Documents” and generates a DataTable. pass the DataTable to View.

 public ActionResult DocumentList()
{
DataTable dt = new DataTable();
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientcontext = spContext.CreateUserClientContextForSPHost())
{
List list = clientcontext.Web.GetList(spContext.SPHostUrl.ToString() + "Shared Documents");
clientcontext.Load(list);
CamlQuery cq = new CamlQuery();
ListItemCollection lic = list.GetItems(cq);
clientcontext.Load(lic, items => items.Take(15).Include(item => item["FileLeafRef"], item => item["Modified"],
item => item["Author"], item => item["FileRef"], item => item["File_x0020_Size"]));
clientcontext.ExecuteQuery();
dt.Columns.Add("File Name");
dt.Columns.Add("Modified", typeof(DateTime));
dt.Columns.Add("Author");
dt.Columns.Add("Size");

foreach (ListItem item in lic)
{
DataRow dr = dt.NewRow();
dr["File Name"] = "<a href=\"" + spContext.SPHostUrl.ToString().TrimEnd('/') + item["FileRef"]
+ "\">" + item["FileLeafRef"] + "</a>";
dr["Modified"] = item["Modified"];
dr["Author"] = ((FieldUserValue)item["Author"]).LookupValue;
dr["Size"] = item["File_x0020_Size"];
dt.Rows.Add(dr);
}
}

return View(dt);
}

Add below code to your new view, code will generate a HTML table using our ModelTable.

<div style="padding:5px 10px 3px 5px">
<table border="1" cellpadding="5" id="my-table">
<thead>
<tr>
@foreach (System.Data.DataColumn col in Model.Columns)
{
<th>@col.Caption</th>
}</tr>
</thead>
<tbody>
@foreach (System.Data.DataRow row in Model.Rows)
{
<tr>
@foreach (var cell in row.ItemArray)
{
<td>@Html.Raw(cell.ToString())</td>
}</tr>
}</tbody>
</table>
</div>

View name and action result name should be same else mention the view name in the return statement of action result.

In the AppManifest file select the permissions tab and Scope as list and permission as Read

2016-01-24_14-10-05

Run the project in visual studio using play button (F5), now can see SharePoint App Permission approval page, select Documents list then click “Trust it”

2016-01-24_14-13-02

After that SharePoint will redirect to our local hosted site, navigate to newly created view page. Page will display SharePoint list data.

2016-01-24_14-40-48

You can now compare your retrieved data with the actual SharePoint list.

2016-01-24_14-41-22

Comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s