Retrieving sharepoint lists using SOAP - sharepoint

I am trying to pull specific data points from a sharepoint list using VBA and SOAP. However, I am very new to both VBA and sharepoint, so there a few things that do not make sense to me. Currently, I'm trying to use this site as a reference:
http://depressedpress.com/2014/04/05/accessing-sharepoint-lists-with-visual-basic-for-applications/
I am having problems trying to locate where to find the credential information on the sharepoint site. Where would I go exactly to find the username, password, and SOAP URL?

I wrote the simple SharePoint list SOAP query below in VBA. I was able to run it within Excel and pull data from SharePoint. SOAP is not easy to work with as you need to have all the XML correct or it fails.
You can get a list of the services with examples by opening the /_vti_bin/Lists.asmx link on your SharePoint server, e.g.,
http://yourserver.com/_vti_bin/Lists.asmx And if you get a blank page then it means your version of SharePoint doesn't support SOAP web services.
Example: Queries the SharePoint UserInfo list for data using VBA
Sub spListQuery()
Dim webUrl, wsdl, action, soap, xhr As XMLHTTP60
itemId = 1
listName = "UserInfo"
webUrl = "http://yourserver.com" 'set to SharePoint site url
wsdl = "/_vti_bin/Lists.asmx"
action = "http://schemas.microsoft.com/sharepoint/soap/GetListItems"
soap = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<soap:Envelope " & _
"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _
"xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " & _
"xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
"<soap:Body>" & _
"<GetListItems xmlns=""http://schemas.microsoft.com/sharepoint/soap/"">" & _
"<listName>" & listName & "</listName>" & _
"<query><Query>" & _
"<Where><Eq><FieldRef Name=""ID""/><Value Type=""Integer"">" & itemId & "</Value></Eq></Where>" & _
"</Query></query>" & _
"<viewFields><ViewFields/></viewFields>" & _
"</GetListItems>" & _
"</soap:Body>" & _
"</soap:Envelope>"
Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xhr.Open "POST", webUrl & wsdl, False
xhr.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xhr.setRequestHeader "SOAPAction", action
xhr.Send soap
MsgBox xhr.Status & ":" & xhr.statusText & vbCrLf & xhr.responseText
End Sub

Related

How to assign the "table name" of a query in the powerpivot datamodel

I'm using PowerPivot, and I'm creating a table in the datamodel using the next code:
Set ObjConn = ActiveWorkbook.Connections.Add2( _
Name:= "SomeConnName", _
Description:="DescSomeConn", _
ConnectionString:="OLEDB;Provider=OraOLEDB.Oracle;Data Source=XXXX;User ID=YYYY;Password=ZZZ;Persist Security Info=true", _
CommandText:="SELECT * FROM mytable", _
lCmdtype:=xlCmdSql, _
CreateModelConnection:=True, _
ImportRelationships:=False)
It works well, except it always create the table named "Query".
I already searched in Microsoft Documentation and googled this way too much and I can't directly find where or how to set the table/query name.... or how to rename it after it was created.
Help is appreciated.
EDIT
Thanks to #QHarr guide, I changed my approach to this:
ActiveWorkbook.Queries.Add Name:="QueryXY09", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = OleDb.DataSource(""provider=OraOLEDB.Oracle.1;data source=MyDWHConn;"", [Query=""SELECT * FROM mytable""])" _
& Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
ActiveWorkbook.Connections.Add2 _
"Query - QueryXY09", "Connection to the 'QueryXY09' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=QueryXY09;Extended Properties=", _
"""QueryXY09""", 6, True, False
Which makes the goal, now I can set the name of the connection.
But this represents two problems. First data extraction is clearly slower, it seems it is using .net libraries, also, it looks like a double step.
Second, now I don't have a place to store the password, neither connection accept the user id/password for this approach. This last is the most worrisome, since the purpose of this is, of course, the automation of the extraction of data. However, I notice that, when I moved the file to a testing server, it prompted me for the user/password once, then I closed everything, open again and there was no prompt. So, where was the password saved?

Pulling ListBox Data, Text and HTMLrange as Email Body

So I will try and be as clear as possible.
I am trying to make a macro that will populate and email with the following things:
Values from Listboxes
Writing for the main message
and a Table of values (which I've currently got being populated as a HTML body.
For example, I would like the email body to look as follows:
"Dear" [Name from preselected listbox]
"In order that we compile the latest update for the NAV please can you arrange the following information to be provided for" [The date]
[LIST THROUGH HTML]
"Please can you provide this information by the folowinf date"/....
I've currently got the list being pulled through correctly, but that has then stopped the vba body being entered. Therefore, the following code only pulls through the list.
Dim Addressee As String, SenRan As Range ' Define the receipient as words
Addressee = Application.VLookup(SourceLiBo.Value, Sheet1.Range("A1:B1000"), 2, False) 'finds the email address for chosen name
Set SenRan = ThisWorkbook.Sheets("Assets").Range("A1").CurrentRegion 'Selects the range of assets to be emailed.
With OEmail
.To = Addressee 'Send to addressee
.Subject = "Information Request " & Format(Date, "mmmm")
.Body = "Dear " & Me.SourceLiBo & "," & Chr(10) & _
"In order that we can compile the latest update for the NAV, please can you arrange the following information to be provided for " & Format(Date, "mmmm") & ":" & Chr(10) & _
""
.HTMLBody = rangetoHTML(SenRan)
End With
How is the best way to go about this in order to have all the data pull through. I would set variables with the strings wanted for the body and input it through .HMTLBody, but would that also allow me to pull through th listbox values in HMTLBody
I have now reworded it, thanks to braX explaining my errors. The code below (with the Ron Bruin Function) provides the correct answer.
With OEmail
.To = Addressee 'Send to addressee
.Subject = "Information Request " & Format(Date, "mmmm")
.HTMLBody = "Dear " & Me.SourceLiBo & "," & _
"<br><br>" & _
"In order that we can compile the latest update for the NAV, please can you arrange the following information to be provided for " & Format(Date, "mmmm") & ":" & _
"<br><br>" & _
rangetoHTML(SenRan) & _
"<br><br>" & _
"Please let us know if there are any additional purchases not reflected in the list above." & _
"<br><br>" & _
"Please can you provide this information no later than 10 working days from the date of this email to allow us to process all updates for delivery." & _
"<br><br>" & _
"Many Thanks"
End With

Using "setClientCertificate" when there are multiple certificates installed

I wrote a WinHttp POST request in VBA. It works good as long as there is only one certificate installed on the computer. However, some users have multiple certs with similar certificate names and therefore it returns an error:
a certificate is required to complete client authentication
Any suggestions on how I can select the correct certificate when multiple certificates share similar names? I've tried using both the "friendly name" and the "CN" name of the cert.
Below is my code:
Sub dapull()
Dim URL As String: URL = "https://ce.midwest.org/dart/xml/query"
Dim mfile As String
pulldate = Format(Worksheets("Sheet2").Range("date").Value, "yyyy-mm-dd")
mfile = "<?xml version=" & """" & "1.0" & """" & "?><Envelope xmlns=" & """" & "http://schemas.xmlsoap.org/soap/envelope/" & """" & "><Header/><Body><QueryRequest xmlns=" & """" & "http://markets.midwest.org/dart/xml" & """" & "><QueryResults day=" & """" & pulldate & """" & "><Location>BART</Location></QueryResults></QueryRequest></Body></Envelope>"
Set Req = New WinHttp.WinHttpRequest
With Req
.Open "POST", URL, False
.SetClientCertificate "CURRENT_USER\MY\name" '*this is the issue line
.SetRequestHeader "content-type", "text/xml"
.Send (mfile)
.ResponseText
End With
End Sub
I have the same issue, did you manage to solve it?
VBA just picks the first one :( no way to list or to identify which is which (or at least sort by date or something else before picking up the certificate).
If you did it, please let me know how
For now I "solved" it by asking people to copy their own right certificate into Trusted People section and put into my XLSM an option to switch the store so that it is picked up from CURRENT_USER\TrustedPeople\ instead of MY store.
It works but it is not elegant as it needs the certificate to be manually re-copied every 6 or 12 months (but better than not working at all :) )

Import Sharepoint 2010 list data from Excel table using VBA

i have learned how to gather data from a sharepoint list into Excel using VBA simple macro only.
Now i would like to do the other way around - update some list in my Excel file, and send them back to sharepoint to update the list, using VBA only.
is that possible, and if yes - how?
Thanks!
Yes. You can use the XMLHttpRequest object provided by Microsoft's XML SDK, as well as the UpdateListItems web service provided by SharePoint to update one or more items. Add a reference to "Microsoft XML, v6.0" in the Tools -> References menu in your Visual Basic Editor, and then use something like the code below.
Dim objXMLHTTP As MSXML2.XMLHTTP
Dim strListNameOrGuid As String
Dim strBatchXml As String
Dim strSoapBody As String
Set objXMLHTTP = New MSXML2.XMLHTTP
strListNameOrGuid = "My List Name or GUID"
' Delete item with internal ID of "1"
strBatchXml = "<Batch OnError='Continue'><Method ID='1' Cmd='Delete'><Field Name='ID'>1</Field></Method></Batch>"
objXMLHTTP.Open "POST", "http://myserver/mysite/_vti_bin/Lists.asmx", False
objXMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
objXMLHTTP.setRequestHeader "SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"
strSoapBody = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " _
& "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " _
& "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><UpdateListItems " _
& "xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>" & strListNameOrGuid _
& "</listName><updates>" & strBatchXml & "</updates></UpdateListItems></soap:Body></soap:Envelope>"
objXMLHTTP.send strSoapBody
If objXMLHTTP.Status = 200 Then
' Do something with response
End If
Set objXMLHTTP = Nothing
You can read more about the syntax of the UpdateListItems and how the batch XML should be structured by going here.
Not the answer to your question but maybe interesting enough. You can pull data from a Sharepoint list with PowerQuery,

update sql query stops updating excel spreadsheet

I have a classic ASP web application that updates a table in a Excel spreadsheet.
I am using the following to connect to the Excel file
Set rows = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Server.MapPath("data.xlsx") & ";" & _
"Extended Properties=" & Chr(34) & "Excel 12.0 Xml;HDR=Yes" & Chr(34) & ";"
That is a sample query that I am executing
query = "UPDATE [Sheet1$] SET [STATUS] = 'unclaimed', [CLAIMER] = '' ,[SDATE] = '"
& DATE & "' WHERE [JUR] = '" & in_jur & "' AND [WC] = " & in_wc
objConnection.Execute query,numRows,adExecuteNoRecords
The above query gets executed in the process.asp page when the user clicks on a button that calls a javascript function that sends the value of in_jur and in_wc like this
function sendForProcessing() {
somejur = //use the document.getElementByID to get selected jur
somewc = //use the document.getElementByID to get selected wc
window.location = process.asp?in_jur=somejur&in_wc=somewc
}
The application updates the sheet correctly for some time (i counted the number of times it works. 1st test: 10 times 2nd test: 16 times it just varies) and then it stops working.
I do not get error message in the objConnection.Execute.
When i print out the numRows it prints 1.
Is there a better way to update the row in Excel?

Resources