0

Using Silverlight 5, VB.Net and SharePoint Online.

Tried this to read the current user's name into a textbox, but the textbox remains blank.

Any ideas? Thanks.

Imports Microsoft.SharePoint.Client

Partial Public Class MainPage
    Inherits UserControl

    Public oUser As String

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub onQuerySucceeded(ByVal sender As Object, ByVal args As ClientRequestSucceededEventArgs)
        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("SharePoint Query Failed" & args.Message & vbLf & args.StackTrace)
    End Sub

    Private Sub DisplayInfo()

    End Sub

    Private Delegate Sub UpdateUIMethod()

    Private Sub UserControl_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Call GetSPUser()
        TextBox1.Text = oUser
    End Sub

    Private Sub GetSPUser()
        Dim clientContext As ClientContext = clientContext.Current
        Dim oUser As User = clientContext.Web.CurrentUser

        clientContext.Load(oUser)
       clientContext.ExecuteQueryAsync(AddressOf onQuerySucceeded, AddressOf onQueryFailed)
    End Sub
End Class
  • Is this a copy-paste of your code? The Dim clientContext As ClientContext = clientContext.Current line should be Dim clientContext As ClientContext = ClientContext.Current in my opinion. – Norbert Dec 13 '12 at 14:16
  • Thanks Norbert. But that code is cut and pasted form another application that works. So I don't think it's that. – finisterre Dec 13 '12 at 15:32
2

OK. Got it working:

Imports Microsoft.SharePoint.Client

Partial Public Class MainPage
    Inherits UserControl

    Public Property oUser As User
    Public Property oWeb As Web


    Public Sub New()
        InitializeComponent()
    End Sub


    Private Sub onQuerySucceeded(ByVal sender As Object, ByVal args As ClientRequestSucceededEventArgs)
        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("SharePoint Query Failed" & args.Message & vbLf & args.StackTrace)
    End Sub

    Private Sub DisplayInfo()
        TextBox1.Text = oUser.LoginName
        TextBox2.Text = oUser.Title
        TextBox3.Text = oUser.Email
        TextBox4.Text = oUser.Id
        TextBox5.Text = oUser.PrincipalType
        TextBox6.Text = oUser.Tag
        TextBox7.Text = oUser.ServerObjectIsNull
    End Sub

    Private Delegate Sub UpdateUIMethod()

    Private Sub UserControl_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Call GetSPUser()
    End Sub

    Private Sub GetSPUser()
        Dim clientContext As ClientContext = clientContext.Current
        oWeb = clientContext.Web
        oUser = oWeb.CurrentUser
        clientContext.Load(oUser)
        clientContext.ExecuteQueryAsync(AddressOf onQuerySucceeded, AddressOf onQueryFailed)
    End Sub
End Class
0

I have never worked with Client Object Model from Silverlight, but I can see a general mistake in your code:

You have the oUser object declared twice. First, as a member of the MainPage class, second as a local variable in the GetSPUser method. In the GetSPUser method you retrieve the current user information into the local variable. In the UserControl_Loaded method you call the GetSPUser method, but then set the textbox with the value from the "global" variable, the one from the class level. That one has never been initialized, so it's always empty string.

Your code should be:

Imports Microsoft.SharePoint.Client

Partial Public Class MainPage
    Inherits UserControl

    Public oUser As User

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub onQuerySucceeded(ByVal sender As Object, ByVal args As ClientRequestSucceededEventArgs)
        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("SharePoint Query Failed" & args.Message & vbLf & args.StackTrace)
    End Sub

    Private Sub DisplayInfo()

    End Sub

    Private Delegate Sub UpdateUIMethod()

    Private Sub UserControl_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Call GetSPUser()
        TextBox1.Text = oUser.LoginName
    End Sub

    Private Sub GetSPUser()
        Dim clientContext As ClientContext = clientContext.Current
        oUser = clientContext.Web.CurrentUser

        clientContext.Load(oUser)
       clientContext.ExecuteQueryAsync(AddressOf onQuerySucceeded, AddressOf onQueryFailed)
    End Sub
End Class

Please tell me if it worked.

Cheers!

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.