So...i created a Sharepoint Add-in (C# MVC) to get list information and create/update items. I've done the creating/updating in the past, not gonna tackle that now.
My problem here is displaying the list items into the MVC View. What i've done so far ->
I created a model (class) with the information that i'll show in the table:
public class IRFItem
{
public string Title { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
//public string CreatedBy { get; set; }
}
In the same file (for the sake of keeping my tests compact) i also added a way to get the items i need:
public static List<IRFItem> GetItems(SharePointContext spContext, CamlQuery camlQuery)
{
List<IRFItem> items = new List<IRFItem>();
//var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current);
using (var clientContext = spContext.CreateUserClientContextForSPAppWeb())
{
if (clientContext != null)
{
List irfList = clientContext.Web.Lists.GetByTitle("IRF");
ListItemCollection irfListItems = irfList.GetItems(camlQuery);
clientContext.Load(irfListItems);
clientContext.ExecuteQuery();
if (irfListItems != null)
{
foreach (var irfListItem in irfListItems)
{
items.Add(
new IRFItem
{
Title = irfListItem["Title"].ToString(),
StartDate = irfListItem["StartDate"].ToString(),
EndDate = irfListItem["EndDate"].ToString(),
});
}
}
else
{
items.Add(
new IRFItem
{
Title = "Empty",
StartDate = "Empty",
EndDate = "Empty"
});
}
}
}
return items;
}
In my custom controller (called SharepointController so i dont mess up with the default ones) i added this ->
// GET: Sharepoint
[SharePointContextFilter]
public ActionResult Index()
{
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
ViewBag.Username = SharePointService.GetUserName(spContext);
CamlQuery queryProducts = new CamlQuery();
queryProducts.ViewXml =
@"<View>
<ViewFields><FieldRef Name='StartDate' /><FieldRef Name='LinkTitle' /><FieldRef Name='Title' /><FieldRef Name='Author' /><FieldRef Name='EndDate' /><FieldRef Name='ID' /></ViewFields>
</View>";
List<IRFItem> items = SharePointService.GetItems(spContext, queryProducts);
//SharePointService.GetItems(spContext, queryProducts);
return View(items);
}
And finally my desired view contains->
@foreach (AddinTest1Web.Models.IRFItem irfItem in Model)
{
<tr>
<td>@irfItem.Title</td>
<td>@irfItem.StartDate</td>
<td>@irfItem.EndDate</td>
</tr>
}
I get no errors, but my table is always empty...I even added that else part in GetItems to create an item that shows empty just so i know if its a sharepoint related problem or something else. My MVC experience isn't much to be honest (just started learning it a week ago, but im the kind of guy that learns by doing).
Does anyone see any problems here? I've followed a tutorial for this and made my own little changes.
Ref: https://www.chakkaradeep.com/2013/10/18/building-apps-for-sharepoint-with-mvc/
I can get the site title/description by using CSOM (i tested by appending that to items.Add() and it does display. However while debugging im getting count 0 on the SP List.
Any kind of help will be highly appreciated, thx.
EDIT: I jumped over the error by giving the application more permissions (to list & web just to be safe) and i am getting back results, however i am unable to create the items because executeQuery does not finish on time. Any idea how to delay that? I remember i had a bigggg problem with tasks in the past so i have no idea where to start here.