【问题标题】:How to generate a ClientContext from SiteId in SharePoint Online?如何从 SharePoint Online 中的 SiteId 生成 ClientContext?
【发布时间】:2017-09-26 19:55:42
【问题描述】:

我有一个 SiteId,我想生成一个 ClientContext 来获取该特定站点的所有组。但我无法找到一种从 SiteId 生成 ClientContext 的方法,就像我们在本地 SharePoint 中所做的那样。

有没有办法从 SharePoint Online 中的 SiteId 生成 ClientContext 或者我们只需要 URL?

我想实现这样的目标:

using(var context = new ClientContext(new GUId(siteId))
{
//TODO
}

【问题讨论】:

    标签: sharepoint-online


    【解决方案1】:

    您可以通过两个步骤获取您的ClientContext

    • 使用搜索 API 按 ID 搜索网站
    • 使用网站的 URL 创建客户端上下文

    这里有一些 PowerShell 正是这样做的。出于方便,我使用了PnP Cmdlets,使用普通的 CSOM 也可以实现类似的结果。

    # this is your site's ID
    $siteId = "a20d2341-1b4f-47ed-8180-24a5c31adfa9"
    # basically any known site URL - the root is probably fine
    $anySiteUrl = "https://<yourtenant>.sharepoint.com"
    $credential = Get-Credential
    
    Connect-PnPOnline –Url $anySiteUrl –Credentials $credential
    # search for site by ID
    $site = Submit-PnPSearchQuery -Query "SiteID:$siteId AND ContentClass=STS_Site"
    if ($site.ResultRows.Count -eq 1) 
    {
        # URL to use for "real" connection
        $siteUrl = $site.ResultRows[0].Path
        Connect-PnPOnline –Url $siteUrl –Credentials $credential
        $currentSite = Get-PnPSite
        # and there is your ClientContext
        $ctx = Get-PnPContext
        $web = $currentSite.RootWeb
        $ctx.Load($web)
        $ctx.Load($web.SiteGroups)
        $ctx.ExecuteQuery()
        # here are your groups
        $web.SiteGroups
    }
    

    (注意:您必须安装SharePointPnPPowerShellOnline PowerShell 模块才能运行此代码。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 2017-01-18
      相关资源
      最近更新 更多