Thursday, February 14, 2008

Get Internal Name of list Item:

Get Internal Name of list Item:

SPSite Site = new SPSite("SiteURI");

SPWeb Web = Site.OpenWeb();

SPList List = Web.Lists["List Name"];

string ColumnName = "My Field Name"; //Name of the field as in list settings

//Update field name with internal name of the column

for (int fc = 0; fc < List.Fields.Count; fc++)

{

if (List.Fields[fc].Title == ColumnName )

{

ColumnName = List.Fields[fc].InternalName;

}

}

string query = "<Query>";

query += " <Where>";

query += " <Eq>";

query += " <FieldRef Name="Author" />";

query += " <Value Type="User">shaiju</Value>";

query += " </Eq>";

query += " </Where>";

query += "</Query>";

SPQuery Query = new SPQuery();

Query.Query = query;

SPListItemCollection listItems = COIList.GetItems(Query);

foreach (SPListItem item in listItems)

{

temp = string.Empty;

try

{

temp = item[ColumnName].ToString();

}

catch (NullReferenceException) { }

if (temp == "E1457")

{

MessageBox.Show(item[ColumnName].ToString());

}

}

1 comment:

Anonymous said...

Thanks - just what I was looking for.

Here are my adaptations::

public static string getIntName(SPList List, string ColName)
{
string sReturn = ColName; // default

try
{
for (int fc = 0; fc < List.Fields.Count; fc++)
{
if (List.Fields[fc].Title.ToUpper() == ColName.Trim().ToUpper())
sReturn = List.Fields[fc].InternalName;
}
}
catch { }

return sReturn;

}

public static string getIntName(SPWeb web, string ListName, string ColName)
{
string sReturn = ColName; // default
SPList List = returnList(web, ListName);

try
{
for (int fc = 0; fc < List.Fields.Count; fc++)
{
if (List.Fields[fc].Title.ToUpper() == ColName.Trim().ToUpper())
sReturn = List.Fields[fc].InternalName;
}
}
catch { }

return sReturn;

}

public static SPList returnList(SPWeb site, string ListName)
{
SPList spl = null;
foreach (SPList list in site.Lists)
{
if (list.Title.Equals(ListName))
{
spl = list;
break;
}
}
return spl;
}
DMG