如何以编程方式登录 SharePoint Online 并获取 Web HTML? [英] How to programmatically login on SharePoint Online and get web HTML?

查看:63
本文介绍了如何以编程方式登录 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:

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

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

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

use the list_id to access page: https://{domain}.sharepoint.com/_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 = "https://tenant.sharepoint.com";
            string userName = "xxx@tenant.onmicrosoft.com";
            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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