Friday, December 12, 2008

SharePoint SPListItem Update (object model)

Hi,
I found something interesting and it took long time for me to figer out the problem which i am trying to solve from 1 day or more.

for example you have an document library and a word document in it, you have written an event handler which will triger when u do check in for an document.
the event handler should update some specific column which is there in your document.
the senario could be:
columns to be updated:
Column1
Column2
Column3

It happes to so that you do not have all the columns exits for document all the time, but still you need to validate that if those columns exists then do some operation.
how you will check weather thoese columns are null or they have value:
simple as below:

SPListItem item (Assume that you have got an document object in the item of SPList variable).

if((item["Column1"] != null) && (item["Column1"].toString()!=""))
//Then do as below.

Yes you got this correct, but this does not work all the time. so the trick is below:

if (item.Fields.ContainsField("Allekirjoittaja") == true)
{
WriteDebugLog("Field is available");
}

If you r trying to check the field which is not in the document then it will throw and exception of item.SystemUpdate(false);
the error could be as Error is updating the item fields or something like that.
Happy coding.