Skip to content

Commit

Permalink
Update tests/sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Schaeflein committed Jan 13, 2025
1 parent 52c0025 commit c967102
Show file tree
Hide file tree
Showing 13 changed files with 537 additions and 74 deletions.
24 changes: 2 additions & 22 deletions build-commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,5 @@ dotnet swagger tofile --output codegen\openapi\graph.community.spinfo.json .\moc

kiota generate -l csharp -d codegen\openapi\graph.community.spinfo.json -c "Graph.Community.SPClient" -n Graph.Community -o ./codegen/lib/apiclient --cc --co

kiota info -d "codegen\openapi\graph.community.spinfo.json" -l CSharp

# The language CSharp is currently in Stable maturity level.
# After generating code for this language, you need to install the following packages:
# Package Name Version
# Microsoft.Kiota.Abstractions 1.8.1
# Microsoft.Kiota.Authentication.Azure 1.1.4
# Microsoft.Kiota.Http.HttpClientLibrary 1.3.8
# Microsoft.Kiota.Serialization.Form 1.1.5
# Microsoft.Kiota.Serialization.Json 1.2.0
# Microsoft.Kiota.Serialization.Multipart 1.1.3
# Microsoft.Kiota.Serialization.Text 1.1.4
#
# Hint: use the install command to install the dependencies.
# Example:
# dotnet add package Microsoft.Kiota.Abstractions --version 1.8.1
# dotnet add package Microsoft.Kiota.Authentication.Azure --version 1.1.4
# dotnet add package Microsoft.Kiota.Http.HttpClientLibrary --version 1.3.8
# dotnet add package Microsoft.Kiota.Serialization.Form --version 1.1.5
# dotnet add package Microsoft.Kiota.Serialization.Json --version 1.2.0
# dotnet add package Microsoft.Kiota.Serialization.Multipart --version 1.1.3
# dotnet add package Microsoft.Kiota.Serialization.Text --version 1.1.4
NOTE:
need to manually update the folder/file names for SiteDesigns
2 changes: 1 addition & 1 deletion sample/Graph.Community.SPClient.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.11.1" />
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>
Expand Down
42 changes: 21 additions & 21 deletions sample/MenuService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ private async ValueTask MenuAsync()
Console.WriteLine("Select a sample:");
Console.WriteLine("");
Console.WriteLine("1. Diagnostics");
Console.WriteLine("2. SPWeb");
Console.WriteLine("3. Hub Sites");
//Console.WriteLine("4. Change log");
//Console.WriteLine("5. Site Pages");
Console.WriteLine("2. Hub Sites");
Console.WriteLine("3. SPWeb");
Console.WriteLine("4. Site Pages");
//Console.WriteLine("5. Change log");
//Console.WriteLine("6. SharePoint Search");
//Console.WriteLine("7. Site Design");
Console.WriteLine("7. Site Design");
//Console.WriteLine("8. Group extensions (Graph)");
//Console.WriteLine("9. ");
Console.WriteLine("");
Expand All @@ -65,26 +65,26 @@ private async ValueTask MenuAsync()

case ConsoleKey.D2:
case ConsoleKey.NumPad2:
var rootSiteSample = serviceProvider.GetRequiredService<Web>();
await rootSiteSample.Run();
var hubSiteListSample = serviceProvider.GetRequiredService<HubSiteList>();
await hubSiteListSample.Run();
break;

case ConsoleKey.D3:
case ConsoleKey.NumPad3:
var expiringSecretsSample = serviceProvider.GetRequiredService<HubSiteList>();
await expiringSecretsSample.Run();
var webSample = serviceProvider.GetRequiredService<Web>();
await webSample.Run();
break;

//case ConsoleKey.D4:
//case ConsoleKey.NumPad4:
// var changeLogSample = serviceProvider.GetRequiredService<ChangeLog>();
// await changeLogSample.Run();
// break;
case ConsoleKey.D4:
case ConsoleKey.NumPad4:
var sitePagesSample = serviceProvider.GetRequiredService<SitePages>();
await sitePagesSample.Run();
break;

