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

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
share|improve this question
    
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
add comment

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer
add comment

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!

share|improve this answer
    
Thanks Norbert. Just tried your new code, but still doesn't work. Still a blank textbox! –  finisterre Dec 13 '12 at 15:39
    
Would you like to continue the debugging on chat? –  Norbert Dec 13 '12 at 15:44
    
If you have time. I've not done a chat before though, so can you initiate it. Thanks. –  finisterre Dec 13 '12 at 15:49
    
Just click here: chat.stackexchange.com/rooms/6639/discussion-with-kriistiian –  Norbert Dec 13 '12 at 15:51
    
Still looking for an answer, if anyone can help? –  finisterre Dec 13 '12 at 16:51
add comment

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.