Search code examples
asp.netgridviewsharepointsharepoint-clientobject

Sharepoint: Binding GridView with SharePoint List (Client Object Model)


I want to show a list on an .aspx site. Therefore I have to use the SP client object model.

I found the following tutorial, but this doesn't use the client libraries: http://social.technet.microsoft.com/wiki/contents/articles/30287.binding-gridview-with-sharepoint-list.aspx

My code so far looks the following:

ClientContext clientContext = GetContext(accessToken);

Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();

// Get the email input list.
List inboxList = web.Lists.GetByTitle("InboxList");
Microsoft.SharePoint.Client.ListItemCollection items = inboxList.GetItems(new CamlQuery());

 clientContext.Load(inboxList);
 clientContext.Load(items, ic => ic.Include(i => i["DisplayName"], i => i["Client_Title"], i => i["HasUniqueRoleAssignments"]));
 clientContext.ExecuteQuery();



 foreach (Microsoft.SharePoint.Client.ListItem i in items)
 {
     clientContext.Load(i);
 }

 clientContext.ExecuteQuery();
 oGrid.DataSource = items;
 oGrid.DataBind();

But this shows only some "meta data" of the list item collection, see screenshot: enter image description here

If I use oGrid.DataSource = inboxList; I get an InvalidOperationException because the data source isn't type of IListSource, IEnumerable nor IDataSource.

If I use oGrid.DataSource = inboxList.DataSource; I get an PropertyOrFieldNotInitializedException, but I don't know how to load this attribute (via clientContext.Load it didn't work)?!


Solution

  • I got it - works with following code:

     protected void Page_Load(object sender, EventArgs e)
     {
            ...
                ClientContext clientContext = GetContext(accessToken);
    
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.ExecuteQuery();
    
                // Get the email input list.
                List inboxList = web.Lists.GetByTitle("InboxList");
                Microsoft.SharePoint.Client.ListItemCollection items = inboxList.GetItems(new CamlQuery());
    
                clientContext.Load(inboxList);
                clientContext.Load(items);
                clientContext.ExecuteQuery();
    
                foreach (Microsoft.SharePoint.Client.ListItem i in items)
                {
                    clientContext.Load(i);
                }
                clientContext.ExecuteQuery();
    
                oGrid.DataSource = GetInboxListData(inboxList, items);
                oGrid.DataBind();
    
            }
            else if (!IsPostBack)
            {
                Response.Write("Could not find a context token.");
                return;
            }
        }
    
        private DataTable GetInboxListData(List inboxList, Microsoft.SharePoint.Client.ListItemCollection items)
        {
            DataTable dt = new DataTable();
    
            dt.Columns.Add("From");
            dt.Columns.Add("To");
            dt.Columns.Add("Subject");
            dt.Columns.Add("Body");
            dt.Columns.Add("Attachments");
            dt.Columns.Add("Sent");
    
            DataRow row;
    
            foreach(Microsoft.SharePoint.Client.ListItem item in items)
            {
                row = dt.Rows.Add();
                row["From"] = item["From1"].ToString();
                row["To"] = item["To"].ToString();
                row["Subject"] = item["Subject1"].ToString();
                row["Body"] = item["Body1"].ToString();
                row["Attachments"] = item["Attachments"].ToString();
                row["Sent"] = item["Sent"].ToString();
            }
            return dt;
        }
    

    This is similar to Retrieve the values from a list to Gridview in SharePoint Webpart? but with client object model methods & objects.