Last active
August 29, 2015 14:07
-
-
Save neremin/1c0771c0c3beb0260d2a to your computer and use it in GitHub Desktop.
SharePoint's MultiChoice field extensions for Mappings. Tested on SP2013 Foundation.
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
/// <devdoc> | |
/// Based on Andrey Markeev's example http://sharepoint.stackexchange.com/a/28522 | |
/// </devdoc> | |
public static class SPFieldMultiChoiceExtensions | |
{ | |
public const string MultiValueSeparator = ";#"; | |
private const string ValueAttributeName = "Value"; | |
private const string MappingsElementName = "MAPPINGS"; | |
private const string MappingElementName = "MAPPING"; | |
public static string GetMappedKey<T>(this SPFieldMultiChoice field, T value, string resourceFile = ResourceFileNames.Core) | |
{ | |
return GetMappingKeyByValue(field.Mappings, Convert.ToString(value), resourceFile); | |
} | |
public static string GetMappedValue<T>(this SPFieldMultiChoice field, T value, string resourceFile = ResourceFileNames.Core) | |
{ | |
return GetMappingValueByKey(field.Mappings, Convert.ToString(value), resourceFile); | |
} | |
private static string GetMappingKeyByValue(string mappingsXml, string fieldMultiValue, string resourceFile) | |
{ | |
return string.Join | |
( | |
MultiValueSeparator, | |
SelectMappingNodes | |
( | |
mappingsXml, fieldMultiValue, | |
(m, value) => LocalizedEqual(m.Value, value, resourceFile) | |
) | |
.Select(m => m.Attribute(ValueAttributeName).Value) | |
); | |
} | |
private static string GetMappingValueByKey(string mappingsXml, string fieldMultiValue, string resourceFile) | |
{ | |
return string.Join | |
( | |
MultiValueSeparator, | |
SelectMappingNodes | |
( | |
mappingsXml, fieldMultiValue, | |
(m, value) => m.Attribute(ValueAttributeName).Value.Equals(value) | |
) | |
.Select(m => GetLocalizedValue(m.Value, resourceFile)) | |
); | |
} | |
private static IEnumerable<XElement> SelectMappingNodes(string mappingsXml, string fieldMultiValue, Func<XElement, string, bool> mappingMatch) | |
{ | |
var document = XDocument.Parse(mappingsXml); | |
var mappings = document.Element(MappingsElementName) ?? new XElement(MappingsElementName); | |
return fieldMultiValue.Split(new string[] { MultiValueSeparator }, StringSplitOptions.RemoveEmptyEntries) | |
.Select(value => mappings.Elements(MappingElementName).FirstOrDefault(m => mappingMatch(m, value))) | |
;//.Where(foundMapping => foundMapping != null); | |
} | |
private static bool LocalizedEqual(string mappingValue, string value, string resourceFile) | |
{ | |
return GetLocalizedValue(mappingValue, resourceFile).Equals(value); | |
} | |
private static string GetLocalizedValue(string mappingValue, string resourceFile) | |
{ | |
if (mappingValue.TrimStart().StartsWith("$")) | |
return SPUtility.GetLocalizedString(mappingValue, resourceFile, (uint)Thread.CurrentThread.CurrentUICulture.LCID); | |
return mappingValue; | |
} | |
} |
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
[TestFixture] | |
public class SPFieldMultiChoiceExtensionsTests | |
{ | |
private const string Delimiter = SPFieldMultiChoiceExtensions.MultiValueSeparator; | |
private const string NotStarted = "Not Started"; | |
private const string InProgress = "In Progress"; | |
private const string Completed = "Completed"; | |
private const string Postponed = "Postponed"; | |
private const string Waiting = "Waiting"; | |
private const string mappingsXml = | |
@"<MAPPINGS> | |
<MAPPING Value=""1"">" + NotStarted + @"</MAPPING> | |
<MAPPING Value=""2"">" + InProgress + @"</MAPPING> | |
<MAPPING Value=""3"">" + Completed + @"</MAPPING> | |
<MAPPING Value=""4"">" + Postponed + @"</MAPPING> | |
<MAPPING Value=""5"">" + Waiting + @"</MAPPING> | |
</MAPPINGS>"; | |
[TestCase(Completed, | |
Result = "3")] | |
[TestCase(NotStarted + Delimiter + Waiting, | |
Result = "1" + Delimiter + "5")] | |
[TestCase(Postponed + Delimiter + InProgress + Delimiter + Waiting, | |
Result = "4" + Delimiter + "2" + Delimiter + "5")] | |
public string GetMappedKey_finds_correct_values(string values) | |
{ | |
using (ShimsContext.Create()) | |
{ | |
SPFieldMultiChoice field = new ShimSPFieldMultiChoice | |
{ | |
MappingsGet = () => mappingsXml | |
}; | |
return field.GetMappedKey(values); | |
} | |
} | |
[TestCase("3", | |
Result = Completed)] | |
[TestCase("1" + Delimiter + "5", | |
Result = NotStarted + Delimiter + Waiting)] | |
[TestCase("4" + Delimiter + "2" + Delimiter + "5", | |
Result = Postponed + Delimiter + InProgress + Delimiter + Waiting)] | |
public string GetMappedValue_finds_correct_keys(string keys) | |
{ | |
using (ShimsContext.Create()) | |
{ | |
SPFieldMultiChoice field = new ShimSPFieldMultiChoice | |
{ | |
MappingsGet = () => mappingsXml | |
}; | |
return field.GetMappedValue(keys); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment