我有一个SiteId,我想生成一个ClientContext
来获取该特定网站的所有组。但我无法找到从SiteId生成ClientCond的方法,就像我们在SharePoint内部部署一样。
有没有办法从SharePoint Online中的SiteId生成ClientContext,或者我们只需要URL?
我想实现这样的目标:
using(var context = new ClientContext(new GUId(siteId))
{
//TODO
}
答案 0 :(得分:0)
您可以分两步获取ClientContext
:
这里有一些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模块才能运行此代码。)