SharePoint List Error: "Value does not fall within the expected range" - sharepoint

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?

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].ToString();
The GetField method looks for a field by both DisplayName & InternalName.

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

public static string GetItemURLValue(SPListItem item, string fieldName)
{
string exit = "";
SPFieldUrlValue link = new SPFieldUrlValue(item[fieldName].ToString());
exit = link.Url;
return exit;
}

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.

There is a special method for retrieving URLs. Try this:
SPListItem li = ...
SPFieldUrlValue fuv = new SPFieldUrlValue(li[strFieldName].ToString());
return fuv.Url;

Mine is a Windows application. I used to get this exception after I created the set up and tried deploying.
My application needed to write in Excel and then save it. I used reference of COM component 'Microsoft Excel 11.0 Object'. I noticed when I add this reference actually 3 dll's appear in my reference list.
Microsoft.office.core
Excel
VBIDE
I removed the 'VBIDE' reference and my problem is solved.

If it's just a column name and in "Single line of Text" format, what about:
item["URL"] != null ? item["URL"].ToString() : "Not Found";

Related

Read data in content type via CSOM

I have an issue with CSOM and content type, I used CSOM to read some value in a sharepoint list, it works well until I try to used content type If I create a new content type with my fields, it's worked but if I add my field in an existing content type. It doesn't work and that's return me the following error:
Error: Object reference not set to an instance of an object
Here a piece of code from where my error is comming:
List list = web.Lists.GetByTitle("myList");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
ListItemCollection items = list.GetItems(query);
l_objCtx.Load(items, its => its.Include(item => item[ColumnName], item => item.Id));
l_objCtx.ExecuteQuery();
foreach (var item in items)
{
m_strGetprimary = item.Id.ToString();
return true;
}
I've done a lot of researches but I found nothing related to this issue
Any idea of what I'm doing wrong?
Thank you for your help
After adding SharePoint field to existing content type please check field name that you have just added. Sometimes it can be different and then your variable ColumnName might have wrong text value.
How to check it:
Go to site settings > Site content types > Your content type.
Select/click your field and in URL you will have param Field={field name}.

Kentico Document Get Page Meta Data Custom Page Type

When trying to retrieve DocumentPageTitle and DocumentPageDescription using GetStringValue() on a custom page type TreeNode, the result is always coming back as the default value (in this case an empty string) passed into the method.
I'm able to successfully retrieve other column values as well as standard document properties such as DocumentName, DocumentID and AbsoluteURL, but not the document meta properties.
The respective fields in the Meta tab of document/page do have values and are being successfully rendered in the by default such as <meta name="description" content=".." />
// returns empty string
string documentPageDescription = DocumentContext.CurrentDocument.GetString("DocumentPageDescription", string.Empty);
// returns empty string
TreeNode document = parameters[0] as TreeNode;
string documentPageDescription = document.GetStringValue("DocumentPageDescription", string.Empty);
I've tried setting option Inherits fields from page type to "Page (menu item)", but that did not help.
Does the custom page type need to inherit from something specifically or have a specific setting activated to access these values? Or if what I think is a TreeNode in fact isn't, how could I get the TreeNode from this object that has the properties listed before available?
Thank you for any help you can provide.
ValidationHelper.GetString(CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("DocumentPageDescription"), string.Empty)
Two things to check, one, are you sure the meta data is available on the page you are pulling? Two, is your API actually pulling all the data for that page?
I've used these in my test and both returned the metadata.
var page = DocumentHelper.GetDocuments().Path("/Articles/Coffee-Beverages-Explained").FirstObject;
Response.Write(page.GetStringValue("DocumentPageDescription", string.Empty));
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
TreeNode tn = tree.SelectNodes().OnCurrentSite().Path("/Articles/Coffee-Beverages-Explained").FirstObject;
Response.Write(tn.GetStringValue("DocumentPageDescription", string.Empty));
The DocumentPageTitle and DocumentPageDescription were coming back as null when the custom page type document/page was inheriting from parent/global values.
I was able to use the following to get the properties when not inheriting, while falling back to the parent value when inheriting was taking place:
string documentPageTitle = document.GetStringValue("DocumentPageTitle", DocumentContext.CurrentTitle);
This approach came from the following issue on Kentico DevNet.
Thank you for your help and suggestions, it's appreciated.

Sharepoint + Value does not fall within the expected range + SPListItem

Looks like other people have had this issue, but I would like to understand how to target a column if there are spaces in a list column name when using the code below. Here is what I have:
SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["Project Info"];
SPListItemCollection collItem = oList.GetItems("Project Description");
if (collItem.Count > 0)
{
foreach (SPListItem oItem in collItem)
{
description = oItem["Project Description"].ToString();
}
}
If I create the list field "Project Description" with underscores for the column name (Project_Description), then it works just fine, but with the code above I get an error: Value does not fall within the expected range
Looks like there is something with the display vs internal name of the column. Can someone help me with this? Thanks.
Try "Project_x0020_Description". AFAIK this works when using the web services. Not sure whether it also helps in your case.
Here is some more information: http://www.davehunter.co.uk/Blog/Lists/Posts/Post.aspx?ID=95
Use Always the URl of the List, because URL's dont give problems like that ;)

SharePoint: How do you reference a localized list?

