Skip to content

Instantly share code, notes, and snippets.

@gautamdsheth
Last active September 8, 2020 07:28
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/5b9ce92fe6dcaa77e22821570004717d to your computer and use it in GitHub Desktop.
Save gautamdsheth/5b9ce92fe6dcaa77e22821570004717d 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.Model.Configuration;
using OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Test
{
static void Main(string[] args)
{
AuthenticationManager authenticationManager = new AuthenticationManager();
using (ClientContext clientContext = authenticationManager.GetSharePointOnlineAuthenticatedContextTenant("https://tenant-name.sharepoint.com/sites/testSiteCollection", "user@tenant.sharepoint.com", "password"))
{
using (new PnPProvisioningContext((resource, scope) => Task.FromResult(AcquireTokenAsync(resource, scope))))
{
var tenant = new Tenant(clientContext);
var ec = ExtractConfiguration.FromString(System.IO.File.ReadAllText("C:\\PnPCommunity\\Templates\\ExtractTeam.json"));
var template1 = tenant.GetTenantTemplate(ec);
XMLTemplateProvider provider = new XMLFileSystemTemplateProvider(@"C:\PnPCommunity\Templates", "");
provider.SaveAs(template1, "TeamTemplate.xml");
}
}
}
public static string AcquireTokenAsync(string resource, string scope = null)
{
// replace this with any other method you prefer to get MS Graph Access toke
// with Group.ReadWrite.All permissions
var tenantId = "tenant.onmicrosoft.com";
// ensure that your Azure AD app has Group.ReadWrite.All and User.Read.All permissions
var clientId = "bb0c5778-9d5c-41ea-a4a8-8cd417a3bc71";
var username = "username";
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment