Files
VSC/_NuGet/CSOM/CreateCustomPermissionLevel.cs
T
claudio 368d6fafea Issue
Code backup
2026-05-10 16:59:01 +02:00

70 lines
2.7 KiB
C#

namespace Console.Office365
{
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using Newtonsoft.Json.Linq;
using OfficeDevPnP.Core.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
CreateCustomPermissionLevel();
}
public static void CreateCustomPermissionLevel()
{
OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
string siteUrl = "https://*********.sharepoint.com/sites/communitysite";
string userName = "Sathish@*********.onmicrosoft.com";
string password = "************";
using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
{
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.Load(web.AllProperties);
clientContext.Load(web.RoleDefinitions);
clientContext.ExecuteQueryRetry();
var roleDefinitions = web.RoleDefinitions;
// Get Owners Group and Remove the Permission Levels
var ownersGroupRoleAssignment = web.RoleAssignments.GetByPrincipal(clientContext.Web.AssociatedOwnerGroup);
ownersGroupRoleAssignment.RoleDefinitionBindings.RemoveAll();
ownersGroupRoleAssignment.Update();
clientContext.Load(ownersGroupRoleAssignment);
clientContext.ExecuteQuery();
// Get Full Control Role Definition
var customFullControlRoleDefinition = roleDefinitions.GetByName("MyPermissionLevelCreatedByCode");
clientContext.Load(customFullControlRoleDefinition);
clientContext.ExecuteQuery();
RoleDefinitionBindingCollection collRDB = new RoleDefinitionBindingCollection(clientContext);
collRDB.Add(roleDefinitions.GetByName("MyPermissionLevelCreatedByCode"));
// Bind the Newly Created Permission Level to Owners Group
web.RoleAssignments.Add(web.SiteGroups.GetById(ownersGroupRoleAssignment.PrincipalId), collRDB);
// Bind the Newly Created Permission Level to Owners Group
//ownersGroupRoleAssignment.RoleDefinitionBindings.Add(customFullControlRoleDefinition);
//ownersGroupRoleAssignment.Update();
clientContext.Load(ownersGroupRoleAssignment);
clientContext.ExecuteQuery();
}
}
}
}