I'm using VB.Net 4.0/Silverlight 5 and the SharePoint Client Object Model. I have SharePoint Foundation 2010 running on localhost.

I've been trying for days to read some values from a list, with no success.

I have a List called 'pfa'. The list has 3 columns; FullName, JobTitle and PhoneNumber.

I just need some sample code to read the items from the list and display them in a textbox.

Can anyone help? My latest code is below. And, this is just attempting to read the Id: field for now.



Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client

Namespace Jason
    Partial Public Class MainPage
        Inherits UserControl
        Private oWebsite As Web
        Private collList As ListCollection
        Private collListItem As ListItemCollection
        Private listInfo As IEnumerable(Of List)

        Public Sub New()
        End Sub

        Private Sub onQuerySucceeded(ByVal sender As Object, ByVal args As ClientRequestSucceededEventArgs)
            MessageBox.Show("Request Succeeded")
            Dim updateUI As UpdateUIMethod = AddressOf DisplayInfo
        End Sub

        Private Sub onQueryFailed(ByVal sender As Object, ByVal args As ClientRequestFailedEventArgs)
            MessageBox.Show("Request failed. " & args.Message & vbLf & args.StackTrace)
        End Sub

        Private Sub DisplayInfo()
            Dim oListItem As SP.ListItem
            For Each oListItem In collListItem
                MyOutput.Text += vbLf & "Id: " & oListItem.Id
            Next oListItem
        End Sub

        Private Delegate Sub UpdateUIMethod()

        Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
            Dim clientContext As ClientContext = clientContext.Current
            Dim oList As SP.List = clientContext.Web.Lists.GetByTitle("pfa")
            oWebsite = clientContext.Web
            Dim _Query = SP.CamlQuery.CreateAllItemsQuery
            Dim collListItem As ListItemCollection = oList.GetItems(_Query)


            clientContext.ExecuteQueryAsync(AddressOf onQuerySucceeded, AddressOf onQueryFailed)
        End Sub
    End Class
End Namespace
  • I've tried everything. Have just added my latest code in the original message. Help! – finisterre Oct 15 '12 at 9:24

Let's start with the fact that you have two separate variables named collListItem. The global one is never used. The collListItem in the scope of the button click event handler is created and loaded, but the variable goes out of scope outside of the click handler, and is reclaimed by the garbage collector.

First, change this:

Dim collListItem As ListItemCollection = oList.GetItems(_Query)

To this:

collListItem = oList.GetItems(_Query)

Now you're loading the data into the global variable rather than the local variable.

  • rjcup - thank you so much. I've been struggling with this for days. I think I've gone code blind by staring at it for so long. I tried your change, and it works. I could kick myself! Thanks again. – finisterre Oct 15 '12 at 14:32
  • Sometimes you just need a fresh set of eyes. HTH. – RJ Cuthbertson Oct 15 '12 at 14:36
  • Just be mindful to clean up your disposable objects that you're declaring globally (IE: Private oWebsite As Web). If SharePoint reclaims it, it writes to the ULS logs and then you'll have an admin to deal with. – RJ Cuthbertson Oct 15 '12 at 14:37
  • Is there a tutorial I can read anywhere on cleaning up disposable objects? I've not been doing this so far! – finisterre Oct 15 '12 at 19:29
  • Check out this link: msdn.microsoft.com/en-us/library/aa973248.aspx This is the disposable objects best practices. – RJ Cuthbertson Oct 15 '12 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.