Forum Discussion

Wasikhan's avatar
Mar 27, 2024
Solved

Project server On-Prem - Get SharePoint connected sites.

Hi,

 

i need to retrieve SharePoint connected site for specific or loop through all the projects to get Site address from PowerShell. i would appreciate for the help.

 

 

Regards

Wasi

  • Hello Wasikhan ,

    If you just want to get the project site address for sites that are associated to projects, you could query the Odata API in PowerShell. I've not run this in years so it probably wont run / will need updating but hopefully this will help get you started:

    #add SharePoint Online DLL - update the location if required
    $programFiles = [environment]::getfolderpath("programfiles")
    add-type -Path $programFiles'\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll'
    
    #set the environment detail variables: username, password and pwaUrl
    $username = "username" 
    $password = "password"
    $securePass = ConvertTo-SecureString $password -AsPlainText -Force
    $pwaUrl = 'pwa url'
    
    $results1 = @()
    
    #set the Odata URL
    $url = $pwaUrl + "/_api/ProjectData/Projects()?`$Filter=ProjectType ne 7&`$Select=ProjectWorkspaceInternalUrl"
    
    write-host "Getting the list of Project Sites" -ForegroundColor Yellow
    
    while ($url){
    
        [Microsoft.SharePoint.Client.SharePointOnlineCredentials]$spocreds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePass);    
        $webrequest = [System.Net.WebRequest]::Create($url)
        $webrequest.Credentials = $spocreds
        $webrequest.Accept = "application/json;odata=verbose"
        $webrequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
        $response = $webrequest.GetResponse()
        $reader = New-Object System.IO.StreamReader $response.GetResponseStream()
        $data = $reader.ReadToEnd()
        $results = ConvertFrom-Json -InputObject $data
        $results1 += $results.d.results
    
        if ($results.d.__next){
            $url=$results.d.__next.ToString()
        }
        else {
            $url=$null
        }
    }
    
    $projectsites = $results1.ProjectWorkspaceInternalUrl
    
    write-host "List of Project Sites completed, there are: " $projectsites.count " Project Sites." -ForegroundColor Green
    write-host "Starting to process the list of project sites" -ForegroundColor Yellow

    You can use the SharePoint CSOM API, which will be simpler but that will return all sub webs in the site collection so you might find that some of the sub webs included are not linked to Projects in PWA.

    Hope that helps

    Paul

1 Reply

  • Hello Wasikhan ,

    If you just want to get the project site address for sites that are associated to projects, you could query the Odata API in PowerShell. I've not run this in years so it probably wont run / will need updating but hopefully this will help get you started:

    #add SharePoint Online DLL - update the location if required
    $programFiles = [environment]::getfolderpath("programfiles")
    add-type -Path $programFiles'\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll'
    
    #set the environment detail variables: username, password and pwaUrl
    $username = "username" 
    $password = "password"
    $securePass = ConvertTo-SecureString $password -AsPlainText -Force
    $pwaUrl = 'pwa url'
    
    $results1 = @()
    
    #set the Odata URL
    $url = $pwaUrl + "/_api/ProjectData/Projects()?`$Filter=ProjectType ne 7&`$Select=ProjectWorkspaceInternalUrl"
    
    write-host "Getting the list of Project Sites" -ForegroundColor Yellow
    
    while ($url){
    
        [Microsoft.SharePoint.Client.SharePointOnlineCredentials]$spocreds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePass);    
        $webrequest = [System.Net.WebRequest]::Create($url)
        $webrequest.Credentials = $spocreds
        $webrequest.Accept = "application/json;odata=verbose"
        $webrequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
        $response = $webrequest.GetResponse()
        $reader = New-Object System.IO.StreamReader $response.GetResponseStream()
        $data = $reader.ReadToEnd()
        $results = ConvertFrom-Json -InputObject $data
        $results1 += $results.d.results
    
        if ($results.d.__next){
            $url=$results.d.__next.ToString()
        }
        else {
            $url=$null
        }
    }
    
    $projectsites = $results1.ProjectWorkspaceInternalUrl
    
    write-host "List of Project Sites completed, there are: " $projectsites.count " Project Sites." -ForegroundColor Green
    write-host "Starting to process the list of project sites" -ForegroundColor Yellow

    You can use the SharePoint CSOM API, which will be simpler but that will return all sub webs in the site collection so you might find that some of the sub webs included are not linked to Projects in PWA.

    Hope that helps

    Paul

Resources