Skip to content

Commit

Permalink
New graph helper (#4617)
Browse files Browse the repository at this point in the history
* Update use of GraphHelper

* Renamed GraphHelper to ApiRequestHelper

* Simplified examples

* simplified URL

* renamed file and updated url handling

* Nightly publish to PowerShell Gallery

* Update to version checker

* Rewrote to use tracelog and added Format-PnPTraceLog cmdlet

* Added automatic login: more work to do

* switched to settings.json

* Nightly publish to PowerShell Gallery

* Add ExtendPermissionsToUnprotectedFiles parameter to Set-PnPTenant cmdlet (#4602)

Co-authored-by: Gautam Sheth <gautam.sheth@staffbase.com>

* Nightly publish to PowerShell Gallery

* Added deprecation notice on the SharePoint Mail API

* Nightly publish to PowerShell Gallery

* Added paramset for working with doclibs

* Finalized implementation of Get-PnPFolderItem

* Added same logic to Get-PnPFileInFolder and Get-PnPFolderInFolder as well

* Added changelog entry

* moved from cachesettings.json to settings.json

* Updated handling of tracelog

* refactored private field name

* Updated tracelogging

* renamed file

* Updated tracelogging cmdlets

* Nightly publish to PowerShell Gallery

* removed unneeded include

* Removed unused usings

* Fixed using

* Changed warning message

* Updated cleaning

* Updated cache enabled message

* Fixed JSON escaping in log message

---------

Co-authored-by: Gautam Sheth <gautamdsheth@outlook.com>
Co-authored-by: Gautam Sheth <gautam.sheth@staffbase.com>
Co-authored-by: Koen Zomers <koen@zomers.eu>
  • Loading branch information
4 people authored Dec 15, 2024
1 parent 472a561 commit b611be0
Show file tree
Hide file tree
Showing 564 changed files with 1,675 additions and 2,515 deletions.
16 changes: 8 additions & 8 deletions documentation/Invoke-PnPGraphMethod.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,48 +96,48 @@ Set the new displayName of the group with a Patch request.

### Example 4
```powershell
Invoke-PnPGraphMethod -Url "v1.0/users?$filter=accountEnabled ne true&$count=true" -Method Get -ConsistencyLevelEventual
Invoke-PnPGraphMethod -Url "users?`$filter=accountEnabled ne true&`$count=true" -Method Get -ConsistencyLevelEventual
```

Get users with advanced query capabilities. Use of -ConsistencyLevelEventual.

### Example 5
```powershell
Invoke-PnPGraphMethod -Url "https://graph.microsoft.com/v1.0/users"
Invoke-PnPGraphMethod -Url "users"
```

Performs a GET request to retrieve users from the Microsoft Graph API using the full URL.

### Example 6
```powershell
Invoke-PnPGraphMethod -Url "https://graph.microsoft.com/v1.0/users/user@contoso.com/photo/`$value" -OutFile c:\temp\photo.jpg
Invoke-PnPGraphMethod -Url "users/user@contoso.com/photo/`$value" -OutFile c:\temp\photo.jpg
```

Downloads the user profile photo of the specified user to the specified file.

### Example 7
```powershell
Invoke-PnPGraphMethod -Url "https://graph.microsoft.com/v1.0/users/user@contoso.com/photo/`$value" -OutStream | Add-PnPFile -FileName user.jpg -Folder "Shared Documents"
Invoke-PnPGraphMethod -Url "users/user@contoso.com/photo/`$value" -OutStream | Add-PnPFile -FileName user.jpg -Folder "Shared Documents"
```

Takes the user profile photo of the specified user and uploads it to the specified library in SharePoint Online.

### Example 8
```powershell
$task = Invoke-PnPGraphMethod -Url "https://graph.microsoft.com/v1.0/planner/tasks/23fasefxcvzvsdf32e" # retrieve the task so we can figure out the etag which is needed to update the task
$task = Invoke-PnPGraphMethod -Url "planner/tasks/23fasefxcvzvsdf32e" # retrieve the task so we can figure out the etag which is needed to update the task
$etag = $task.'@odata.etag'
$headers = @{"If-Match"=$etag}
$content = @{"title"="My new task title"}
Invoke-PnPGraphMethod -Url "https://graph.microsoft.com/v1.0/planner/tasks/23fasefxcvzvsdf32e" -Method PATCH -Content $content -AdditionalHeaders $headers
Invoke-PnPGraphMethod -Url "planner/tasks/23fasefxcvzvsdf32e" -Method PATCH -Content $content -AdditionalHeaders $headers
```

This example retrieves a Planner task to find the etag value which is required to update the task. In order to update the task through call to the Microsoft Graph API we need to include an If-Match header with the value of the etag. It then creates the content to update, in this case the title of the task, and calls the PATCH method on the Graph end-point to update the specific task.

### EXAMPLE 9
```powershell
$batch = New-PnPBatch -RetainRequests
Invoke-PnPSPRestMethod -Method Get -Url "https://graph.microsoft.com/v1.0/users" -Batch $batch
Invoke-PnPSPRestMethod -Method Get -Url "https://graph.microsoft.com/v1.0/groups" -Batch $batch
Invoke-PnPSPRestMethod -Method Get -Url "users" -Batch $batch
Invoke-PnPSPRestMethod -Method Get -Url "groups" -Batch $batch
$response = Invoke-PnPBatch $batch -Details
$response
```
Expand Down
3 changes: 0 additions & 3 deletions src/Commands/Admin/AddHomeSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.Core.Model;
using System;

namespace PnP.PowerShell.Commands.Admin
{
Expand Down
2 changes: 0 additions & 2 deletions src/Commands/Admin/AddHubToHubAssociation.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;

namespace PnP.PowerShell.Commands.Admin
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/AddOrgAssetsLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using Microsoft.Online.SharePoint.TenantAdministration;
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/GetContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Model.SharePoint;
using System;
using System.Collections.Generic;
using System.Management.Automation;
Expand Down
3 changes: 0 additions & 3 deletions src/Commands/Admin/GetContainerType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Utilities;
using System;
using System.Collections.Generic;
using System.Management.Automation;

Expand Down
3 changes: 0 additions & 3 deletions src/Commands/Admin/GetDeletedContainer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Utilities;
using System;
using System.Collections.Generic;
using System.Management.Automation;

Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/GetHubSite.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/Admin/GetStorageEntity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Management.Automation;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Enums;
using System.Collections.Generic;
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/GetTenant.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using PnP.PowerShell.Commands.Model;
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Admin/GetTenantDeletedSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override void ExecuteCmdlet()
}
foreach (DeletedSiteProperties item in list)
{
WriteObject(new Model.SPODeletedSite(item, Detailed.ToBool(), AdminContext, this));
WriteObject(new Model.SPODeletedSite(item, Detailed.ToBool(), AdminContext));
}
if (!flag2 && flag3)
{
Expand All @@ -74,7 +74,7 @@ protected override void ExecuteCmdlet()
try
{
AdminContext.ExecuteQueryRetry();
WriteObject(new Model.SPODeletedSite(deletedSitePropertiesByUrl, Detailed.ToBool(), AdminContext, this));
WriteObject(new Model.SPODeletedSite(deletedSitePropertiesByUrl, Detailed.ToBool(), AdminContext));
}
catch (ServerException e) when (e.ServerErrorTypeName.Equals("Microsoft.SharePoint.Client.UnknownError", StringComparison.InvariantCultureIgnoreCase))
{
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/Admin/GetTenantInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Utilities.REST;
using System;
using System.Management.Automation;

Expand Down Expand Up @@ -33,11 +32,11 @@ protected override void ExecuteCmdlet()
}

WriteVerbose("Acquiring access token for Microsoft Graph to look up Tenant");
var graphAccessToken = TokenHandler.GetAccessToken(this, $"https://{Connection.GraphEndPoint}/.default", Connection);
//var graphAccessToken = TokenHandler.GetAccessToken(this, $"https://{Connection.GraphEndPoint}/.default", Connection);
var requestUrl = BuildRequestUrl();

WriteVerbose($"Making call to {requestUrl} to request tenant information");
var results = GraphHelper.Get<Model.TenantInfo>(this, Connection, requestUrl, graphAccessToken);
var results = this.RequestHelper.Get<Model.TenantInfo>(requestUrl);
WriteObject(results, true);
}

Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/GetTenantInternalSetting.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using PnP.PowerShell.Commands.Model;
Expand Down
2 changes: 0 additions & 2 deletions src/Commands/Admin/GrantHubSiteRights.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Admin
Expand Down
3 changes: 0 additions & 3 deletions src/Commands/Admin/NewTenantSite.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using Microsoft.Online.SharePoint.TenantManagement;
using Microsoft.SharePoint.Client;
using PnP.Framework;
using PnP.Framework.Entities;

using PnP.PowerShell.Commands.Base;
using System;
using System.Management.Automation;
using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands
{
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/RegisterAppCatalogSite.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;

Expand Down
2 changes: 0 additions & 2 deletions src/Commands/Admin/RegisterHubSite.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using PnP.Framework.Sites;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;

Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/RemoveContainerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;

namespace PnP.PowerShell.Commands.Admin
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/RemoveKnowledgeHubSite.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;

Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/RemoveStorageEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Enums;

namespace PnP.PowerShell.Commands
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/RenameTenantSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Model;
using PnP.PowerShell.Commands.Utilities;
using System;
using System.Collections.Generic;
using System.Management.Automation;
Expand Down
2 changes: 0 additions & 2 deletions src/Commands/Admin/RestoreDeletedContainer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Admin
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/SetBuiltInDesignPackageVisibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Administration;
using System;

namespace PnP.PowerShell.Commands.Admin
{
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/SetDisableSpacesActivation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using Microsoft.Online.SharePoint.TenantAdministration;
using PnP.PowerShell.Commands.Base.PipeBinds;
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/SetHideDefaultThemes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;

Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/SetOrgAssetsLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using Microsoft.Online.SharePoint.TenantAdministration;
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Admin/SetStorageEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Enums;

namespace PnP.PowerShell.Commands
Expand Down
2 changes: 0 additions & 2 deletions src/Commands/Apps/AddApplicationCustomizer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.Framework.Entities;

using PnP.PowerShell.Commands.Enums;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;

namespace PnP.PowerShell.Commands.Branding
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Apps/AddAzureADServicePrincipalAppRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AddAzureADServicePrincipalAppRole : PnPGraphCmdlet

protected override void ExecuteCmdlet()
{
var principal = Principal.GetServicePrincipal(this, Connection, AccessToken);
var principal = Principal.GetServicePrincipal(RequestHelper);

if (principal == null)
{
Expand All @@ -49,7 +49,7 @@ protected override void ExecuteCmdlet()

if (AppRole.AppRole == null)
{
var resource = ParameterSetName == ParameterSet_BYBUILTINTYPE ? ServicePrincipalUtility.GetServicePrincipalByBuiltInType(this, Connection, AccessToken, BuiltInType) : Resource.GetServicePrincipal(this, Connection, AccessToken);
var resource = ParameterSetName == ParameterSet_BYBUILTINTYPE ? ServicePrincipalUtility.GetServicePrincipalByBuiltInType(RequestHelper, BuiltInType) : Resource.GetServicePrincipal(RequestHelper);

if (resource == null)
{
Expand All @@ -69,7 +69,7 @@ protected override void ExecuteCmdlet()

WriteVerbose($"Adding app role {appRole.Value}: {appRole.DisplayName}");

var response = ServicePrincipalUtility.AddServicePrincipalRoleAssignment(this, Connection, AccessToken, principal, appRole);
var response = ServicePrincipalUtility.AddServicePrincipalRoleAssignment(RequestHelper, principal, appRole);
WriteObject(response, false);
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/Commands/Apps/GetApp.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

using System.Management.Automation;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.Framework.ALM;
using System;
using PnP.PowerShell.Commands.Enums;
using PnP.Framework.Enums;
using Microsoft.SharePoint.Client;

namespace PnP.PowerShell.Commands.Apps
{
Expand Down
7 changes: 3 additions & 4 deletions src/Commands/Apps/GetAzureADAppSitePermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Model;
using PnP.PowerShell.Commands.Utilities.REST;

namespace PnP.PowerShell.Commands.Apps
{
Expand Down Expand Up @@ -52,14 +51,14 @@ protected override void ExecuteCmdlet()
var accessToken = AccessToken;

// All permissions, first fetch just the Ids as the API works in a weird way that requesting all permissions does not reveal their roles, so we will request all permissions and then request each permission individually so we will also have the roles
var permissions = GraphHelper.GetResultCollection<AzureADAppPermissionInternal>(this, Connection, $"https://{Connection.GraphEndPoint}/v1.0/sites/{siteId}/permissions?$select=Id", accessToken);
var permissions = this.RequestHelper.GetResultCollection<AzureADAppPermissionInternal>($"v1.0/sites/{siteId}/permissions?$select=Id");
if (permissions.Any())
{
var results = new List<AzureADAppPermission>();
foreach (var permission in permissions)
{
// Request the permission individually so it will include the roles
var detailedApp = GraphHelper.Get<AzureADAppPermissionInternal>(this, Connection, $"https://{Connection.GraphEndPoint}/v1.0/sites/{siteId}/permissions/{permission.Id}", accessToken);
var detailedApp = this.RequestHelper.Get<AzureADAppPermissionInternal>($"v1.0/sites/{siteId}/permissions/{permission.Id}");
results.Add(detailedApp.Convert());
}

Expand All @@ -76,7 +75,7 @@ protected override void ExecuteCmdlet()
}
else
{
var results = GraphHelper.Get<AzureADAppPermissionInternal>(this, Connection, $"https://{Connection.GraphEndPoint}/v1.0/sites/{siteId}/permissions/{PermissionId}", AccessToken);
var results = RequestHelper.Get<AzureADAppPermissionInternal>($"v1.0/sites/{siteId}/permissions/{PermissionId}");
WriteObject(results.Convert());
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/Apps/GetAzureADServicePrincipal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ protected override void ExecuteCmdlet()
switch (ParameterSetName)
{
case ParameterSet_BYAPPID:
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByAppId(this, Connection, AccessToken, AppId);
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByAppId(RequestHelper, AppId);
break;
case ParameterSet_BYOBJECTID:
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByObjectId(this, Connection, AccessToken, ObjectId);
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByObjectId(RequestHelper, ObjectId);
break;
case ParameterSet_BYAPPNAME:
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByAppName(this, Connection, AccessToken, AppName);
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByAppName(RequestHelper, AppName);
break;
case ParameterSet_BYBUILTINTYPE:
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByBuiltInType(this, Connection, AccessToken, BuiltInType);
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByBuiltInType(RequestHelper, BuiltInType);
break;
case ParameterSet_ALL:
var servicePrincipals = ServicePrincipalUtility.GetServicePrincipals(this, Connection, AccessToken, Filter);
var servicePrincipals = ServicePrincipalUtility.GetServicePrincipals(RequestHelper, Filter);
WriteObject(servicePrincipals, true);
return;
}
Expand Down
Loading

0 comments on commit b611be0

Please sign in to comment.