views:

9270

answers:

6

Hi I am developing using the SharePoint namespace and I ran into the following error when I try to retrieve a URL column from one of my lsits.

 "Value does not fall within the expected range"

All I am doing is:

item["URL"]

Can someone tell me what I can do about this?

A: 

Did you misspell it? or use the wrong case?

OTisler
sometimes there is an internal name which doesn't match the field name. You might have to reference that internal name instead
OTisler
I checked the internal name it is URL as well...
Oliver S
hmm, how are you retrieving the list? Is there a larger snippet you can include?
OTisler
A: 

This usually means "URL" is not a field in the list.

If it's a promoted InfoPath column, try deactivating and re-activating the form template to the site. I have noticed that I have to do this whenever I add a new promoted field to an infopath template.

Tundey
It is in the list...
Oliver S
A: 

To get the URL of an SPListItem, use Item.Url.

Andy Mikula
Not the URL I am looking for, I am looking for a column URL I created.
Oliver S
If it's a promoted InfoPath column, try deactivating and re-activating the form template to the site. I have noticed that I have to do this whenever I add a new promoted field to an infopath template.
Tundey
+3  A: 

The error definitely means that the field can't be found.

Debug the process and look at the ListItem.Fields.SchemaXML property to find its internal name, it may be saved internally as something other than URL. You can also use the following method to get a list item value.

SPField l_field = l_item.Fields.GetField("URL");
string l_fieldValue = l_item[l_field.Id].ToSting();

The GetField method looks for a field by both DisplayName & InternalName.

Jason
I recommend SharePoint manager for looking at the schema xml and other fun stuff in SharePoint
Nat
+1  A: 
    public static string GetItemURLValue(SPListItem item, string fieldName)
    {
        string exit = "";
        SPFieldUrlValue link = new SPFieldUrlValue(item[fieldName].ToString());
        exit = link.Url;
        return exit;
    }
ryan
A: 

There is a special method for retrieving URLs. Try this:

SPListItem li = ...
SPFieldUrlValue fuv = new SPFieldUrlValue(li[strFieldName].ToString());
return fuv.Url;
strongopinions
Sorry, I see that ryan has posted a similar solution.
strongopinions