Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Get the List Item Column Titles, Lookup field values from SPLists

$
0
0

Hi there

Below source exporting Lookups data to spreadsheet. But it is NOT exporting all the Lookup data from the List items, just exporting the Lookup data that is in DefaultView.

Came to know that the best practice is to use SPLookupFieldValue approach.

Help appreciated to refactor below source to get Lookup field values.

Guid lstGuid = new Guid(field.LookupList);
                    Guid webGuid = field.LookupWebId;
                    SPWeb spWeb = CT.SpContentType.ParentWeb.Site.AllWebs[webGuid];
                    webUrl = "'" + spWeb.Title + "'(" + spWeb.Url + ")";
                    SPList spList = spWeb.Lists[lstGuid];
                    lst = spList.Title;
                    SPList listUrl = spWeb.GetList(spWeb.Url);
                    string absoluteListUrl = spList.RootFolder.ServerRelativeUrl;


                    SPView view = spList.DefaultView;
                    SPListItemCollection items = spList.GetItems(view);

                    StringCollection viewFields = view.ViewFields.ToStringCollection();

                    //SPList url data to spreadsheet
	            ExcelHelper.SetCellValue(spreadsheet, worksheet, c, r, "'" + lst + "' in web " + webUrl, false);

                    foreach (string fieldName in viewFields)
                    {
                        c = c + 1;
                        //Field Names
			ExcelHelper.SetCellValue(spreadsheet, worksheet, c, r, fieldName, false);
		    }

                    foreach (SPListItem item in items)
                    {
                        c = 2;
                        foreach (string fieldName in viewFields)
                        {

                            var value = item[fieldName];

                            if (value != null)
                            {
                                //Field value
				ExcelHelper.SetCellValue(spreadsheet, worksheet, c++, r + 1, value.ToString().Replace(";#","-"), false);
                            }
                            else
                            {
                                ExcelHelper.SetCellValue(spreadsheet, worksheet, c++, r + 1, "N/A", false);

                            }
                        }
                        r++;

                    }


Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>