locked
Read value of an item RRS feed

  • Question

  • A new column "MyColumn" is added to a list which will host a semicolon delimited list of people aliases. When trying to read the values back programmatically as in

    item["MyColumn"].ToString(), I would get some additional chars like "1;#Joe;#23;#Henry;#19;#Andrew"

     

    The values added were Joe;Henry;Andrew.

     

    Does anyone know whats the proper way to extract these values other than parsing them out?

    Thursday, January 24, 2008 4:10 AM

Answers

  • You're seeing the ;# delimiter because this is how SharePoint stores a collection of values in an SPField when the SPField type you're using inherits from the SPFieldLookup object.  In your case, you've got a User field that allows multiple values.

     

    Most of the SPFieldLookup types have an associates SPFieldLookupValue type to aid in the retrieval of values from the field, and alleviates the need to do any string or regular expression parsing which can be inefficient when obtaining values from an SPField in SharePoint. To really make what you're trying to do easy, you can construct an SPFieldUserValueCollection object using the value of your field.  This object is a Generic List of SPFieldUserValue objects, each of which corresponds with a user listed in the field value and contains an SPUser object property for them as well.

     

    Here's a code example for the SPFieldUserValueCollection:

    Code Snippet

    SPFieldUserValueCollection spuvc = new SPFieldUserValueCollection(SPContext.Current.Web, item["MyColumn"].ToString());

    foreach (SPFieldUserValue userVal in spuvc)

    {

    SPUser user = userVal.User;

    string userName = user.Name;

    string loginName = user.LoginName;

    string email = user.Email;

    }

     

     

    The only confusing bit is that the documentation on MSDN is a little lite and doesn't explain which SPWeb to use.  The above code uses SPContext.Current.Web, but you might want to use SPSecurity.RunWithElevatedPrivileges() to get by any security validation errors.  You might also need to play with the SPWeb object used in the constructor because it could be the SPContext.Current.Site.RootWeb you need.

     

    -Rich

    Thursday, January 24, 2008 5:53 PM

All replies

  • I usually use a quick-and-dirty Split method (which gives you an array of individual values). But Mark Arend has a nicer solution; using regular expressions. Read more details on his blog:

    http://blogs.msdn.com/markarend/archive/2007/05/29/parsing-multi-value-fields-multichoice-lookup-user-url-rules-for-the-delimiter.aspx

     

    Hth,
    Jan

     

    Thursday, January 24, 2008 7:44 AM
  • You're seeing the ;# delimiter because this is how SharePoint stores a collection of values in an SPField when the SPField type you're using inherits from the SPFieldLookup object.  In your case, you've got a User field that allows multiple values.

     

    Most of the SPFieldLookup types have an associates SPFieldLookupValue type to aid in the retrieval of values from the field, and alleviates the need to do any string or regular expression parsing which can be inefficient when obtaining values from an SPField in SharePoint. To really make what you're trying to do easy, you can construct an SPFieldUserValueCollection object using the value of your field.  This object is a Generic List of SPFieldUserValue objects, each of which corresponds with a user listed in the field value and contains an SPUser object property for them as well.

     

    Here's a code example for the SPFieldUserValueCollection:

    Code Snippet

    SPFieldUserValueCollection spuvc = new SPFieldUserValueCollection(SPContext.Current.Web, item["MyColumn"].ToString());

    foreach (SPFieldUserValue userVal in spuvc)

    {

    SPUser user = userVal.User;

    string userName = user.Name;

    string loginName = user.LoginName;

    string email = user.Email;

    }

     

     

    The only confusing bit is that the documentation on MSDN is a little lite and doesn't explain which SPWeb to use.  The above code uses SPContext.Current.Web, but you might want to use SPSecurity.RunWithElevatedPrivileges() to get by any security validation errors.  You might also need to play with the SPWeb object used in the constructor because it could be the SPContext.Current.Site.RootWeb you need.

     

    -Rich

    Thursday, January 24, 2008 5:53 PM