diff --git a/src/Commands/Base/PipeBinds/SearchExternalConnectionPipeBind.cs b/src/Commands/Base/PipeBinds/SearchExternalConnectionPipeBind.cs index 5360c7645..35f96a934 100644 --- a/src/Commands/Base/PipeBinds/SearchExternalConnectionPipeBind.cs +++ b/src/Commands/Base/PipeBinds/SearchExternalConnectionPipeBind.cs @@ -23,6 +23,11 @@ public SearchExternalConnectionPipeBind(string identity) _identity = identity; } + public string GetExternalConnectionId(PSCmdlet cmdlet, PnPConnection connection, string accessToken) + { + return _identity ?? _searchExternalConnection?.Id ?? GetExternalConnection(cmdlet, connection, accessToken)?.Id; + } + public Model.Graph.MicrosoftSearch.ExternalConnection GetExternalConnection(PSCmdlet cmdlet, PnPConnection connection, string accessToken) { if(_searchExternalConnection != null) diff --git a/src/Commands/Search/GetSearchExternalConnection.cs b/src/Commands/Search/GetSearchExternalConnection.cs index 7c5caae22..6e8d2d090 100644 --- a/src/Commands/Search/GetSearchExternalConnection.cs +++ b/src/Commands/Search/GetSearchExternalConnection.cs @@ -13,7 +13,7 @@ namespace PnP.PowerShell.Commands.Search [OutputType(typeof(Model.Graph.MicrosoftSearch.ExternalConnection))] public class GetSearchExternalConnection : PnPGraphCmdlet { - [Parameter(Mandatory = false)] + [Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true)] public string Identity; protected override void ExecuteCmdlet() diff --git a/src/Commands/Search/GetSearchExternalItem.cs b/src/Commands/Search/GetSearchExternalItem.cs index cb92f7762..216d12536 100644 --- a/src/Commands/Search/GetSearchExternalItem.cs +++ b/src/Commands/Search/GetSearchExternalItem.cs @@ -25,7 +25,7 @@ public class GetSearchExternalItem : PnPGraphCmdlet protected override void ExecuteCmdlet() { - var externalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken); + var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId)); var searchQuery = new Model.Graph.MicrosoftSearch.SearchRequests { @@ -39,7 +39,7 @@ protected override void ExecuteCmdlet() ], ContentSources = [ - $"/external/connections/{externalConnection.Id}" + $"/external/connections/{externalConnectionId}" ], Query = new Model.Graph.MicrosoftSearch.SearchRequestQuery { @@ -59,11 +59,11 @@ protected override void ExecuteCmdlet() if(hits == null || hits.Count == 0) { - WriteVerbose($"No external items found{(ParameterSpecified(nameof(Identity)) ? $" with the identity '{Identity}'" : "")} on external connection '{externalConnection.Id}'"); + WriteVerbose($"No external items found{(ParameterSpecified(nameof(Identity)) ? $" with the identity '{Identity}'" : "")} on external connection '{externalConnectionId}'"); return; } - WriteVerbose($"Found {hits.Count} external item{(hits.Count != 1 ? "s" : "")}{(ParameterSpecified(nameof(Identity)) ? $" with the identity '{Identity}'" : "")} on external connection '{externalConnection.Id}'"); + WriteVerbose($"Found {hits.Count} external item{(hits.Count != 1 ? "s" : "")}{(ParameterSpecified(nameof(Identity)) ? $" with the identity '{Identity}'" : "")} on external connection '{externalConnectionId}'"); var externalItems = hits.Select(s => new Model.Graph.MicrosoftSearch.ExternalItem { Id = s.Resource.Properties["fileID"].ToString()[(s.Resource.Properties["fileID"].ToString().LastIndexOf(',') + 1)..], diff --git a/src/Commands/Search/GetSearchExternalSchema.cs b/src/Commands/Search/GetSearchExternalSchema.cs index b1449fb57..ef648645f 100644 --- a/src/Commands/Search/GetSearchExternalSchema.cs +++ b/src/Commands/Search/GetSearchExternalSchema.cs @@ -15,8 +15,8 @@ public class GetSearchExternalSchema : PnPGraphCmdlet protected override void ExecuteCmdlet() { - var searchExternalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken); - var graphApiUrl = $"v1.0/external/connections/{searchExternalConnection.Id}/schema"; + var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId)); + var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}/schema"; var result = Utilities.REST.GraphHelper.Get(this, Connection, graphApiUrl, AccessToken, additionalHeaders: new System.Collections.Generic.Dictionary { { "Prefer", "include-unknown-enum-members" } }); WriteObject(result, false); } diff --git a/src/Commands/Search/RemoveSearchExternalConnection.cs b/src/Commands/Search/RemoveSearchExternalConnection.cs index 47c7e7f4c..e0abbeb8f 100644 --- a/src/Commands/Search/RemoveSearchExternalConnection.cs +++ b/src/Commands/Search/RemoveSearchExternalConnection.cs @@ -14,8 +14,8 @@ public class RemoveSearchExternalConnection : PnPGraphCmdlet protected override void ExecuteCmdlet() { - var externalConnection = Identity.GetExternalConnection(this, Connection, AccessToken); - Utilities.REST.GraphHelper.Delete(this, Connection, $"v1.0/external/connections/{externalConnection.Id}", AccessToken); + var externalConnectionId = Identity.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(Identity)); + Utilities.REST.GraphHelper.Delete(this, Connection, $"v1.0/external/connections/{externalConnectionId}", AccessToken); } } } \ No newline at end of file diff --git a/src/Commands/Search/RemoveSearchExternalItem.cs b/src/Commands/Search/RemoveSearchExternalItem.cs index 0ba337245..0295a6108 100644 --- a/src/Commands/Search/RemoveSearchExternalItem.cs +++ b/src/Commands/Search/RemoveSearchExternalItem.cs @@ -21,16 +21,16 @@ public class RemoveSearchExternalItem : PnPGraphCmdlet protected override void ExecuteCmdlet() { - var connection = ConnectionId.GetExternalConnection(this, Connection, AccessToken); + var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId)); try { - var response = GraphHelper.Delete(this, Connection, $"beta/external/connections/{connection.Id}/items/{ItemId}", AccessToken); - WriteVerbose($"External item with ID '{ItemId}' successfully removed from external connection '{connection.Id}'"); + var response = GraphHelper.Delete(this, Connection, $"beta/external/connections/{externalConnectionId}/items/{ItemId}", AccessToken); + WriteVerbose($"External item with ID '{ItemId}' successfully removed from external connection '{externalConnectionId}'"); } catch (PSInvalidOperationException ex) { - throw new PSInvalidOperationException($"Removing external item with ID '{ItemId}' from external connection '{connection.Id}' failed with message '{ex.Message}'", ex); + throw new PSInvalidOperationException($"Removing external item with ID '{ItemId}' from external connection '{externalConnectionId}' failed with message '{ex.Message}'", ex); } } } diff --git a/src/Commands/Search/SetSearchExternalConnection.cs b/src/Commands/Search/SetSearchExternalConnection.cs index 2460439b0..306fb4ea9 100644 --- a/src/Commands/Search/SetSearchExternalConnection.cs +++ b/src/Commands/Search/SetSearchExternalConnection.cs @@ -41,8 +41,8 @@ protected override void ExecuteCmdlet() var jsonContent = JsonContent.Create(bodyContent, null, new JsonSerializerOptions { DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull }); WriteVerbose($"Constructed payload: {jsonContent.ReadAsStringAsync().GetAwaiter().GetResult()}"); - var externalConnection = Identity.GetExternalConnection(this, Connection, AccessToken); - var graphApiUrl = $"v1.0/external/connections/{externalConnection.Id}"; + var externalConnectionId = Identity.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(Identity)); + var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}"; Utilities.REST.GraphHelper.Patch(this, Connection, AccessToken, jsonContent, graphApiUrl); } } diff --git a/src/Commands/Search/SetSearchExternalItem.cs b/src/Commands/Search/SetSearchExternalItem.cs index 9e2f0062f..bae981ea4 100644 --- a/src/Commands/Search/SetSearchExternalItem.cs +++ b/src/Commands/Search/SetSearchExternalItem.cs @@ -105,8 +105,8 @@ protected override void ExecuteCmdlet() var jsonContent = JsonContent.Create(bodyContent); WriteVerbose($"Constructed payload: {jsonContent.ReadAsStringAsync().GetAwaiter().GetResult()}"); - var searchExternalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken); - var graphApiUrl = $"v1.0/external/connections/{searchExternalConnection.Id}/items/{ItemId}"; + var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId)); + var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}/items/{ItemId}"; var results = Utilities.REST.GraphHelper.Put(this, Connection, graphApiUrl, AccessToken, jsonContent); WriteObject(results, false); } diff --git a/src/Commands/Search/SetSearchExternalSchema.cs b/src/Commands/Search/SetSearchExternalSchema.cs index e4f4f2645..55691db0f 100644 --- a/src/Commands/Search/SetSearchExternalSchema.cs +++ b/src/Commands/Search/SetSearchExternalSchema.cs @@ -37,7 +37,7 @@ public class SetSearchExternalSchema : PnPGraphCmdlet protected override void ExecuteCmdlet() { - var searchExternalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken); + var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId)); switch(ParameterSetName) { @@ -53,7 +53,7 @@ protected override void ExecuteCmdlet() var jsonContent = new StringContent(SchemaAsText); WriteVerbose($"Constructed payload: {jsonContent.ReadAsStringAsync().GetAwaiter().GetResult()}"); - var graphApiUrl = $"v1.0/external/connections/{searchExternalConnection.Id}/schema"; + var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}/schema"; var results = Utilities.REST.GraphHelper.Patch(this, Connection, AccessToken, jsonContent, graphApiUrl); WriteVerbose("Trying to retrieve location header from response which can be used to poll for the status of the schema operation");