SPField Determining a Field Type
Posted: 12/12/2011 Filed under: SharePoint | Tags: Programming, SPField Leave a comment »What if you needed to find all “People or Group” field from a particular list? The code snippet below shows you how easily.
1: using (SPSite oSite = new SPSite("http://mysitecollection"))
2: {
3: using (SPWeb oWeb = oSite.AllWebs["myweb"])
4: {
5: SPList oList = oWeb.Lists["mylist"];
6:
7: foreach (SPField oField in oList.Fields)
8: {
9: if (oField.TypeDisplayName.ToString() == "Person or Group")
10: {
11: Console.WriteLine(oField.Title);
12: Console.WriteLine();
13: }
14: }
15: }
16: }
Note the bit where I used the TypeDisplayName property, this is where I specify the field type I want, and in this case it’s “People or Group”. If I wanted the “Date and Time” field, I’ll just specify “Date and Time”.
How do I get these values? When you create a new column they are listed under Name and Type.
Hope this helps.