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:

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 = "";
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;

Following are some more references on this :


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