Open xml from Sharepoint document library in poweshell

I am trying to manipulate an xml file from a sharepoint document library using powershell. Unfortunately I can't open it and pass it to xml. :( I have the same file in the document library and locally on my hard drive. When I open it from the hard drive everything is fine. When I open it from the document library SharePoint, casting to xml occurs because of an extra character at the beginning of the file.I compared the binary with $ splistitem.File.OpenBinary () and with get-content C: .... \ file.xml and the same. The problem is getting a string from bytes. I tried all available encodings but nothing worked. Here is my code:

   # Add SharePoint snapin if needed
    if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue)     -eq $null)
    {
        Add-PSSnapin Microsoft.SharePoint.Powershell
    }

...
    $splistitem = ...
...

    $encode = New-Object System.Text.UTF8Encoding

    [xml]$xmlFromSharepoint = $encode.GetString($splistitem.File.OpenBinary()) #throws exception
    [xml]$xmlFromFile = get-content C:\....\file.xml #works fine

    $web.Dispose()

    Write-Host "Press any key to close ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

      

An exception is the message:

        Cannot convert value "?<?xml version="1.0" encoding="utf-8"?> 
..." to type "System.Xml.XmlDocument". Error: "Data at the
    root level is invalid. Line 1, position 1."
    At C:\aaa.ps1:14 char:24
    + [xml]$xmlFromSharepoint <<<<  = $encode.GetString($splistitem.File.OpenBinary
    ())
        + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMet
       adataException
        + FullyQualifiedErrorId : RuntimeException

      

Does anyone know how to fix the code above?

Thanks in advance!

0


source to share


1 answer


I answer myself. :)

The file infront character is a byte order mark for UTF8 encoding. I found that I can download the xml file directly from the sharepoint stream. This poses a problem. My goal was to spoof the connection string for a Reporting Services data source in a sharepoint library. The .rsds file is a regular xml file that contains a connection string. Here's the code to change it:

# Add SharePoint snapin if needed
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue)     -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.Powershell
}

#Configure Connection strings
$web = Get-SPWeb "http://localhost/c1"
$reportsList = $web.Lists | Where-Object { $_.Title -eq "Reports" }
$dataSource = $reportsList.Items | Where-Object { $_.Name -eq "Datasource.rsds" }
$dataSource.File.CheckOut();
$xml = New-Object System.Xml.XmlDocument
$stream = $dataSource.File.OpenBinaryStream();
$xml.Load($stream)
$xml.DataSourceDefinition.ConnectString = "test"
$stream.Position = 0;
$stream.SetLength(0);
$xml.Save($stream);
$dataSource.File.SaveBinary($stream)
$dataSource.File.CheckIn("");
$web.Dispose()

Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

      



Actually I figured out that the above code doesn't work. To change the reports, I have to use the ReportServer web service. Here's the working code now:

function ConfigureConnectionString
{
param($webUrl, $connectionString)
    $reportServerURI = "http://$serverUrl/ReportServer"
    $ReportPathWildCard = "/";
    $NameSharedSchedule="NeverExpireCache";

    $reportServerURI2010 = "$reportServerURI/ReportService2010.asmx?WSDL"
    $RS = New-WebServiceProxy -Class 'RS' -NameSpace 'RS' -Uri $reportServerURI2010 -UseDefaultCredential
    $dataSources = $RS.ListChildren($ReportPathWildCard,$true) | Where-Object {$_.TypeName -eq "DataSource" -and $_.Path -like "*$webUrl*"}
    foreach($ds in $dataSources) {
        $dsDefinition = $RS.GetDataSourceContents($ds.Path)

        $dsDefinition.CredentialRetrieval = "Store"
        $dsDefinition.Enabled = "TRUE";
        $dsDefinition.UserName = "domain\user";
        $dsDefinition.Password = "Password";
        $dsDefinition.ConnectString = $connectionString;

        #Update the DataSource with this new content
        $RS.SetDataSourceContents($ds.Path, $dsDefinition);
    }
}

      

+2


source







All Articles