//case ConsoleKey.D5:
//case ConsoleKey.NumPad5:
// var sitePagesSample = serviceProvider.GetRequiredService<SitePages>();
// await sitePagesSample.Run();
// var changeLogSample = serviceProvider.GetRequiredService<ChangeLog>();
// await changeLogSample.Run();
// break;

//case ConsoleKey.D6:
Expand All @@ -93,11 +93,11 @@ private async ValueTask MenuAsync()
// await searchSample.Run();
// break;

//case ConsoleKey.D7:
//case ConsoleKey.NumPad7:
// var siteDesignSample = serviceProvider.GetRequiredService<SiteDesign>();
// await siteDesignSample.Run();
// break;
case ConsoleKey.D7:
case ConsoleKey.NumPad7:
var siteDesignSample = serviceProvider.GetRequiredService<SiteDesign>();
await siteDesignSample.Run();
break;

//case ConsoleKey.D8:
//case ConsoleKey.NumPad8:
Expand Down
4 changes: 2 additions & 2 deletions sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ static async Task Main(string[] args)

// Add our sample classes
services.AddTransient<Diagnostics>();
services.AddTransient<Web>();
services.AddTransient<HubSiteList>();
services.AddTransient<Web>();
services.AddTransient<SitePages>();
//services.AddTransient<ChangeLog>();
//services.AddTransient<SiteGroups>();
//services.AddTransient<SitePages>();
//services.AddTransient<SharePointSearch>();
//services.AddTransient<SiteDesign>();
//services.AddTransient<GraphGroupExtensions>();
Expand Down
148 changes: 148 additions & 0 deletions sample/SiteDesign.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using Azure.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Graph.Community.SPClient.Sample
{
internal class SiteDesign
{
private readonly AzureAdSettings azureAdSettings;
private readonly SharePointSettings sharePointSettings;

public SiteDesign(
IOptions<AzureAdSettings> azureAdOptions,
IOptions<SharePointSettings> sharePointOptions)
{
this.azureAdSettings = azureAdOptions.Value;
this.sharePointSettings = sharePointOptions.Value;
}

public async Task Run()

Check warning on line 25 in sample/SiteDesign.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
////////////////////////////////////////
//
// Capture diagnostic information
//
///////////////////////////////////////

var logger = new StringBuilderHttpMessageLogger();

//////////////////////
//
// TokenCredential
//
//////////////////////

var credential = new ChainedTokenCredential(
new SharedTokenCacheCredential(new SharedTokenCacheCredentialOptions() { TenantId = azureAdSettings.TenantId, ClientId = azureAdSettings.ClientId }),
new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TenantId = azureAdSettings.TenantId, ClientId = azureAdSettings.ClientId })
);


////////////////////////////////////////////////////////////
//
// SharePoint REST Client with Logger and SharePoint service handler
//
////////////////////////////////////////////////////////////

// Configure our client
SPClientOptions clientOptions = new()
{
// use the default user agent

//UserAgentInfo = new SharePointThrottlingDecoration()
//{
// CompanyName = "Company",
// AppName = "Application",
// AppVersion = "0.0.0",
// ISV = false
//},

// use our logger
MessageLogger = logger
};

var spClient = SPClientFactory.Create(sharePointSettings.SpoTenantUrl, credential, clientOptions);

///////////////////////////////////////
//
// Setup is complete, run the sample
//
//////////////////////////////////////

var scopes = new string[] { $"{sharePointSettings.SpoTenantUrl}/AllSites.FullControl" };
var WebUrl = $"{sharePointSettings.SpoTenantUrl}{sharePointSettings.ServerRelativeSiteUrl}";

//var siteScript = new SiteScriptMetadata()
//{
// Title = "Green Theme",
// Description = "Apply the Green Theme",
// Content = "{\"$schema\": \"schema.json\",\"actions\": [{\"verb\": \"applyTheme\",\"themeName\": \"Green\"}],\"bindata\": { },\"version\": 1}",
//};

//try
//{


// var createdScript = await graphServiceClient
// .SharePointAPI(WebUrl)
// .SiteScripts
// .Request()
// .WithScopes(scopes)
// .CreateAsync(siteScript);

// var siteDesign = new SiteDesignMetadata()
// {
// Title = "Green Theme",
// Description = "Apply the Green theme",
// SiteScriptIds = new System.Collections.Generic.List<Guid>() { new Guid(createdScript.Id) },
// WebTemplate = "64" // 64 = Team Site, 68 = Communication Site, 1 = Groupless Team Site
// };

