15 Comments on "Properly Populating and Retrieving SharePoint Field Data"

  1. chris says:

    Hi this post is great!
    However, is it possible to post soemthing on SPFieldUserMulti.

    I wish to extract all the users from a multiselect Person field and give them permissions in an event handler.

    Any ideas??Thank you

  2. Rogério Cruzeiro says:

    Hi, thanks for the heads up on this matter.

    Following you method I’m getting this error “Value does not fall within the expected range” when I make this assignment:

    listItem["ItemName"] = User;
    //user is an SPUser obj

    I filled the User variable with the steps that you provide and debugging it on VS2005 I can see that the value in it is correct. Something like {SPVM\bob}

    Any help available?

    Thanks,
    Roger

  3. Dimitrie Agafitei says:

    SPFieldMultiChoiceValue does not implement an iterator so you can not use a foreach loop to retrieve individual values.

  4. Cory says:

    Thanks for the comments guys.

    Rogerio, usually you will see that error when your field name you’re looking for doesn’t exist in the collection. In your case “ItemName” might not be a valid column on your list.

    Dimitrie, you’re absolutely right and I’ve updated the code to reflect this issue.

  5. Carrla says:

    I create the class Lookup to validate the fields of Lookup and insert them in SharePoint.
    When I create a document, and call this class gives me the following error to save the fields Lookup …
    ERROR:
    Value does not fall within the expected range

    Class Lookup:

    public class WPLookUP : Indice
    {
    DropDownList dropDownList;
    SPFieldLookup lookup;

    public WPLookUP(SPFieldLookup l, string defaultValue)
    : base(l)
    {

    lookup = l;
    dropDownList = new DropDownList();
    bool exist = false;
    SPList list;
    defaultValue = “Escolha uma opção”;
    dropDownList.Items.Add(new ListItem(string.Empty, string.Empty));

    using (SPSite site = new SPSite(WebSettings.SiteUrl + WebSettings.DCSite))
    {
    using (SPWeb web = site.OpenWeb())
    {
    list = web.Lists.GetList(new Guid(lookup.LookupList), false);

    SPListItemCollection items = list.Items;

    foreach (SPListItem spitem in items)
    {

    ListItem item = new ListItem();

    string value = (string)spitem[lookup.Title];

    SPFieldLookupValue lookupValue = (SPFieldLookupValue)lookup.GetFieldValue(value);-> Error

    item.Text = lookupValue.LookupValue;

    item.Value = lookupValue.LookupValue;

    item.Attributes.Add(“LookupId”, lookupValue.LookupId.ToString());

    if (defaultValue != null)
    {
    if (defaultValue.Equals(spitem[lookup.LookupField].ToString()))
    {
    item.Selected = true;
    exist = true;
    }
    }
    dropDownList.Items.Add(item);

    }
    }
    }

    }
    Help me please…

  6. Hi, I hope you can help. I need to populate a SPUserField control that is not part of a list, it will be displayeed on a custom web part. How can I tell the SPUserFIeld which user I want it to display?

    thanks!

  7. Robert Graauw says:

    The Value is not within expected range error is because the method expects an ID if you know the ID then you can fill it in yourself like Blogposter did. But if your ideas are created dynamically or you have hundreds of items in your lookup field then that could be a problem.

    Check out the link below for a helper class to retrieve a an ID from a specific lookup list item. My lookup column is working just great now thanks to corey and the guy below. You gotta love the sharepoint community.

    http://blogs.sharepointguys.com/matt/sharepoint-2007-development/splookupfield-helper-function/

  8. Robert Graauw says:

    sorry its cory, my bad

  9. Robert Graauw says:

    ideas = ID’s

    !@#%^&*

  10. Elijah says:

    Thanks, Worked Perfectly.
    I’m not sure why it doesn’t implement IEnumerable

  11. jp says:

    Thank you for the most needed information. I am trying to do some SPQuery and I think my CAML might be crashing due to some of the lookup fields coming back with raw formatting? Can your technique be used for that?

  12. jp says:

    Can this technique be used when running field compares in SPquery? Like in a contains or eq?

    I am thinking CAML might not match the values due to the SP formatting junk?

    ” + “srch1, srch2″ + “

  13. jp says:

    Thanks so much for summarizing the classes for the raw data types and the great examples!!! I had been searching for how to handle these.

  14. Harsha says:

    Awesome.. simply good..

Trackbacks for this post

  1. sp wf « Dev42 and Friends

Got something to say? Go for it!