Few common mistakes @ SPList and SPListItem objects

We can assume many things in life, but not everything match with our assumption. Similar story with SPList.Fields properties. Fields is a properties of the SPList objects which actually returns the SPFieldCollection object which also basically inherited  from SPBaseCollection.
From .NET perspective, we can use indexer or name to get the actual Field object from the collection of object. So let’s say we have a SharePoint list name CLIENT and there are column fields client and Title.

Now, let’s say we want to check the name of the client with the passing value in the list while updating the list. In other words, we have a variable name Client which holds the static value, which we need to check while iterating within the list. Below the simple code to get the same:

String ClientName;

SPList _ClientList = _SPCurWeb.Lists[“CLIENT”];

foreach (SPItem _ListItem in _ClientList.Items)

{

if (_ListItem["client"].ToString() == Client)

{

ClientName = _ListItem["client"].ToString();

break;

}

}

Sometimes we may found that the above mentioned code is not working perfectly. This is because we are comparing the _ListItem["Title"].ToString() i.e. the ListItemValue with static variable Client. It is expected to work, but actually it didn’t. Why?

If you debug the code, there would be high possibilities to be found that the value _ListItem["Title"].ToString() is combination of internal ID along with # and the field value. This is very similar to edit the concatenated filed value in DataSheet view in MOSS.

ListItemFieldDataValue

ListItemFieldDataValue

So, may be the approach could be check whether the value is exist within the field value or not. But how? There is one method name Contains() method in the string object and using this method we can achieve the same

String ClientName;

SPList _ClientList = _SPCurWeb.Lists[“CLIENT”];

foreach (SPItem _ListItem in _ClientList.Items)

{

if if (Client.Contains(_ListItem["client"].ToString()))

{

ClientName = _ListItem["client"].ToString();

break;

}

}

So, once we use this Contains() method, the thing is getting easier to us.

Few more words :) . Regarding the indexer in the SPItem object in SharePoint, it doesn’t allow value properties to get the individual field value. Here is an example to get the ListItem value of the FieldName EntryType:

String EntryType;

EntryType     =    _ListItem["EntryType"].value;   >> Invalid Statement
EntryType     =    _ListItem["EntryType"].ToString();  Seems to be correct, however sometimes can trouble you
EntryType     =    _ListItem["EntryType"];   >> Will return you the object and you need to transform it to the filed value type object (i.e. string(_ListItem["EntryType"]))

Similarly, if you want to update any list item, you need to follow the similar approach.

SPListItem _NewItem = _SPList.Items.Add();

_NewItem["Title"] = “Test”;

_NewItem["RefID"] = RefItemID;

_NewItem["PatientName"] = PatientName;

_NewItem.Update();

Please note, you can’t use to set value like _NewItem["Title"].value = “Test” which is invalid and you may get the following errors:

Error1:    Property or indexer ‘Microsoft.SharePoint.SPFieldCollection.this[string]‘ cannot be assigned to…
Error2: Cannot implicitly convert type ‘string’ to Microsoft.SharePoint.SPField’

And the last point is to use the update() method of the SPListItem object. Executing this method, basically update the item in the SharePoint list.

No comments yet.
You must be logged in to post a comment.