Posts

Showing posts with the label List

SharePoint | Create a dropdown list populated with list items

Image
Problem: Create an html control with data from a SharePoint location (list) Solution: Add the (dropdownlist) control through the SharePoint Designer design view and associate a new data source to it, by clicking "Items" value, and choosing "Add a new data source". Tip: The allowintegratedsecurity="true" parameter will keep the page from throwing an exception due to the Integrated Security check from web.config default settings.

Compare a string with a list of strings using IEqualityComparer

Problem: Check if a string appears, partially, on a list of strings. The List.Contains() method does this, but it only returns complete matches. Solution: Create a custom IEQualityComparer. string[] excludePages = { "string1", "string2", "string3", "str"}; StringEqualityComparer comparer = new StringEqualityComparer(); if (excludePages.Contains("string2", comparer)) { //string 2 exists in excludePages } else { //string 2 does not exist in excludePages } public class StringEqualityComparer : IEqualityComparer { public bool Equals(string currentListString, string testString) { return (testString.Contains(currentListString)); } public int GetHashCode(string obj) { return obj.GetHashCode(); } } Source: http://www.java2s.com/Code/CSharp/LINQ/ContainswithstringvalueandIEqualityComparer.htm

Creating a list of double string items

Situation: You can to create a List object to save two different string values, without using string splits or substrings. Solution: Use the KeyValuePair structure. public List<KeyValuePair<string, string>> GetAllTimeZones() { List <KeyValuePair<string, string>> timeZones = new List<KeyValuePair<string, string>>(); timeZones.Add(new KeyValuePair<string, string>(string1, string2)); } return timeZones; } To retrieve data: foreach (KeyValuePair<string, string> currentTimeZone in GetAllTimeZones()) { ddlTimeZones.Items.Add(new ListItem(currentTimeZone.Key, currentTimeZone.Value)); }