I have a SharePoint list that gets installed by a feature.
I need to enable content types which I do so in a feature receiver:
SPList list = site.Lists["NameOfList"];
list.ContentTypesEnabled = true;
list.Update();
The problem is that it seems the method:site.Lists["NameOfList"], is really looking at the Title not the ID.
How can I get the list by using the ID and NOT the title which is subject to change because of Localization...
<ListInstance
TemplateType="10051"
Id="ThisISTheIDField" //Want to retrieve list instance based on this field.
Title="$Resources:MyFile,MyResourceName;"
Url="Lists/URLOFList"
OnQuickLaunch="TRUE">
</ListInstance>
Thanks in advance. And 10 years of hail Maries for the MS SharePoint developer who wrote this internal dare I say - c.r.a.p?
So your list is a custom list, right?
You can add:
EnableContentTypes=TRUE
To your list definition and you won't need the feature?
See: List Element
There is no site.Lists (Maybe is a SPWeb? The naming is awkward, I Know)
<SPWeb Instance>.Lists.GetList(Guid uniqueId, bool bFetchMetadata)
<SPWeb Instance>.Lists.GetList(Guid uniqueId, bool bFetchMetadata, bool bFetchSecurityData)
<SPWeb Instance>.GetList(string strUrl)
The third option could combine the url and use the internal name to grab it (in the URL lists are accessed by the internal name)
If the operation has to be completed only once in a lifetime, I'd suggest looping throuhg the .Lists collection and checking if the baseTemplate matches:
For Each oList In web.Lists
If (list1.BaseTemplate = 10051) Then
oList.ContentTypesEnabled=True
End If
Next

Accessing SPLIstItem properties in SharePoint

I'm trying something very simple - accessing my SharePoint list's items and their properties.
However, SPListItem.Properties count is zero for all normal lists. Everything works as expected for document and pages libraries. So, if the list items are based on document type, all is good. If they are based on item, properties are not returned.
I've tried in two environments, with new sites created from OOTB publishing templates, with new lists which are based on OOTB content types etc. Always the same thing.
Right amount of SPListItems is always returned. Title and Name are fine. It's just that the .Properties hashtable is totally empty.
In desperation, I wrote a web part that outputs the following (ugly!) diagnostics.
foreach (SPList list in SPContext.Current.Web.Lists)
{
foreach (SPListItem item in list.Items)
{
Label label = new Label();
label.Text = "Name: " + item.Name + "Property count: " + item.Properties.Count;
this.Controls.Add(label);
}
}
The only observation is that it works exactly as I described earlier. I just share the code to show that this is the most basic operation imaginable.
Here is sample output - I've added the line breaks for readability ;-)
Name: Test Property count: 0
Name: default.aspx Property count: 21
Obviously item "Test" is an item based list item and default.aspx is a page.
Has anyone ever encountered anything like this? Any ideas?
item["FieldName"] is the canonical way to get a value of a metadata column in a SharePoint list. If you need to get the list of available fields for a SharePoint list, check the parent SPList object's Fields property which is a collection of the fields in this list. Each of those field objects will have a property called InternalName and that is what you should use to access its value when you are working with an instance of SPListItem.
Are you trying to get the field Values? Sadly, they are not strongly typed:
string ModifiedBy = (string)item["Author"];
To get the proper names of the fields (they have to be the internal names), go to the List and then List Settings. You will find the List of Columns there. Click on any Column Name to go to the Edit Page, and then look at the URL in the Address Bar of your Browser. At the very end, there should be a parameter "Field=Editor" or similar - that's your internal field name.
If you wonder why a field like "Test Field" looks strange, that is because Sharepoint encodes certain characters. A Space would be encoded to x0020 hence the Internal Name for "Test Field" is "Test_x0020_Field".
In order to get the proper field type to cast to:
string FieldType = item["Author"].GetType().FullName;
(The Intermediate Window in Visual Studio is tremendously helpful for this!)
I have found the following extension to the SPListItem class to be very helpful.
internal static class SharePointExtensions
{
internal static Dictionary<string, object> PropertiesToDictionary(this SPListItem item)
{
// NOTE: This code might be temping - but don't do it! ...itdoes not work as there are often multiple field definitions with the same Title!
// return item.Fields.Cast<SPField>().ToDictionary(fld => fld.Title, fld => item[fld.Title]);
Dictionary<string, object> dict = new Dictionary<string, object>();
var fieldNames = item.Fields.Cast<SPField>().Select(fld => fld.Title).Distinct().OrderBy(sz => sz).ToArray();
foreach (fieldName in fieldNames)
dict.Add(sz, item[fieldName]);
return dict;
}
}
with it you can simply do:
item.PropertiesToDictionary()
...and you will have a nice Linq dictionary object that will work just the way you'd expect. Is it a little heavy-weight / low-performance? Sure. Are most people willing to make that trade-off? I think so...
Do you run SPListItem.Update() or .SystemUpdate() after setting the properties?
If you want to get an object out of a SPField of a SPListItem you've got to do like this:
SPField field = ((SPList)list).Fields.GetField("FieldName");
object fieldValue = field.GetFieldValue(((SPListItem)item)[field.Title].ToString());
The ListItem.Properties hashtable will be empty unless you assign to it.
SPListItem item = properties.ListItem;
item.Properties["Key"] = "value";
int total = item.Properties.Count;
Answer:
"total == 1"
SPList yourList = web.Lists["Your list name"];
string sColumnValue = oSPListItem[yourList.Fields["yourSiteColumn display
name"].InternalName].ToString();

Resources