Creating WPF DataGrid with dynamic fields from Sharepoint DefaultView

Feb 2, 2011 at 5:06 PM

Hi,

I am developping a WPF Sharepoint 2010 client  windows application.

I need to have a datagrid to display the content of a Document Library based on the DefaultView.

For now, I manage to have the datagrid columns dynamically generated from the ViewFields collection from the DefaultView:

              

                grid.ItemsSource = null;
                grid.Items.Clear();
                grid.Columns.Clear();
                List oList = SPWeb.Lists.GetById(docLib);
                SPCtx.Load(oList);
                SPCtx.Load(oList.Views);
                SPCtx.ExecuteQuery();
                int viewIndex = 0;
                Guid guidVue = Guid.Empty;
                foreach (View vue in oList.Views)
                {
                    if (vue.DefaultView)
                    {
                        guidVue = vue.Id;
                    }
                    viewIndex++;
                }
                View defView = oList.GetView(guidVue);
                SPCtx.Load(oList.GetView(guidVue));
                SPCtx.ExecuteQuery();
                
                ViewFieldCollection viewFields = defView.ViewFields;
                SPCtx.Load(viewFields);
                SPCtx.ExecuteQuery();
                List<string> champs = new List<string>();
                DataGridHyperlinkColumn hlID = new DataGridHyperlinkColumn();
                hlID.Header = "ID";
                hlID.Binding = new Binding("ID");
                grid.Columns.Add(hlID);
                foreach (string fld in viewFields)
                {
                    champs.Add(fld);
                    grid.Columns.Add(CreateColumn(fld));
                 }
                ListItemCollection itemCollection = oList.GetItems(new CamlQuery { ViewXml = "<View />" });   
                SPCtx.Load(itemCollection);//, items => items.Include(item => item["Title"]));   
                SPCtx.ExecuteQuery();
                foreach (ListItem listItem in itemCollection)
                {
                    var row = new DataGridRow();
                    row.Item = GetString(listItem.FieldValues["ID"]);
                    row.Header = "id";
                    var cell = new DataGridCell();
                    
                    grid.Items.Add(row); 
                }

Last 10 lines don't work...

I can't find the way to populate the datagrid's rows...

Is there a way with ItemsSource or whatever?

Thanks for your help