如何以编程方式登录 SharePoint Online 并获取 Web HTML?

编程入门 行业动态 更新时间:2024-10-23 13:33:02
本文介绍了如何以编程方式登录 SharePoint Online 并获取 Web HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用C#和CSOM,并希望通过以下步骤实现SharePoint Online网站的目标:

I am using C# and CSOM, and would like to achieve a goal for a SharePoint Online site with following steps:

  • 通过给定的列表名称获取 list_id (对于CSOM,此步骤非常简单)

  • acquire a list_id by a given list name (this step is very easy with CSOM)

    使用 list_id 访问页面: {domain} .sharepoint/_layouts/15/listedit.aspx?List = {list_id} (根据列表ID的列表设置页面)

    use the list_id to access page: {domain}.sharepoint/_layouts/15/listedit.aspx?List={list_id} (the list setting page according to the list ID)

    抓取整个页面的 HTML 内容,然后做一些进一步的 GET/POST 操作

    grab the whole page HTML content, then do some further GET/POST operations

    我的问题是:我陷入了第2步&3,无法以编程方式登录SharePoint Online网站并保留会话上下文以进行进一步的连续GET/POST操作.

    My issue is: I am stuck on step 2 & 3, unable to login into the SharePoint Online site programmatically and hold the session context for a further successive GET/POST operations.

    • 实际上我已经使用 var httpClient = new HttpClient(new HttpClientHandler {Credentials = new NetworkCredential(username,password)})

    然后使用此 HttpClient 登录OnPerm网站,并保留登录上下文以获取进一步的GET/POST请求

    then use this HttpClient to login into the OnPerm site, and hold the login context for further GET/POST requests

    我对SharePoint Online的意图与上面相同,具有给定的字符串站点,字符串用户名和 SecureString密码,用于登录SharePoint在线站点,并以编程方式执行GET/POST.

    My intention to SharePoint Online is same as above, with a given string site, string username and SecureString password, to login the SharePoint Online site and do GET/POST programmatically.

    当前,我只是使用 SharePointOnlineCredentials 替换 NetworkCredential 来获取 HttpClient 进行登录,但只会出现401和502错误.

    Currently I am simply using SharePointOnlineCredentials for replacing NetworkCredential to get a HttpClient for login, but only get 401 and 502 errors.

    推荐答案

    我们可以使用WebClient在SharePoint中获取页面HTML.以下代码供您参考.

    We can use the WebClient to get the page HTML in SharePoint. The following code for your reference.

    using System; using System.Security; using Microsoft.SharePoint.Client; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string siteUrl = "tenant.sharepoint"; string userName = "xxx@tenant.onmicrosoft"; string password = "xxx"; string listName = "listname"; var securePassword = new SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } var credentials = new SharePointOnlineCredentials(userName, securePassword); var ctx = new ClientContext(siteUrl); ctx.Credentials = credentials; var list = ctx.Web.Lists.GetByTitle(listName); ctx.Load(list); ctx.ExecuteQuery(); using (var wc = new System.Net.WebClient()) { wc.Credentials = credentials; wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC)"; var pageHtml = wc.DownloadString(siteUrl + "/_layouts/15/listedit.aspx?List={" + list.Id.ToString() + "}"); Console.WriteLine(pageHtml); } Console.ReadKey(); } } }
  • 更多推荐

    如何以编程方式登录 SharePoint Online 并获取 Web HTML?

    本文发布于:2023-11-26 00:05:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1631876.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:方式   SharePoint   Online   Web   HTML

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!