// var createdDesign = await graphServiceClient
// .SharePointAPI(WebUrl)
// .SiteDesigns
// .Request()
// .WithScopes(scopes)
// .CreateAsync(siteDesign);

// var applySiteDesignRequest = new ApplySiteDesignRequest
// {
// SiteDesignId = createdDesign.Id,
// WebUrl = WebUrl
// };

// var applySiteDesignResponse = await graphServiceClient
// .SharePointAPI(WebUrl)
// .SiteDesigns
// .Request()
// .WithScopes(scopes)
// .ApplyAsync(applySiteDesignRequest);

// foreach (var outcome in applySiteDesignResponse.CurrentPage)
// {
// Console.WriteLine(outcome.OutcomeText);
// }

//}
//catch (Exception ex)
//{

// Console.WriteLine(ex.Message);
//}


Console.WriteLine("Press enter to show log");
Console.ReadLine();
Console.WriteLine();
var log = logger.GetLog();
Console.WriteLine(log);

}
}
}
106 changes: 106 additions & 0 deletions sample/SitePages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Azure.Identity;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Graph.Community.SPClient.Sample
{
internal class SitePages
{
private readonly AzureAdSettings azureAdSettings;
private readonly SharePointSettings sharePointSettings;

public SitePages(
IOptions<AzureAdSettings> azureAdOptions,
IOptions<SharePointSettings> sharePointOptions)
{
this.azureAdSettings = azureAdOptions.Value;
this.sharePointSettings = sharePointOptions.Value;
}


public async Task Run()
{
////////////////////////////////////////
//
// Capture diagnostic information
//
///////////////////////////////////////

var logger = new StringBuilderHttpMessageLogger();

//////////////////////
//
// TokenCredential
//
//////////////////////

var credential = new ChainedTokenCredential(
new SharedTokenCacheCredential(new SharedTokenCacheCredentialOptions() { TenantId = azureAdSettings.TenantId, ClientId = azureAdSettings.ClientId }),
new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TenantId = azureAdSettings.TenantId, ClientId = azureAdSettings.ClientId })
);


////////////////////////////////////////////////////////////
//
// SharePoint REST Client with Logger and SharePoint service handler
//
////////////////////////////////////////////////////////////

// Configure our client
SPClientOptions clientOptions = new()
{
// use the default user agent

//UserAgentInfo = new SharePointThrottlingDecoration()
//{
// CompanyName = "Company",
// AppName = "Application",
// AppVersion = "0.0.0",
// ISV = false
//},

// use our logger
MessageLogger = logger
};

var spClient = SPClientFactory.Create(sharePointSettings.SpoTenantUrl, credential, clientOptions);

///////////////////////////////////////
//
// Setup is complete, run the sample
//
//////////////////////////////////////

var scopes = new string[] { $"{sharePointSettings.SpoTenantUrl}/AllSites.FullControl" };
var WebUrl = $"{sharePointSettings.SpoTenantUrl}{sharePointSettings.ServerRelativeSiteUrl}";

try
{
var sitePages = await spClient[sharePointSettings.ServerRelativeSiteUrl]._api.Sitepages.Pages.GetAsync();

Console.WriteLine($"Site Pages for {WebUrl}");
Console.WriteLine($" count: {sitePages?.Value?.Count.ToString() ?? "<null>" }");

}
catch (Exception ex)
{
await logger.WriteLine("");
await logger.WriteLine("================== Exception caught ==================");
await logger.WriteLine(ex.ToString());
}


Console.WriteLine("Press enter to show log");
Console.ReadLine();
Console.WriteLine();
var log = logger.GetLog();
Console.WriteLine(log);


}
}
}
1 change: 1 addition & 0 deletions sample/Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public async Task Run()
});

Console.WriteLine($"Title: {web?.Title}");
Console.WriteLine($"Home page: {web?.WelcomePage}");
Console.WriteLine($"UserCustomAction count: {web?.UserCustomActions?.Count ?? 0}");
Console.WriteLine();
}
Expand Down
Loading

0 comments on commit c967102

Please sign in to comment.