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);
                if (irfListItems != null)
                    foreach (var irfListItem in irfListItems)
                        new IRFItem
                            Title = irfListItem["Title"].ToString(),
                            StartDate = irfListItem["StartDate"].ToString(),
                            EndDate = irfListItem["EndDate"].ToString(),
                    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
    public ActionResult Index()

        var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

        ViewBag.Username = SharePointService.GetUserName(spContext);

        CamlQuery queryProducts = new CamlQuery();
        queryProducts.ViewXml =
         <ViewFields><FieldRef Name='StartDate' /><FieldRef Name='LinkTitle' /><FieldRef Name='Title' /><FieldRef Name='Author' /><FieldRef Name='EndDate' /><FieldRef Name='ID' /></ViewFields> 

        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)

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.

2 Answers 2


for building sharepoint mvc apps check this link enter image description here

  • That tutorial is a bit old, but i'll take a more in-depth look at it. The problem isnt creating the MVC app, thats easily done by using latest VS2017 and creating a sharepoint add-in. I can get web information and everything else that i need. The problem is when im adding the retrieved items (the one i get from executequery). I'm getting this error -> The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. That error is from "items.Add" because it doesnt wait for executeQuery to finish..
    – Dante R.
    Commented Aug 4, 2018 at 11:42
  • fixed it, but may i ask what does "clientContext.Dispose();" actually do? I'll upvote your answer just cause seeing that include code gave me the idea to test it and it actually threw an error cause a field had a different name.
    – Dante R.
    Commented Aug 4, 2018 at 12:01
  • 1
    some objects are disposable, it means that you should dispose them by yourself, if you don't dispose them then they will consume more memory and connection to them will remain opened. e.g in SharePoint if you write SPSite site = new SPSite.Open("Site url here"); if you don't dispose this object if will remain opened and system will not work properly. To prevent disposable issues you can use using(){} syntax e.g using(SPSite site = new SPSite.Open("Site url here")) { //TODO code here } Commented Aug 4, 2018 at 12:20
  • Got it! Many thanks, that info will come in handy :D.
    – Dante R.
    Commented Aug 4, 2018 at 12:39

Ok so i fixed the problem (been fighting with this for hours and it was so damn simple i wanna punch myself right now).

In case anyone encounters this ->

Why my list didnt get any items (even tho no erorrs) -> Not enough permissions given to the app. Fixed by giving full list permissions and full web permissions.

Why my foreach was giving an error -> Apparently one column had a different name. I figured that out by trying this code (which apparently is better cause it throws an erorr, unlike getting everything from the list) ->

                itemss => itemss.Include(
                    item => item["Title"],
                    item => item["StartDate"],
                    item => item["EndDate"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.