如何使用Sharepoint.client创建组并将其添加到sharepoint站点 [英] How do you create and add a group to a sharepoint site using Sharepoint.client

查看:109
本文介绍了如何使用Sharepoint.client创建组并将其添加到sharepoint站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让这一整天工作,而且不想工作。我正在构建一个应用程序,将三个默认组添加到站点,希望清理权限。我要向您展示的代码将创建组,但在我转到权限时它们不会显示。它只是基本上创建了这个组,就是这样。



使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;使用System.Drawing
;
使用System.Data;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用Microsoft.SharePoint.Client;


private void CreateMainGroups(string siteurl,string SiteName)
{
// method为站点创建一个defualt访问者,贡献者和完全控制组。
//向网站添加新的自定义组

ClientContext context = new ClientContext(siteurl);

Web web = context.Web;
//查看组是否存在
context.Load(web.SiteGroups);
context.ExecuteQuery();
Group Admin =(来自web.SiteGroups中的g,其中g.Title == SiteName +Owners选择g).SingleOrDefault();
Group Visitor =(来自web.SiteGroups中的g,其中g.Title == SiteName +Visitors选择g).SingleOrDefault();
Group Members =(来自web.SiteGroups中的g,其中g.Title == SiteName +Members选择g).SingleOrDefault();




if(Admin == null)
{
//如果不存在则创建管理组
GroupCreationInformation groupCreationInfoAdmin = new GroupCreationInformation();
groupCreationInfoAdmin.Title = SiteName +Owners;
groupCreationInfoAdmin.Description =使用此组为人们提供对SharePoint网站的完全控制权限:+ SiteName;
Admin = web.SiteGroups.Add(groupCreationInfoAdmin);

}

if(Members == null)
{
//如果不存在则创建贡献者组
GroupCreationInformation groupCreationInfoContribut = new GroupCreationInformation();
groupCreationInfoContribut.Title = SiteName +Members;
groupCreationInfoContribut.Description =使用此组为人们提供SharePoint网站的权限:+ SiteName;
Members = web.SiteGroups.Add(groupCreationInfoContribut);

}

if(访客== null)
{
//创建访客群组,如果它不存在
GroupCreationInformation groupCreationInfoVisitors = new GroupCreationInformation();
groupCreationInfoVisitors.Title = SiteName +Visitors;
groupCreationInfoVisitors.Description =使用此组授予人们对SharePoint网站的读取权限:+ SiteName;
Visitor = web.SiteGroups.Add(groupCreationInfoVisitors);

}



//分配站点角色这是它应该将组分配给Web上的关联组的问题区域但是它永远不会奏效

web.AssociatedOwnerGroup = Admin;
web.AssociatedOwnerGroup.Update();
Admin.Update();

web.AssociatedVisitorGroup =访客;
web.AssociatedVisitorGroup.Update();
Visitor.Update();

web.AssociatedMemberGroup =会员;
web.AssociatedMemberGroup.Update();
Members.Update();

web.Update();



context.ExecuteQuery();






}





我在这里错过了什么吗?任何帮助将不胜感激。

解决方案

我能够提出解决方案,这个问题做了以下代码,这基本上取代了我的问题代码部分



 RoleDefinition rda = web.RoleDefinitions.GetByName(完全控制); 
RoleDefinition rdm = web.RoleDefinitions.GetByName(Contribute);
RoleDefinition rdv = web.RoleDefinitions.GetByName(Read);



RoleDefinitionBindingCollection bindA = new RoleDefinitionBindingCollection(context);
RoleDefinitionBindingCollection bindM = new RoleDefinitionBindingCollection(context);
RoleDefinitionBindingCollection bindV = new RoleDefinitionBindingCollection(context);
bindA.Add(rda);
bindM.Add(rdm);
bindV.Add(rdv);

web.RoleAssignments.Add(Admin,bindA);
web.RoleAssignments.Add(Members,bindM);
web.RoleAssignments.Add(Visitor,bindV);





如果有人有更好的解决方案让我知道这是我能让它工作的唯一方法。


I have been trying to get this to work all day and its just not wanting to work. I am building a application to add the three default groups to sites in hopes of cleaning up permissions. The code I am about to show you will create the groups but they will not show up when i go to permissions. It just basicly creates the group and thats it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;


private void CreateMainGroups(string siteurl, string SiteName)
        {
          //method creates a defualt visitor, contributor and full control group for a site.
            // Add a new custom group to the site 
        
            ClientContext context = new ClientContext(siteurl);
        
            Web web = context.Web;
            //See if the groups exist
            context.Load(web.SiteGroups);
            context.ExecuteQuery();
            Group Admin = (from g in web.SiteGroups where g.Title == SiteName + " Owners" select g).SingleOrDefault();
            Group Visitor = (from g in web.SiteGroups where g.Title == SiteName + " Visitors" select g).SingleOrDefault();
            Group Members = (from g in web.SiteGroups where g.Title == SiteName + " Members" select g).SingleOrDefault();

                      
            
            
            if (Admin == null)
            {
                //Create the Admin Group if it dont exist
                GroupCreationInformation groupCreationInfoAdmin = new GroupCreationInformation();
                groupCreationInfoAdmin.Title = SiteName + " Owners";
                groupCreationInfoAdmin.Description = " Use this group to give people full control permissions to the SharePoint site: " + SiteName;
                Admin = web.SiteGroups.Add(groupCreationInfoAdmin);
               
            }
           
            if(Members == null)
            {
            //Create the Contributer Group  if it dont exist
            GroupCreationInformation groupCreationInfoContribut = new GroupCreationInformation();
            groupCreationInfoContribut.Title = SiteName + " Members";
            groupCreationInfoContribut.Description = "Use this group to give people contribute permissions to the SharePoint site: "+ SiteName;
            Members = web.SiteGroups.Add(groupCreationInfoContribut);
           
            }

            if(Visitor == null)
            {
            //Create the Visitor Group  if it dont exist
            GroupCreationInformation groupCreationInfoVisitors = new GroupCreationInformation();
            groupCreationInfoVisitors.Title = SiteName + " Visitors";
            groupCreationInfoVisitors.Description = "Use this group to grant people read permissions to the SharePoint site: " + SiteName;
            Visitor = web.SiteGroups.Add(groupCreationInfoVisitors);
            
            }
           
           
           
            //Assign site roles This is the problem area it's supposed to assign the group to the associated group on the web but it never works. 

            web.AssociatedOwnerGroup = Admin;
            web.AssociatedOwnerGroup.Update();
            Admin.Update();

            web.AssociatedVisitorGroup = Visitor;
            web.AssociatedVisitorGroup.Update();
            Visitor.Update();

            web.AssociatedMemberGroup = Members;
            web.AssociatedMemberGroup.Update();
            Members.Update();

            web.Update();
            
           
            
            context.ExecuteQuery();
          





        }



Am I missing something here? Any help would be appreciated.

解决方案

I was able to come up with a solutionf ro this issue doing the following code this basicly replaced my problem code section

RoleDefinition rda = web.RoleDefinitions.GetByName("Full Control");
              RoleDefinition rdm = web.RoleDefinitions.GetByName("Contribute");
              RoleDefinition rdv = web.RoleDefinitions.GetByName("Read");



              RoleDefinitionBindingCollection bindA = new RoleDefinitionBindingCollection(context);
              RoleDefinitionBindingCollection bindM = new RoleDefinitionBindingCollection(context);
              RoleDefinitionBindingCollection bindV = new RoleDefinitionBindingCollection(context);
              bindA.Add(rda);
              bindM.Add(rdm);
              bindV.Add(rdv);

              web.RoleAssignments.Add(Admin, bindA);
              web.RoleAssignments.Add(Members, bindM);
              web.RoleAssignments.Add(Visitor, bindV);



If anyone has a better solution let me know this is the only way I could get it to work.


这篇关于如何使用Sharepoint.client创建组并将其添加到sharepoint站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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