2017-06-20 50 views
0

我想使用CSOM設置特定子站點的組所有者。使用Powershell和CSOM檢索Sharepoint聯機組

我的網站收藏路徑是「https://mytenant.sharepoint.com/」。

子站點路徑「https://mytenant.sharepoint.com/subsite」。

首先我要檢索我的特定子站點的SharePoint組,但我的代碼檢索我的收集站點的所有子站點組。

這裏是我的代碼:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 
$siteURL = "https://mytenant.sharepoint.com/subsite" 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL) 
$userId = "myID" 
$pwd = Read-Host -Prompt "Enter password" -AsSecureString 
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd) 
$ctx.credentials = $creds 

$web = $ctx.web 
$groups = $web.SiteGroups 
$ctx.Load($groups) 

$ctx.ExecuteQuery() 

foreach($group in $groups){ 
    Write-Host "Site Group : " $group.Title 
    $ctx.ExecuteQuery() 

} 

任何人都可以解釋我做錯了嗎?

回答

2

SharePoint組的作用域網站集(docs):

SharePoint服務器支持兩種羣體:域組和SharePoint組。域組保留在SharePoint Server控制之外;用戶無法使用SharePoint Server來定義,瀏覽或修改域組成員資格。 SharePoint組的作用域爲網站集級別,並且只能在網站集內使用。域組可以在Active Directory目錄服務範圍內的任何位置使用。

在SharePoint Server OM你有SPWeb.SiteGroupsSPWeb.Groups,第二可以自動過濾掉特定的網頁自定義權限中使用的羣體。這個meams,他們被用在一些安全的對象,如ListItem

在SharePoint客戶端OM中,此區別消失,您只有Web.SiteGroups集合。

要得到業主,會員和訪問者組特定的Web使用特性:

$web.AssociatedVisitorGroup 
$web.AssociatedMemberGroup 
$web.AssociatedOwnerGroup 

你可以用方法來創建它們:

$usr = $web.EnsureUser($userId) 
$ctx.Load($usr) 
$ctx.ExecuteQuery() 
$web.CreateDefaultAssociatedGroups($usr.LoginName, "", "") 
$ctx.ExecuteQuery() 

你更新後的腳本應該是這樣的:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 
$siteURL = "https://mytenant.sharepoint.com/subsite" 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL) 
$userId = "myID" 
$pwd = Read-Host -Prompt "Enter password" -AsSecureString 
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd) 
$ctx.credentials = $creds 

$web = $ctx.web 

$ctx.Load($web.AssociatedMemberGroup) 
$ctx.Load($web.AssociatedOwnerGroup) 
$ctx.Load($web.AssociatedVisitorGroup) 
$ctx.ExecuteQuery() 

Write-Host "Owners group : " $web.AssociatedMemberGroup.Title 
Write-Host "Members group : " $web.AssociatedMemberGroup.Title 
Write-Host "Visitors group : " $web.AssociatedMemberGroup.Title 

請使用此以作進一步參考:Using the Client Object Model

+0

我嘗試$ web.AssociatedVisitorGroup,仍然不起作用 – ysfibm

+0

你是什麼意思?你得到錯誤,屬性爲空? – tinamou

+0

它檢索我:Microsoft.SharePoint.Client.Group – ysfibm

0

下面的代碼爲我工作

ClientContext _ctx= new ClientContext(strURL); 
_ctx.Credentials = new NetworkCredential(strUserName, pass); 
Web web = _ctx.Site.RootWeb; 
_ctx.Load(web, x => x.Title); 
var groups = web.SiteGroups; 
_ctx.Load(web.SiteGroups); 
_ctx.ExecuteQuery(); 

string strGroup = ""; 
foreach (var grp in groups) 
{ 
     strGroup += grp.Title + " : " + grp.Id; 
} 
相關問題