pnpcore icon indicating copy to clipboard operation
pnpcore copied to clipboard

Archiving SharePoint Online Site

Open jansenbe opened this issue 5 months ago • 0 comments

Discussed in https://github.com/pnp/pnpcore/discussions/1507

Originally posted by EdAlexander August 16, 2024 I am trying to find a pnp.core code equivelant to the PowerShell commandlet "Set-PNPSiteArchiveState". Is there a supported way to do this in c#?

I have tried simple restful call with bearer token but always get "unauthorized" as response. ` using (var sourceContext = context.Clone(new System.Uri(SelectedCase.caseUrl))) {

            using (var httpClient = new HttpClient())
            {
               
                var accessToken = await context.AuthenticationProvider
                    .GetAccessTokenAsync(new Uri("https://{mytennant}.sharepoint.com/.default"));
                
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                var requestUri = new Uri("https://{mytennant}-admin.sharepoint.com/_api/SiteArchive/Archive");
                var content = new StringContent("{\"siteUrl\":\"https://{mytennant}.sharepoint.com/sites/" + sourceSite + "\"}", System.Text.Encoding.UTF8, "application/json");

                var response = await httpClient.PostAsync(requestUri, content);

                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Site archived successfully.");
                }
                else
                {
                    Console.WriteLine($"Failed to archive site. Status code: {response.StatusCode}");
                }
            }

jansenbe avatar Sep 18 '24 12:09 jansenbe