namespace SharePointExtensions.ListItem { public static class ListItemExtensions { // Get value from text/multiline/single-choice field public static string GetStringValue(this SPListItem item, string columnName) { return item[columnName] as String; } // Get value from number field public static Double? GetNumberValue(this SPListItem item, string columnName) { return item[columnName] as Double?; } // Get value from currency field public static Double? GetCurrencyValue(this SPListItem item, string columnName) { return item[columnName] as Double?; } // Get value from datetime field public static DateTime? GetDateTimeValue(this SPListItem item, string columnName) { return item[columnName] as DateTime?; } // Get value from multiple choice field public static SPFieldMultiChoiceValue GetMultipleChoiceValueCollection(this SPListItem item, string columnName) { if (item[columnName] == null) return null; return new SPFieldMultiChoiceValue(item[columnName] as string); } // Set multi choice field to the given choices 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; item.Update(); } // Add choice to multi choice field public static void AddChoiceToMultipleChoiceValueCollection(this SPListItem item, string columnName, string choice) { SPFieldMultiChoiceValue value; if (item[columnName] != null) value = item[columnName] as SPFieldMultiChoiceValue; else value = new SPFieldMultiChoiceValue(); value.Add(choice); item[columnName] = value; item.Update(); } // Get value from lookup field public static SPFieldLookupValue GetLookupValue(this SPListItem item, string columnName) { if (item[columnName] == null) return null; return new SPFieldLookupValue(item[columnName] as string); } // Set value of a lookup field public static void SetLookupValue(this SPListItem item, string columnName, int itemId) { item[columnName] = itemId; } // Get value from multiple lookup field public static SPFieldLookupValueCollection GetLookupValueCollection(this SPListItem item, string columnName) { if (item[columnName] == null) return null; return new SPFieldLookupValueCollection(item[columnName] as string); } // Set value of multiple lookup field public static void SetLookupValueCollection(this SPListItem item, string columnName, int[] itemIds) { var value = new SPFieldLookupValueCollection(); foreach (int i in itemIds) value.Add(new SPFieldLookupValue(i.ToString())); item[columnName] = value; } // Add value tos multiple lookup field public static void AddLookupToLookupValueCollection(this SPListItem item, string columnName, int itemId) { SPFieldLookupValueCollection value; if (item[columnName] != null) value = item[columnName] as SPFieldLookupValueCollection; else value = new SPFieldLookupValueCollection(); value.Add(new SPFieldLookupValue(itemId.ToString())); item[columnName] = value; } // Get value from true/false field public static Boolean? GetTrueFalseValue(this SPListItem item, string columnName) { return item[columnName] as Boolean?; } // Get value from user field public static SPFieldUserValue GetUserValue(this SPListItem item, string columnName) { if (item[columnName] == null) return null; var userField = item.ParentList.Fields.GetFieldByInternalName(columnName); return userField.GetFieldValue(item[columnName] as string) as SPFieldUserValue; } // Get value from multi user field public static SPFieldUserValueCollection GetUserValueCollection(this SPListItem item, string columnName) { if (item[columnName] == null) return null; return item[columnName] as SPFieldUserValueCollection; } // Set value of multi user field public static void SetUserValueCollection(this SPListItem item, string columnName, int[] userIds) { var web = item.Web; var value = new SPFieldUserValueCollection(); foreach (int i in userIds) value.Add(new SPFieldUserValue(web, i.ToString())); item[columnName] = value; } // Add value to multi user field public static void AddUserToValueCollection(this SPListItem item, string columnName, int userId) { var web = item.Web; SPFieldUserValueCollection value; if (item[columnName] != null) value = item[columnName] as SPFieldUserValueCollection; else value = new SPFieldUserValueCollection(); value.Add(new SPFieldUserValue(web, userId.ToString())); item[columnName] = value; } // Get value for url and image field public static SPFieldUrlValue GetUrlValue(this SPListItem item, string columnName) { if (item[columnName] == null) return null; return new SPFieldUrlValue(item[columnName] as string); } // Set value for url and image field public static void SetUrlValue(this SPListItem item, string columnName, string url, string description) { var value = new SPFieldUrlValue { Url = url, Description = description }; item[columnName] = value; } // Get value for Managed Metadata field public static TaxonomyFieldValue GetTaxonomyValue(this SPListItem item, string columnName) { return item[columnName] as TaxonomyFieldValue; } // Set value for Managed Metadata field 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); var term = session.GetTerm(termId); field.SetFieldValue(item, term); } // Get values for Managed Metadata field public static TaxonomyFieldValueCollection GetTaxonomyValueCollection(this SPListItem item, string columnName) { return item[columnName] as TaxonomyFieldValueCollection; } // Set values for Managed Metadata field 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); var terms = session.GetTerms(termIds); field.SetFieldValue(item, terms); } } }
18 July, 2012
Extension Methods for SharePoint fields
By combining the blog on 'Reading and writing SharePoint field values' and 'Extension Methods' the following extension methods for reading and writing data to SharePoint fields have been implemented:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment