Updating a Sharepoint List from Powershell - sharepoint

I can modify a new datarow in Powershell but it will not update the Sharepoint List on the site itself.
Here is a bit of my code
Here i fill my dataset with table info
$connString = 'Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=2;RetrieveIds=Yes;DATABASE=https://sharepoint/;LIST={6d552622-3333-4444-9999-234d32d32d3};'
$spConn = new-object System.Data.OleDb.OleDbConnection($connString)
$spConn.open()
$qry="select * from myList"
$cmd = new-object System.Data.OleDb.OleDbCommand($qry,$spConn)
$da = new-object System.Data.OleDb.OleDbDataAdapter($cmd)
$dataSet = new-object System.Data.DataSet
$sp = $dataSet.Tables.Add("Table")
$da.fill($sp)
Here i add a new datarow
$row = $sp.NewRow()
$sp.Rows.Add($row)
$row["Title"] = "Foo"
And here i try to update the Sharepoint List
$da.Update($sp)
It does not let me update, any help or guidence would be great.
Thanks

You are adding a new row which is an insert. Inserts are not supported by the OleDB provider against a SharePoint list. You can select or update the value of an existing row, but not create a new row.

Related

Format Chart DataLabel in Powershell generated Excel File

Try to format datalabel font size with a powershell generated barchart, but does not work
Read the "whole" API for Chart.SeriesCollection for VBA and .NET. But it does not help. Is it a bug or have I a brain bug? Anyone who can help?
https://learn.microsoft.com/de-de/office/vba/api/excel.chart.seriescollection
My try (with different iterations about this)
$chart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange2.Font.Size = 18
Powershell Error Message: The property 'Size' cannot be found on this object. Verify that the property exists and can be set.
The whole short powershell script:
$excel = New-Object -comobject Excel.Application
$excel.Visible = $True
$workbook = $excel.Workbooks.Add()
$sheet = $excel.Worksheets.Item(1)
$sheet.Activate() | Out-NULL
$sheet.Cells.Item(1,1).Value2 = "City"
$sheet.Cells.Item(1,2).Value2 = "Citizens"
$sheet.Cells.Item(2,1) = "Offenbach"
$sheet.Cells.Item(2,2) = 111020
$sheet.Cells.Item(3,1) = "Heusenstamm"
$sheet.Cells.Item(3,2) = 18200
$sheet.Cells.Item(4,1) = "Rembruecken"
$sheet.Cells.Item(4,2) = 1850
$range = "A1:B4"
$chartSelect = $sheet.range($range)
$ch = $sheet.shapes.addChart().chart
$ch.chartType = 51
$ch.ApplyDataLabels(2)
$ch.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange2.Font.Size = 18
$ch.setSourceData($chartSelect)
Try this ?
1..3| %{$ch.SeriesCollection(1).DataLabels($_).Font.Size = 18}
There are a couple syntaxes that ought to work.
The old one which you need to select 'Show hidden members' in the VBIDE's Object Browser to see (as if it's been deprecated, but it works fine):
ActiveChart.SeriesCollection(2).DataLabels.Font.Size = 18
The new and improved and way more complex one:
ActiveChart.SeriesCollection(2).DataLabels.Format.TextFrame2.TextRange.Font.Size = 18
I'll let you convert from VBA to PowerShell.

Powershell - Create a Pivot Table in Excel

I have managed to open and adjust my file so far like this :
$Fichier = "XXXX\McAfeeWin10.csv"
$objExcel = New-Object -ComObject Excel.Application
$WorkBook = $objExcel.Workbooks.Open($Fichier)
$WorkSheet = $WorkBook.worksheets.item(1)
$objExcel.Visible = $true
$Range = $worksheet.UsedRange.Cells
$range.NumberFormat = "#"
$WorkSheet.Columns("A:C").AutoFit()
That file has 3 columns and looks like this :
Now all I want to do is create a pivot table with the settings like this :
Column = VerNoyau
Row = DATVer
Values = Poste
How can I do it?
I have found pivot table examples but they are very complex and involve creating the whole file. I'm working on an already existing file so I assume it would be simpler.

Is it possible to speed up an Excel query within PowerShell?

I am currently using the following query to add a specific row (with 10 columns) from an Excel spreadsheet (~1500 rows and hosted on SharePoint) to an array in PowerShell.
$connection = New-Object System.Data.OleDb.OleDbConnection
$connectstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$Source;Extended Properties='Excel 12.0 Xml;HDR=YES'"; $connection.ConnectionString = $connectstring
$connection.open()
$cmdObject = New-Object System.Data.OleDb.OleDbCommand
$query = "Select * from [Sheet1$]
WHERE [Sheet1$].[Test] = '$Test'"
$cmdObject.CommandText = $query
$cmdObject.CommandType = "Text"
$cmdObject.Connection = $connection
$oReader = $cmdObject.ExecuteReader()
[void]$oReader.Read()
$Global:oData = New-Object PSObject
$oData | Add-Member NoteProperty List1 $oReader[0]
...
$oData | Add-Member NoteProperty List10 $oReader[9]
$oReader.Close()
$cmdObject.Dispose()
$connection.Close()
$connection.Dispose()
This works absolutely fine, however it is often quite slow. Is there any way in which I could speed up the query? I can't add the entire excel sheet into an array, as the data changes throughout the day and is queried regularly.
I've found other questions, such as Speed up reading an Excel File in Powershell however that doesn't seem relevant to this particular issue.
Appreciate any help.

How to Insert Cells (not Entire Row/Column) in Excel using Powershell

As the title suggested, I'm not sure how to do the equivalent of selecting only a portion of a row and insert cells (shift rows down) using Powershell. It seems like all tutorials online are about inserting the entire row or entire column which is not what I want to do.
Any ideas?
Thanks.
Try something like this:
$xl = New-Object -COM "Excel.Application"
$xl.Visible = $true
$wb = $xl.Workbooks.Open "C:\path\to\your.xlsx"
$ws = $wb.Sheets.Item(1)
$ws.Range("C5:E9").Insert(-4121)

SharePoint 2010 - Add list item with workflow approval status and PowerShell

I want to add a item to a SharePoint 2010 list by using PowerShell.
This list is related with the standard SharePoint Approval Workflow.
I want to add Items and set the workflow status to "Published" with PowerShell.
My Code, but how it is possible to set the Workflow status to "Published"?
$web = Get-SPWeb $Url
$list = $web.Lists["MyList"]
$newitem = $Schulliste.items.Add()
$newitem["Column1"] = "Test1" # Works fine!
#Set Column Writable
$column = $list.Fields["WorkflowName"]
$column.Hidden = $false
$column.ReadOnlyField = $false
$column.Update()
#Update Workflow Item
$newitem["WorkflowName"] = "Published" #Not working
$newitem.update()
#Set Column Readonly
$column = $list.Fields["WorkflowName"]
$column.Hidden = $true
$column.ReadOnlyField = $true
$column.Update()
I think, setting the Status field is not the right way. You should approve the item programatically.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.approve.aspx
$newitem.File.Approve("approved by script")
My workaround now is to activate and deactivate the content approval
$web = Get-SPWeb $Url
$list = $Web.Lists["MyList"]
$list.EnableModeration = $false
$list.Update()
$newitem = $liste.items.Add()
$newitem["Column1"] = "Test1"
$newitem.update()
$list.EnableModeration = $true
$list.Update()

Resources