Followings are the extension methods for SPListItem class.
Single line of text, Multiline lines of text and Choice(Single)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string GetStringValue(this SPListItem item, string columnName) | |
{ | |
return item[columnName] as String; | |
} | |
public static void SetStringValue(this SPListItem item, string columnName, string value) | |
{ | |
item[columnName] = value; | |
} |
Choice (Multiple)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static SPFieldMultiChoiceValue GetMultipleChoiceValueCollection(this SPListItem item, string columnName) | |
{ | |
return (item[columnName] == null) ? null : new SPFieldMultiChoiceValue(item[columnName] as string); | |
} | |
public static void SetMultipleChoiceValueCollection(this SPListItem item, string columnName, string[] choices) | |
{ | |
var value = new SPFieldMultiChoiceValue(); | |
foreach (var choice in choices) | |
{ | |
value.Add(choice); | |
} | |
item[columnName] = value; | |
} | |
public static void SetChoiceToMultipleChoiceValueCollection(this SPListItem item, string columnName, string choice) | |
{ | |
SPFieldMultiChoiceValue choices = (item[columnName] == null) | |
? new SPFieldMultiChoiceValue() | |
: new SPFieldMultiChoiceValue(item[columnName] as string); | |
choices.Add(choice); | |
item[columnName] = choices; | |
} |
Number, Currency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Double? GetNumberValue(this SPListItem item, string columnName) | |
{ | |
return item[columnName] as Double?; | |
} | |
public static void SetNumberValue(this SPListItem item, string columnName, Double value) | |
{ | |
item[columnName] = value; | |
} |
DateTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static DateTime? GetDateTimeValue(this SPListItem item, string columnName) | |
{ | |
return item[columnName] as DateTime?; | |
} | |
public static void SetDateTimeValue(this SPListItem item, string columnName, DateTime value) | |
{ | |
item[columnName] = value; | |
} |
Lookup (Single)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static SPFieldLookupValue GetLookupValue(this SPListItem item, string columnName) | |
{ | |
return (item[columnName] == null) ? null : new SPFieldLookupValue(item[columnName] as string); | |
} | |
public static void SetLookupValue(this SPListItem item, string columnName, int itemId) | |
{ | |
item[columnName] = itemId; | |
} |
Lookup (Multiple)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static SPFieldLookupValueCollection GetLookupValueCollection(this SPListItem item, string columnName) | |
{ | |
return (item[columnName] == null) | |
? null | |
: new SPFieldLookupValueCollection(item[columnName] as string); | |
} | |
public static void SetLookupValueCollection(this SPListItem item, string columnName, int[] itemIds) | |
{ | |
SPWeb web = item.Web; | |
var value = new SPFieldLookupValueCollection(); | |
value.AddRange(itemIds.Select(i => new SPFieldLookupValue(i.ToString()))); | |
item[columnName] = value; | |
} | |
public static void SetLookupValueToLookupValueCollection(this SPListItem item, string columnName, int itemId) | |
{ | |
SPFieldLookupValueCollection value = (item[columnName] != null) | |
? value = item[columnName] as SPFieldLookupValueCollection | |
: value = new SPFieldLookupValueCollection(); | |
value.Add(new SPFieldLookupValue(itemId.ToString())); | |
item[columnName] = value; | |
} |
Yes/No
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Boolean? GetYesNoValue(this SPListItem item, string columnName) | |
{ | |
return item[columnName] as Boolean?; | |
} | |
public static void SetYesNoValue(this SPListItem item, string columnName, Boolean value) | |
{ | |
item[columnName] = value; | |
} |
User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static SPFieldUserValue GetUserValue(this SPListItem item, string columnName) | |
{ | |
SPFieldUser field = item.Fields[columnName] as SPFieldUser; | |
if (field != null) | |
{ | |
SPFieldUserValue fieldValue = field.GetFieldValue(item[columnName].ToString()) as SPFieldUserValue; | |
return (fieldValue == null) ? null : fieldValue; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
public static void SetUserValue(this SPListItem item, string columnName, int userId) | |
{ | |
SPWeb web = item.Web; | |
item[columnName] = new SPFieldUserValue(web, userId.ToString()); | |
} |
User Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static SPFieldUserValueCollection GetUserValueCollection(this SPListItem item, string columnName) | |
{ | |
return (item[columnName] == null) ? null : item[columnName] as SPFieldUserValueCollection; | |
} | |
public static void SetUserValueCollection(this SPListItem item, string columnName, int[] userIds) | |
{ | |
SPWeb web = item.Web; | |
var value = new SPFieldUserValueCollection(); | |
value.AddRange(userIds.Select(i => new SPFieldUserValue(web, i.ToString()))); | |
item[columnName] = value; | |
} | |
public static void SetUserToValueCollection(this SPListItem item, string columnName, int userId) | |
{ | |
SPWeb web = item.Web; | |
SPFieldUserValueCollection value = (item[columnName] != null) | |
? value = item[columnName] as SPFieldUserValueCollection | |
: value = new SPFieldUserValueCollection(); | |
value.Add(new SPFieldUserValue(web, userId.ToString())); | |
item[columnName] = value; | |
} |
Hyperlink or Picture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static SPFieldUrlValue GetUrlValue(this SPListItem item, string columnName) | |
{ | |
return (item[columnName] == null) ? null : new SPFieldUrlValue(item[columnName] as string); | |
} | |
public static void SetUrlValue(this SPListItem item, string columnName, string url, string description) | |
{ | |
var value = new SPFieldUrlValue { Url = url, Description = description }; | |
item[columnName] = value; | |
} |
Managed Metadata (Single)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static TaxonomyFieldValue GetTaxonomyValue(this SPListItem item, string columnName) | |
{ | |
return item[columnName] as TaxonomyFieldValue; | |
} | |
public static void SetTaxonomyValue(this SPListItem item, string columnName, Guid termId) | |
{ | |
var field = item.Fields[columnName] as TaxonomyField; | |
var session = new TaxonomySession(item.Web.Site); | |
Term term = session.GetTerm(termId); | |
field.SetFieldValue(item, term); | |
} |
Managed Metadata (Multiple)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static TaxonomyFieldValueCollection GetTaxonomyValueCollection(this SPListItem item, string columnName) | |
{ | |
return item[columnName] as TaxonomyFieldValueCollection; | |
} | |
public static void SetTaxonomyValueCollection(this SPListItem item, string columnName, Guid[] termIds) | |
{ | |
var field = item.Fields[columnName] as TaxonomyField; | |
var session = new TaxonomySession(item.Web.Site); | |
TermCollection terms = session.GetTerms(termIds); | |
field.SetFieldValue(item, terms); | |
} | |
public static void SetTaxonomyToTaxonomyValueCollection(this SPListItem item, string columnName, Guid termId) | |
{ | |
var field = item.Fields[columnName] as TaxonomyField; | |
var session = new TaxonomySession(item.Web.Site); | |
TaxonomyFieldValueCollection collection = (item[columnName] != null) | |
? collection = item[columnName] as TaxonomyFieldValueCollection | |
: collection = new TaxonomyFieldValueCollection(field); | |
Term term = session.GetTerm(termId); | |
TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue(field) | |
{ | |
TermGuid = term.Id.ToString(), | |
Label = term.Name | |
}; | |
collection.Add(taxonomyFieldValue); | |
field.SetFieldValue(item, collection); | |
} |
You can download SPListItemExtensions.cs file containing all above extension methods.