3

I'm using Linq to grab my SharePoint data (used CKS tools to create the class). I've been having some headaches with using it especially around choice fields. All of the choice values are converted into an enum object and all choice values lose their spaces, for example my choice field ID Type has two choices, and when dumped into Linq I lose the spaces, i.e.

public enum IDType : int {

    None = 0,

    Invalid = 1,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Federal Tax ID")]
    FederalTaxID = 2,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Social Security")]
    SocialSecurity = 4,
}

When i go to display the values to the user I am doing item.IDType.ToString(). This returns FederalTaxID, spaces missing. I understand how/why, but i don't understand how to best handle this so it's user friendly.

My workaround has been to change the properties in the class from Choice to String and remove the enum ref and set as a string. This gets me my spaces...

from
private System.Nullable<IDType> _iDType;
to
private string _iDType;

and

from
    [Microsoft.SharePoint.Linq.ColumnAttribute(Name="IDType", Storage="_iDType", FieldType="Choice")]
    public System.Nullable<IDType> IDType {
        get {
to
    [Microsoft.SharePoint.Linq.ColumnAttribute(Name="IDType", Storage="_iDType", FieldType="text")]
    public string IDType {
        get {

I now have to write back to this list, updating a list item. This can't happen since I changed the type from Choice to String. I get a whole lotta conversion errors...

I found an article http://blogs.msdn.com/b/sharepointdev/archive/2011/05/03/using-linq-to-sharepoint-with-choice-fields.aspx which walks through a long work around but it looks much to long to be legitimate. I expected something as innovative as Linq to SharePoint, especially for SharePoint, would have some legitimate processes for updating. My workaround for this is to use the SharePoint API and get my good 'ole SPListItem and update it.

And another frustration since I'm venting (not with choice fields but with Linq) is if i add another list or add a field to a list, i need to rerun the CKS tool which then recreates the class file, and then I have to go through and change all of my enums back to string...

Is there another way around this? Or is LINQ to SharePoint making more work for me? Is there a best practices out there?

Thanks

2 Answers 2

4

Try using this extension method to get the actual string value of your enum.

string theFullType = item.IDType.StringValueOf();


public static class Extensions
{
    public static string StringValueOf(this Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());
        Microsoft.SharePoint.Linq.ChoiceAttribute[] attributes =
            (Microsoft.SharePoint.Linq.ChoiceAttribute[])fi.GetCustomAttributes(
            typeof(Microsoft.SharePoint.Linq.ChoiceAttribute), false);
        if (attributes.Length > 0)
        {
            return attributes[0].Value;
        }
        else
        {
            return value.ToString();
        }
    }
}
1
  • Thanks, that'll get me a value, but what if the users add another value to the choice field? The associated enum doesn't know about it, and will return an Invalid... Dec 16, 2011 at 14:43
0

You can use the SPMetal parameters file to override the defaults, so that choice fields are generated as strings. See http://msdn.microsoft.com/en-us/library/ee535056.aspx for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.