在MOSS里面,很多时候列表都不是单独存在的,列表之间都存在着用编号LookUp的关联关系,但是,我们在编程的时候,不希望显示的是编号,而是名称,所以这就涉及到了列表的关联。经过断点调试,发现可以通过字段的FieldValueType来判断这个列是不是属于LookUp的类型。
先上代码吧,这是两个方法
Code
private DataTable _sourceDataTable;
public DataTable ListToDataTable(string ListName,string strPrimaryKey, string[] pSourceSPListFields,string[] DisplayField)
{
._sourceDataTable = this.ColumnNameDataTable(ListName, pSourceSPListFields,DisplayField);
SPSite site=new SPSite("Http://lxcsmallcity:3000")
SPWeb web = site.OpenWeb(); //实例化一个网站对象,并用网站对象来打开站点
SPList treeList = web.Lists[ListName]; //实例化一个列表对象,用来存放即将要处理的列表
DataTable dt = new DataTable();
SPListItemCollection caseItems = treeList.Items; //实例化一个列表集,用来存放列表里面的对象
进行遍历#region 进行遍历
foreach (SPListItem caseItem in caseItems)
{
DataRow rowNew = this._sourceDataTable.NewRow();
for (int intI = 0; intI < pSourceSPListFields.Length; intI++)
{
if (treeList.Fields[pSourceSPListFields[intI]].FieldValueType.ToString() == "Microsoft.SharePoint.SPFieldLookupValue" && treeList.Fields[pSourceSPListFields[intI]].Title==strPrimaryKey)
{
//找到对应的关联列表(GUID值)
string strLookUpList = ((Microsoft.SharePoint.SPFieldLookup)(treeList.Fields[pSourceSPListFields[intI]])).LookupList;
//找到对应的关联字段
string strLookUpField= ((Microsoft.SharePoint.SPFieldLookup)(treeList.Fields[pSourceSPListFields[intI]])).LookupField;
//取出关联字段的ID
int nRemoveCount=caseItem[pSourceSPListFields[intI]].ToString().IndexOf(";#",0);
string strLookUpValue = caseItem[pSourceSPListFields[intI]].ToString().Substring(0,nRemoveCount);
//通过取到关联列表的GUID实例化一个列表对象
Guid g = new Guid(strLookUpList);
SPList LookUpList = web.Lists[g];
//把“外键列”也加入到结果中
rowNew[pSourceSPListFields[intI]] = caseItem[pSourceSPListFields[intI]];
//对关联列表的字段进行遍历
foreach (string tmp in DisplayField)
{
//通过ID取到对应列的值
rowNew[tmp] = LookUpList.GetItemById(Int32.Parse(strLookUpValue))[tmp].ToString();
}
}
else
{
Object columnType = this._sourceDataTable.Columns[pSourceSPListFields[intI]].DataType;
rowNew[pSourceSPListFields[intI]] = caseItem[pSourceSPListFields[intI]];
}
}
this._sourceDataTable.Rows.Add(rowNew);
}
return this._sourceDataTable;
}
public DataTable ColumnNameDataTable(string ListName, string[] pSourceSPListFields,string[] DisplayField)
{
DataTable dt = new DataTable();
for (int intI = 0; intI < pSourceSPListFields.Length; intI++)
{
DataColumn columnNew = new DataColumn();
columnNew.ColumnName = pSourceSPListFields[intI];
dt.Columns.Add(columnNew);
}
if (DisplayField!=null)
{
for (int intI = 0; intI < DisplayField.Length; intI++)
{
DataColumn columnNew = new DataColumn();
columnNew.ColumnName = DisplayField[intI];
dt.Columns.Add(columnNew);
}
}
return dt;
}
private DataTable _sourceDataTable;
public DataTable ListToDataTable(string ListName,string strPrimaryKey, string[] pSourceSPListFields,string[] DisplayField)
{
._sourceDataTable = this.ColumnNameDataTable(ListName, pSourceSPListFields,DisplayField);
SPSite site=new SPSite("Http://lxcsmallcity:3000")
SPWeb web = site.OpenWeb(); //实例化一个网站对象,并用网站对象来打开站点
SPList treeList = web.Lists[ListName]; //实例化一个列表对象,用来存放即将要处理的列表
DataTable dt = new DataTable();
SPListItemCollection caseItems = treeList.Items; //实例化一个列表集,用来存放列表里面的对象
进行遍历#region 进行遍历
foreach (SPListItem caseItem in caseItems)
{
DataRow rowNew = this._sourceDataTable.NewRow();
for (int intI = 0; intI < pSourceSPListFields.Length; intI++)
{
if (treeList.Fields[pSourceSPListFields[intI]].FieldValueType.ToString() == "Microsoft.SharePoint.SPFieldLookupValue" && treeList.Fields[pSourceSPListFields[intI]].Title==strPrimaryKey)
{
//找到对应的关联列表(GUID值)
string strLookUpList = ((Microsoft.SharePoint.SPFieldLookup)(treeList.Fields[pSourceSPListFields[intI]])).LookupList;
//找到对应的关联字段
string strLookUpField= ((Microsoft.SharePoint.SPFieldLookup)(treeList.Fields[pSourceSPListFields[intI]])).LookupField;
//取出关联字段的ID
int nRemoveCount=caseItem[pSourceSPListFields[intI]].ToString().IndexOf(";#",0);
string strLookUpValue = caseItem[pSourceSPListFields[intI]].ToString().Substring(0,nRemoveCount);
//通过取到关联列表的GUID实例化一个列表对象
Guid g = new Guid(strLookUpList);
SPList LookUpList = web.Lists[g];
//把“外键列”也加入到结果中
rowNew[pSourceSPListFields[intI]] = caseItem[pSourceSPListFields[intI]];
//对关联列表的字段进行遍历
foreach (string tmp in DisplayField)
{
//通过ID取到对应列的值
rowNew[tmp] = LookUpList.GetItemById(Int32.Parse(strLookUpValue))[tmp].ToString();
}
}
else
{
Object columnType = this._sourceDataTable.Columns[pSourceSPListFields[intI]].DataType;
rowNew[pSourceSPListFields[intI]] = caseItem[pSourceSPListFields[intI]];
}
}
this._sourceDataTable.Rows.Add(rowNew);
}
return this._sourceDataTable;
}
public DataTable ColumnNameDataTable(string ListName, string[] pSourceSPListFields,string[] DisplayField)
{
DataTable dt = new DataTable();
for (int intI = 0; intI < pSourceSPListFields.Length; intI++)
{
DataColumn columnNew = new DataColumn();
columnNew.ColumnName = pSourceSPListFields[intI];
dt.Columns.Add(columnNew);
}
if (DisplayField!=null)
{
for (int intI = 0; intI < DisplayField.Length; intI++)
{
DataColumn columnNew = new DataColumn();
columnNew.ColumnName = DisplayField[intI];
dt.Columns.Add(columnNew);
}
}
return dt;
}
Code
//调用
//这个相当于是主表
string[] FirstInfoField = new string[] { "First_ID", "Second_ID", "FirstName"};
//这个是要关联的表
string[] SecondInfoField = new string[] { "Second_Name", "ReMark"};
//调用方法
DataTable dtOutbound = ListToDataTable("First_Info", "Second_ID", FirstInfoField, SecondInfoField);
//调用
//这个相当于是主表
string[] FirstInfoField = new string[] { "First_ID", "Second_ID", "FirstName"};
//这个是要关联的表
string[] SecondInfoField = new string[] { "Second_Name", "ReMark"};
//调用方法
DataTable dtOutbound = ListToDataTable("First_Info", "Second_ID", FirstInfoField, SecondInfoField);
OK,这样就可以实现两个列表的关联了,现在暂时只是考虑两个列表之间的关联,多个列表之间还没去写。
第一次写文,如有不足之处,还请批评指导,谢谢