Skip to content

Instantly share code, notes, and snippets.

@gautamdsheth
Created June 13, 2020 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gautamdsheth/676f1bd85e2425f453b9358c8be57cdf to your computer and use it in GitHub Desktop.
Save gautamdsheth/676f1bd85e2425f453b9358c8be57cdf to your computer and use it in GitHub Desktop.
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using Newtonsoft.Json.Linq;
using OfficeDevPnP.Core;
using OfficeDevPnP.Core.Framework.Provisioning.Connectors;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Providers;
using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
using System;
using System.IO;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
AuthenticationManager authenticationManager = new AuthenticationManager();
using (ClientContext clientContext = authenticationManager.GetSharePointOnlineAuthenticatedContextTenant("https://tenant-admin.sharepoint.com", "username", "password"))
{
using (new PnPProvisioningContext((resource, scope) => Task.FromResult(AcquireTokenAsync(resource, scope))))
{
var tenant = new Tenant(clientContext);
ProvisioningHierarchy hierarchyToApply = LoadProvisioningHierarchyFromFile("AddAzureADUsers.xml", null);
tenant.ApplyProvisionHierarchy(hierarchyToApply, "SAMPLE-SEQUENCE");
}
}
}
public static string AcquireTokenAsync(string resource, string scope = null)
{
var tenantId = "tenant.onmicrosoft.com";
// ensure that your Azure AD app has Group.ReadWrite.All and User.Read.All permissions
var clientId = "<client-id>";
var username = "admin@tenant.onmicrosoft.com";
var password = "password";
string body;
string response;
if (scope == null) // use v1 endpoint
{
body = $"grant_type=password&client_id={clientId}&username={username}&password={password}&resource={resource}";
response = OfficeDevPnP.Core.Utilities.HttpHelper.MakePostRequestForString($"https://login.microsoftonline.com/{tenantId}/oauth2/token", body, "application/x-www-form-urlencoded");
}
else // use v2 endpoint
{
body = $"grant_type=password&client_id={clientId}&username={username}&password={password}&scope={scope}";
response = OfficeDevPnP.Core.Utilities.HttpHelper.MakePostRequestForString($"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token", body, "application/x-www-form-urlencoded");
}
var json = JToken.Parse(response);
return json["access_token"].ToString();
}
public static ProvisioningHierarchy LoadProvisioningHierarchyFromFile(string templatePath, ITemplateProviderExtension[] templateProviderExtensions)
{
// Prepare the File Connector
string templateFileName = System.IO.Path.GetFileName(templatePath);
// Prepare the template path
var fileInfo = new FileInfo(templatePath);
FileConnectorBase fileConnector = new FileSystemConnector(fileInfo.DirectoryName, "");
// Load the provisioning template file
var isOpenOfficeFile = false;
XMLTemplateProvider provider;
if (isOpenOfficeFile)
{
provider = new XMLOpenXMLTemplateProvider(new OpenXMLConnector(templateFileName, fileConnector));
templateFileName = templateFileName.Substring(0, templateFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";
var hierarchy = (provider as XMLOpenXMLTemplateProvider).GetHierarchy();
if (hierarchy != null)
{
hierarchy.Connector = provider.Connector;
return hierarchy;
}
}
else
{
provider = new XMLFileSystemTemplateProvider(fileConnector.Parameters[FileConnectorBase.CONNECTIONSTRING] + "", "");
}
ProvisioningHierarchy provisioningHierarchy = provider.GetHierarchy(templateFileName);
provisioningHierarchy.Connector = provider.Connector;
// Return the result
return provisioningHierarchy;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment