Search This Blog

Monday, October 3, 2011

DataTable GetUniqueData

public System.Data.DataTable GetUniqueData()
SPQuery objSPQuery = null;
SPListItemCollection objSPListItemCollection;
SPList objSPList;
System.Data.DataTable objDataTable;
DataView objDataView;

using (SPSite objSPSite = new SPSite("<Your Site URl>"))
using (SPWeb objSPWeb = objSPSite.OpenWeb())

objSPQuery = new SPQuery();

//write your query here
objSPQuery.Query = @"<Where>
<FieldRef Name='ID' />

objSPList = objSPWeb.Lists["<List Name>"];
//Fill the list item collection
objSPListItemCollection = objSPList.GetItems(objSPQuery);

if (objSPListItemCollection.Count > 0)
//convert the SPListItemCollection to Datatable
objDataTable = objSPListItemCollection.GetDataTable();

//Create a Dataview for applying UNIQUE
objDataView = new DataView(objDataTable);

//Convert the datatview to Table again by
specifying paramater 'distict' to true.
// the second parameter should be the column to be
checked for unique.
objDataTable = objDataView.ToTable(true, "<column name(s) that you want to check for unique>");
catch(Exception Ex)
throw Ex;
return objDataTable;

No comments:

Post a Comment