-
Dec 7th, 2022, 02:26 PM
#1
Thread Starter
New Member
connecting to SharePoint Online getting Object reference instance object error
I want to upload files from the user's PC to our Sharepoint Online site (the one in Office 365, not on premises), and I found some sample c# code i've translated into VB.net to do the job.
When I try it, during the connect phase I get an error: Object reference not set to an instance of an object for this line:
SPClientContext.Credentials = New SharePointOnlineCredentials(SPUserLogin, securePassword)
debugging and walk through shows the securePpassword has a value and the SPUserLogin value is right. but the SPClientContext.Credentials value is "nothing." So I'm guessing that's where the issue is.
I'm not sure why my Credentials are not getting a value. Can someone help me troubleshoot? I've bolded the line where the error hits, if that helps.
Code:
Imports Microsoft.SharePoint.Client
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Security
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class fileuploadtest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Property SPClientContext As ClientContext
Get
End Get
Set(value As ClientContext)
End Set
End Property
Public Property SPWeb As Web
Get
End Get
Set(value As Web)
End Set
End Property
Public Property SPErrorMsg As String
Get
End Get
Set
End Set
End Property
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
Dim sURL As String = txtSharePointURL.Text
Dim sDocName As String = String.Empty
'Dim securePassword As SecureString = New SecureString
If (Not String.IsNullOrWhiteSpace(sURL) _
AndAlso (Not String.IsNullOrWhiteSpace(txtLibraryName.Text) _
AndAlso Me.FileUpload1.HasFile)) Then
Dim bbConnected As Boolean = Connect(sURL, "xxxxx@xxxxx.xxx", "XXXXXXXXX")
If bbConnected Then
Dim uri As Uri = New Uri(sURL)
Dim sSPSiteRelativeURL As String = uri.AbsolutePath
sDocName = UploadFile(FileUpload1.FileContent, FileUpload1.FileName, sSPSiteRelativeURL, txtLibraryName.Text)
If Not String.IsNullOrWhiteSpace(sDocName) Then
lblMsg.Text = ("The document " _
+ (sDocName + " has been uploaded successfully.."))
Else
lblMsg.Text = SPErrorMsg
End If
End If
End If
End Sub
Public Function Connect(ByVal SPURL As String, ByVal SPUserLogin As String, ByVal SPPassWord As String) As Boolean
Dim bConnected As Boolean = False
Try
SPClientContext = New ClientContext(SPURL)
Dim securePassword = New SecureString
For Each c As Char In SPPassWord.ToCharArray
securePassword.AppendChar(c)
Next
SPClientContext.Credentials = New SharePointOnlineCredentials(SPUserLogin, securePassword)
SPWeb = SPClientContext.Web
SPClientContext.Load(SPWeb)
SPClientContext.ExecuteQuery()
bConnected = True
Catch ex As Exception
bConnected = False
SPErrorMsg = ex.Message
End Try
Return bConnected
End Function
Public Function UploadFile(ByVal fs As Stream, ByVal sFileName As String, ByVal sSPSiteRelativeURL As String, ByVal sLibraryName As String) As String
Dim sDocName As String = String.Empty
Try
If (Not (SPWeb) Is Nothing) Then
Dim sFileUrl = String.Format("{0}/{1}/{2}", sSPSiteRelativeURL, sLibraryName, sFileName)
Microsoft.SharePoint.Client.File.SaveBinaryDirect(SPClientContext, sFileUrl, fs, True)
sDocName = sFileName
End If
Catch ex As Exception
sDocName = String.Empty
SPErrorMsg = ex.Message
End Try
Return sDocName
End Function
End Class
Thanks!
-
Dec 13th, 2022, 02:56 PM
#2
Thread Starter
New Member
Re: connecting to SharePoint Online getting Object reference instance object error
I figured out half of my problem. I have to use a service account without MFA enabled because MFA doesn't work for the connection.
n
But the code still doesn't work in VB.net. I have to run it in C# to get a working result. Can anyone help with that?
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|