halliba / az-pipelines-2-sharepoint

Azure DevOps extension - upload build files to SharePoint Online
GNU General Public License v3.0
9 stars 4 forks source link

[Idea] Additional Task to retrieve the DriveId #2

Closed pri-kise closed 2 years ago

pri-kise commented 2 years ago

Hi,

I had some problems figuring out the DriveID, since we are using drives of subsites. What do you think about an additional task that accepts the Url of the Object Library and sets the DriveId as a pipeline variable?

Here a small draft for getting the drive id by using powershell. I'm sure this can be achived by using javascript, too.

Install-Module Microsoft.Graph
Install-Module JWTDetails
Install-Module MSAL.PS
Import-Module MSAL.PS
Import-Module Microsoft.Graph
Import-Module JWTDetails
$azureApplicationId ="00000000-0000-0000-0000-000000000000"
$azureTenantId = "00000000-0000-0000-0000-000000000000"
$azurePassword = ConvertTo-SecureString "xxx" -AsPlainText -Force

$MsGraphToken = Get-MsalToken -ClientId $azureApplicationId -ClientSecret $azurePassword  -TenantId $azureTenantId -Scope 'https://graph.microsoft.com/.default' -ForceRefresh -ErrorAction Stop
$jwtPayload = Get-JWTDetails $MsGraphToken.AccessToken
$jwtPayload

Connect-MgGraph -AccessToken $MsGraphToken.AccessToken

$url = [uri]"https://mycompanyXXXXX.sharepoint.com/subsite/subsite/MyDocumentLibrary/"

$siteId = $url.Host
$subSiteNames = $url.AbsolutePath.Split("/",[System.StringSplitOptions]::RemoveEmptyEntries)
$subSiteNames
$driveId = ""

foreach ($subSiteName in $subSiteNames){    
    Write-Host "SubSiteName $($subSiteName)"
    Write-Host "SiteId $($siteId)"
    $subSite = Get-MgSubSite -SiteId $siteId | Where-Object { $_.Name -EQ $subSiteName }
    Get-MgSiteDrive -SiteId $siteId
    if ($null -eq $subSite)
    {
        Get-MgSiteDrive -SiteId $siteId
        $drive = Get-MgSiteDrive -SiteId $siteId -All | Where-Object { $_.Name -EQ $subSiteName }
        $driveId = $drive.Id
        Write-Host "DriveId $($driveId)"
    } else {
        Write-Host "SubSiteId $($subSite.Id)"    
        $siteId = $subSite.Id
    }
}
$driveId

Example to set a pipeline variable: https://stackoverflow.com/q/70425335/6068675

console.log('##vso[task.setvariable variable=sharepointDriveId;]', sharepointDriveId);
halliba commented 2 years ago

Hi,

definitely a good idea. I may implement this if I can find time for it.

Thank you.

halliba commented 2 years ago

Hi,

with v0.3.0 you can now just use the SharePoint document library url instead of the drive id, like so:

image

This works for document libs in "normal" sites, subsites and in the root sharepoint site. Just make sure, that you use the full URL, including the name of the library, but not any further paths like .../Shared%20Documents/Forms/AllItems.aspx. I missed that hint in the README, will fix that later.

pri-kise commented 2 years ago

Thanks for implementing this cool enhancement.

VassilisM commented 5 months ago

Hi. I have finally enough access to try again. But I got "itemNotFound". Does this have to do with this ticket and how I am giving it this "driveId: 'https://$(sharepoint_host)/$(sharepoint_site)/$(sharepoint_dir)/$(sharepoint_remoteDir)'" which is in fact a url (because I do not have access to get the ids)? UPDATE: I've split it to 'driveId' and 'targetFolder' but still nothing. I am using the "az-pipelines-2-sharepoint@0"/0.3.4. I will try using the code above meanwhile, to check if it bypasses security and actually returns some ids... Sorry, and thanks!