Click here to Skip to main content
Click here to Skip to main content

Tagged as

SharePoint - Reading values from items in a list

, 30 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

marc9889
Software Developer (Senior) Jacobs Technology
United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 30 Apr 2010
Article Copyright 2010 by marc9889
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid