Friday, June 13, 2008

Working with data from different SharePoint Fields

Many times we spend lots of time in small tasks like understanding the format in which SharePoint data is stored internally when using object model to retrieve/store data in SharePoint lists. So thought of getting all in one place.



Many of the field types like text, Number etc are straightforward to get/set.
A cleaner and professional way of working with the data from Fields would be to make use of Field Value Classes provided.

Following Field Value classes are available:
SPFieldURLValue
SPFieldLookupValue
SPFieldUserValue
SPFieldMultiChoiceValue
SPFieldRatingscaleValue
SPFieldMultiColumnValue


Ex:
1) To retrieve value from URL field :

SPListItem linkItem = SPContext.Current.Web.Lists["Links"].Items[0];
SPFieldUrlValue val = new SPFieldUrlValue(linkItem[SPBuiltInFieldId.URL].ToString());
string linkURL = val.Url;
string linkText = val.Description;

2) To Save value in URL field :

string linkURL = "http://www.samplesite.com/sampleweb";
string linkText = "SampleWebsite";
SPFieldUrlValue val = new SPFieldUrlValue();
val.Description = linkText;
val.Url = linkURL;
SPListItem linkItem = SPContext.Current.Web.Lists["Links"].Items.Add();
linkItem[SPBuiltInFieldId.URL] = val;
linkItem.Update();

Following are some more references on this :
http://spdevguy.com/blogs/spdev/archive/2007/11/16/reading-field-data-with-sharepoint-s-field-value-classes.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldurlvalue.aspx

2 comments:

Fabian Carter June 15, 2012 at 4:54 AM  

It was great to know about these SharePoint fields and even it was much better to get to work with it. I am gaining lot to SharePoint from this.

seoinheritx August 22, 2012 at 11:52 PM  

Thanks a lot for that fantastically amazing post!

Sharepoint Development | Sharepoint Developers