2
votes

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?

7

7 Answers

4
votes

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.

3
votes

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!)

1
votes

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...

0
votes

Do you run SPListItem.Update() or .SystemUpdate() after setting the properties?

0
votes

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());
0
votes

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"

-1
votes
 SPList yourList = web.Lists["Your list name"];
 string sColumnValue = oSPListItem[yourList.Fields["yourSiteColumn display   
  name"].InternalName].ToString();