【问题标题】:Get data from SharePoint Online List using VB.NET使用 VB.NET 从 SharePoint Online 列表中获取数据
【发布时间】:2020-11-04 00:52:28
【问题描述】:

我正在开发一种从 SharePoint Online 列表中获取数据的解决方案,但是当我尝试检索和执行查询时,出现了错误。错误是。

System.Net.WebException: '远程服务器返回错误:(403) Forbidden.'

我使用 4 个文本框(URL、用户、密码、名称列表)和一个 Datagridview。

Using context As ClientContext = New ClientContext(TextBox1.Text.Trim())

    context.AuthenticationMode = ClientAuthenticationMode.[Default]
    context.Credentials = New NetworkCredential(TextBoxName.Text.Trim(), TextBoxPassword.Text.Trim(), "https://example.sharepoint.com/sites/SPSite/")

    Dim webObj As Web = context.Web
    Dim listObj As List = webObj.Lists.GetByTitle(TextBoxLista.Text.Trim())
    Dim cqObj As CamlQuery = CamlQuery.CreateAllItemsQuery(100)
    Dim collObj As ListItemCollection = listObj.GetItems(cqObj)

    context.Load(collObj)
    context.ExecuteQuery() 'Line whit error

End Using

您能帮我知道错误的原因以及如何解决吗?

提前谢谢你。

问候。

【问题讨论】:

  • 您能否提供更多关于哪一行代码引发异常的详细信息?它将帮助其他人分析您的问题。
  • @XingyuZhao,谢谢你的回答。该行是context.ExecuteQuery(),发送消息System.Net.WebException: 'The remote server returned an error: (403) Forbidden.'。在我是所有者的网站中。
  • 尝试使用 SharePointOnlineCredentials 处理握手,例如:context.Credentials = New SharePointOnlineCredentials("yourlogin@yoursite.onmicrosoft.com", passWord)
  • @XingyuZhao 准备好了,成功了,感谢您的支持和回答。我为其他需要它的同事添加答案。问候。

标签: vb.net sharepoint visual-studio-2019


【解决方案1】:

这是解决方案的代码。

谢谢。

Imports Microsoft.SharePoint.Client
Imports Microsoft.SharePoint
Imports System.Security
Imports System.Net
Public Class Form4

    Dim siteUrl As String = "https://namedomain.sharepoint.com/sites/TaskSP/"
    'namedomain is the name of the site domain, TaskSP is the name site.

    Dim context As New ClientContext(siteUrl)
    Dim web As Web

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try

            Dim userNameSP As String = TextBox1.Text
            'Textbox1.text is the mail account to access site.
            Dim password As String = TextBox2.Text
            'Textbox2.text is the password of your mail account to access site.
            Dim secureString As SecureString = New NetworkCredential("", password).SecurePassword

            Dim cred = New SharePointOnlineCredentials(userNameSP, secureString)
            Dim clientContext As New ClientContext(siteUrl)
            clientContext.Credentials = cred
            Dim web As Web = clientContext.Web
            Dim oWebsite As Web = clientContext.Web
            Dim collList As ListCollection = oWebsite.Lists

            Dim oList As List = collList.GetByTitle("TestList")
            'TestList is the name of the list you want to query

            clientContext.Load(oList)

            clientContext.ExecuteQuery()

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

End Class

如果你需要,我希望这对你有用。

问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多