Tuesday, January 15, 2013

Linq to sharepoint library automatically removes spaces between choice options, to get correct choice values we have to use extention methods as described below

http://sharepoint.stackexchange.com/questions/21750/frustrated-linq-to-sharepoint-choice-values-losing-spaces

http://blogs.msdn.com/b/sharepointdev/archive/2011/05/03/using-linq-to-sharepoint-with-choice-fields.aspx

this method does not work for multiple choice fields, so I have created a new static method (which is not an extension method)

public static string StringValueOf(object obj,string propertyName)
{string returnValue = string.Empty;
if (((Microsoft.SharePoint.Linq.ColumnAttribute)(obj.GetType().GetProperty(propertyName).GetCustomAttributes(false)[0])).FieldType == "MultiChoice")
{string allvalues = Convert.ToString(obj.GetType().GetProperty(propertyName).GetValue(obj,null));
string[] values = allvalues.Split(",".ToCharArray());
foreach (string value in values)
{string actualValue = string.Empty;
FieldInfo fi = obj.GetType().GetProperty(propertyName).GetValue(obj, null).GetType().GetField(value.Trim());
Microsoft.SharePoint.Linq.ChoiceAttribute[] attributes = (Microsoft.SharePoint.Linq.ChoiceAttribute[])fi.GetCustomAttributes(typeof(Microsoft.SharePoint.Linq.ChoiceAttribute), false);
if (attributes.Length > 0)
{
actualValue = attributes[0].Value;
}
else
{
actualValue = value.ToString();
}
if (returnValue.Length > 0)
{
returnValue +=
", " + actualValue;
}
else
{
returnValue = actualValue;
}
}
}
else
{
string value = Convert.ToString(obj.GetType().GetProperty(propertyName).GetValue(obj, null));
FieldInfo fi = obj.GetType().GetProperty(propertyName).GetValue(obj, null).GetType().GetField(value);
Microsoft.SharePoint.Linq.ChoiceAttribute[] attributes = (Microsoft.SharePoint.Linq.ChoiceAttribute[])fi.GetCustomAttributes(typeof(Microsoft.SharePoint.Linq.ChoiceAttribute), false);
if (attributes.Length > 0)
{
returnValue = attributes[0].Value;
}          
else
{
returnValue = value.ToString();
}
}
return returnValue;
}

No comments:

SSL Error - The connection for this site is not secure

 After cloning a git repo of dot net framework website and trying to run it all I could see was this error Turns out the fix was to simply e...