Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

Thanks,

JT

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()
            InitializeComponent()
        End Sub

        Private Sub onQuerySucceeded(ByVal sender As Object, ByVal args As ClientRequestSucceededEventArgs)
            MessageBox.Show("Request Succeeded")
            Dim updateUI As UpdateUIMethod = AddressOf DisplayInfo
            Me.Dispatcher.BeginInvoke(updateUI)
        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.Load(collListItem)

            clientContext.ExecuteQueryAsync(AddressOf onQuerySucceeded, AddressOf onQueryFailed)
        End Sub
    End Class
End Namespace
share|improve this question
    
What have you tried? –  RJ Cuthbertson Oct 15 '12 at 3:30
    
I've tried everything. Have just added my latest code in the original message. Help! –  finisterre Oct 15 '12 at 9:24
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
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
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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