Click here to Skip to main content
15,867,986 members
Articles / Productivity Apps and Services / Sharepoint
Tip/Trick

SharePoint - Reading values from items in a list

Rate me:
Please Sign up or sign in to vote.
3.33/5 (2 votes)
30 Apr 2010CPOL1 min read 29K   1  
Extracting a value from a list item in a SharePoint site is frequently necessary, and it comes with a slight twist that some programmers may not be expecting. So, this is an example of a method that works well for most purposes. First, lets identify the site that holds our SharePoint...
Extracting a value from a list item in a SharePoint site is frequently necessary, and it comes with a slight twist that some programmers may not be expecting.

So, this is an example of a method that works well for most purposes. First, lets identify the site that holds our SharePoint list...
Dim site As SPSite = SPContext.Current.Site
Dim url As String = SPContext.Current.Site.Url
Dim spWebSite As SPWeb = Site.OpenWeb("/")
Dim myList As Microsoft.SharePoint.SPList


Next, lets identify the list that we want to read from, and create a list item collection to hold it in memory.
myList = spWebSite.Lists("Employee Information")
Dim myItemCollection As SPListItemCollection
myItemCollection = myList.Items


Now, lets instantiate a lookup so that we can use it to pull the value from our specified list item.
Dim xValLookup As SPFieldLookupValue


Lets also create an arraylist to hold the values we extract. In this example, I created an arraylist to hold the values, and an arraylist to hold all the list items. On each loop, we will create a new instance of xTransaction, add items to it, then add xTransaction to our instance of transactionArry. At the end, we will have an arrylist with several arraylists in it.

Dim transactionArray As ArrayList = New ArrayList
Dim xTransaction As ArrayList


Now lets loop once for each item in the SharePoint list, and use the .LookupValue method to get to the info we need.

For Each xItem As SPListItem In myItemCollection
            xTransaction = New ArrayList
            xValLookup = New SPFieldLookupValue(xItem("Employee").ToString)
            xTransaction.Add(xValLookup.LookupValue)
            xValLookup = New SPFieldLookupValue(xItem("Transaction Type").ToString)
            xTransaction.Add(xValLookup.LookupValue)
            xValLookup = New SPFieldLookupValue(xItem("Transaction Time").ToString)
            xTransaction.Add(xValLookup.LookupValue)
            xValLookup = New SPFieldLookupValue(xItem("Time Zone").ToString)
            xTransaction.Add(xValLookup.LookupValue)
            xValLookup = New SPFieldLookupValue(xItem("Corrected Time").ToString)
            xTransaction.Add(xValLookup.LookupValue)
            xValLookup = New SPFieldLookupValue(xItem("Correction Remarks").ToString)
            xTransaction.Add(xValLookup.LookupValue) 
            transactionArray.Add(xTransaction)           
        Next


Now we can use the arraylist with the extracted values for whatever we need. Note that in the example, I specified the colum names to get the values from, but you can also specify a numeric value as the index if that works better for your objective. Hope this helps!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Jacobs Technology
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --