tobania / VSTS.Extension.SqlReportingServices

VSTS Extension for uploading SSRS Reports
MIT License
14 stars 20 forks source link

No option to update data set #42

Open ghost opened 6 years ago

ghost commented 6 years ago

On data sources, there is the option to either update the data source or include the data source. Update will update the reference, while include will actually upload the data source.

Can we do this for datasets also? Right now, there is only the option to upload the dataset. It won't update the reference to the shared dataset like it does for datasources

mjcarrabine commented 6 years ago

I am running into the same issue.

I normally put all the shared datasets into a separate folder than the reports so this feature is required.

mjcarrabine commented 6 years ago

When a report is uploaded, and the datasource doesn't exist at the expected location, it throws a warning, and the extension is able to update it's reference by looking up the datasource by name.

When a dataset is uploaded, and the datasource doesn't exist at the expected location, it throws a warning. However, the name is always DataSetDataSource so I had to get the name from the xml of the dataset.

The code below shows two ways of updating the datasource reference on the dataset.

I hope this helps!

##########################################################
#                   Uploading datasets                   #
##########################################################

    $rsdFiles = @(Get-ChildItem $DataSetLocalPath);
    $rsdFileCount = $rsdFiles.Length;
    if($IncludeDataSet -eq $true){
        Write-Host "Uploading $rsdFileCount Dataset files";
        $rsdFiles | ForEach-Object {
            $datasetName = $_.FullName; 
            $datasetFileName = [System.IO.Path]::GetFileNameWithoutExtension($datasetName);
            Write-Host "Uploading dataset $datasetName to $DataSetRootPath...";
            Verbose-WriteLine "Reading $datasetName file...";
            [xml]$rsd = Get-Content -Path $datasetName;
            $byteRsd = Get-Content -Encoding Byte -Path $datasetName
            $warnings = $null;
            try{
                $dataset = $ssrs.CreateCatalogItem(
                    "DataSet",
                    $datasetFileName,
                    $DataSetRootPath,
                    $true,
                    $byteRsd,
                    $null,
                    [ref]$warnings
                );

                #If any warning was logged during upload, log them to the console
                if($warnings -ne $null){
                    Write-Warning "One or more warnings occured during upload:";
                    $warningSb = New-Object System.Text.StringBuilder;
                    $warnings | ForEach-Object{
                        $txtWarning = $_.Message;
                        $warningSb.AppendLine("`t- {$txtWarning}");
                    }
                    Write-Warning $warningSb.ToString();
                }

                $dataSetDataSourceReference = $rsd.SharedDataSet.DataSet.Query.DataSourceReference

                if($UpdateDataSource -eq $true){ #Update the datasources
                    Write-Host "Updating the DataSources of the dataset $datasetFileName...";

                    $serverDataSources = $ssrs.ListChildren($DataSourceRootPath, $true) | Where {$_.TypeName -eq "DataSource"};

                    # Using Get/SetDataSources
                    $neededDataSources2 = $ssrs.GetItemDataSources($DataSetRootPath + "/" + $datasetFileName);
                    $neededDataSources2 | ForEach-Object{
                        $reportDataSourceName = $_.Name;

                        Foreach($serverDataSource in $serverDataSources){
                            if([System.String]::Compare($serverDataSource.Name.Trim(),$dataSetDataSourceReference, $true) -eq 0){ # The Name of the neededDataSource is always DataSetDataSource so we need to use $dataSetDatasourceReference here
                                $dataSourcePathNew = $serverDataSource.Path;

                                Write-Host "Updating DataSource '$reportDataSourceName' to path '$dataSourcePathNew'..." -NoNewline;                                                            

                                $dataSourceReferenceNew = New-Object("$type.DataSourceReference");
                                $dataSourceReferenceNew.Reference = $dataSourcePathNew;

                                $dataSourceNew = New-Object ("$type.DataSource");
                                $dataSourceNew.Name = "DataSetDataSource";
                                $dataSourceNew.Item = $dataSourceReferenceNew;

                                $ssrs.SetItemDataSources($DataSetRootPath + "/" + $datasetFileName, $dataSourceNew);

                                Write-Host "Done!";
                                break;
                            }
                        }
                    }

                                        #http://bboydaisuke.blogspot.com/2010/08/how-to-set-shared-datasource-reference.html?m=1
                    ## Using Get/SetItemReferences
                    #$neededDataSources = $ssrs.GetItemReferences($DataSetRootPath + "/" + $datasetFileName, "DataSet");
                    #$neededDataSources | ForEach-Object{
                    #   $reportDataSourceName = $_.Name;                    

                    #   Foreach($serverDataSource in $serverDataSources){
                    #       if([System.String]::Compare($serverDataSource.Name.Trim(),$dataSetDataSourceReference, $true) -eq 0){ # The Name of the neededDataSource is always DataSetDataSource so we need to use $dataSetDatasourceReference here
                    #           $dataSourcePathNew = $serverDataSource.Path;

                    #           Write-Host "Updating DataSource '$reportDataSourceName' to path '$dataSourcePathNew'..." -NoNewline;                                                            

                    #           $dataSourceReferenceNew = New-Object("$type.ItemReference");
                    #           $dataSourceReferenceNew.Name = "DataSetDataSource";
                    #           $dataSourceReferenceNew.Reference = $dataSourcePathNew;

                    #           $ssrs.SetItemReferences($DataSetRootPath + "/" + $datasetFileName, $dataSourceReferenceNew);

                    #           Write-Host "Done!";
                    #           break;
                    #       }
                    #   }
                    #}

                }       

            }catch [System.Exception]{
                Write-Error $_.Exception.Message;
                #Terminate script
                exit -1;
            }
        }
    }
anton-kirschhock commented 5 years ago

Ill look into this and keep you posted