Solved

Excel VBA SharePoint List

Posted on 2013-11-09
4
3,857 Views
Last Modified: 2013-11-10
Hi,

I would like to programmatically ( using Excel VBA )  populate the "Description" e.g. "This list contains contact details"  of a SharePoint list which has a name, say, "ListABC".
      Lists               Description
     ListABC         This list contains contact details

Would appreciate it very much if you can help out with the VBA code.

Environment:  Excel 2010 and SharePoint 2010

Thank you
0
Comment
Question by:Russellbrown
  • 2
  • 2
4 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 39636097
Hi,

this sub can do the job (using SharePoint web service):
Sub UpdateListDescrption()
   ' Just activate first sheet for later usage of the cell values
    Worksheets(1).Activate

    Dim sURL As String
    Dim sEnv As String
    Dim xmlhtp As New MSXML2.XMLHTTP
    Dim xmlDoc As New DOMDocument
   ' Change HERE and update your SharePoint url
    sURL = "http://dev.rainerj.local/sites/ee/_vti_bin/lists.asmx"
    sEnv = "<?xml version=""1.0"" encoding=""utf-8""?>"
    sEnv = sEnv & "<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/"">"
    sEnv = sEnv & "  <soap:Body>"
    sEnv = sEnv & "    <UpdateList xmlns=""http://schemas.microsoft.com/sharepoint/soap/"">"
    sEnv = sEnv & "      <listName>" & Range("A2").Value & "</listName>"
    sEnv = sEnv & "      <listProperties><List Description=""" & Range("B2").Value & """/></listProperties>"
    sEnv = sEnv & "    </UpdateList>"
    sEnv = sEnv & "  </soap:Body>"
    sEnv = sEnv & "</soap:Envelope>"
     
    With xmlhtp
        .Open "post", sURL, False
        .setRequestHeader "Host", "dev.rainerj.local"
        .setRequestHeader "Content-Type", "text/xml; charset=utf-8"
        .setRequestHeader "soapAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateList"
        .send sEnv
        MsgBox .responseText
        'xmlDoc.Save "D:\RainerJ\EE\WebQueryResult.xml"
    End With
End Sub

Open in new window


In your Excel VBA add a reference to "Microsoft XML 6".
Then just update the SharePoint url as well as the request header to fit your environment and simply adjust the two cell values:
- The first one is the NAME of the list
- The second one the new description

HTH
Rainer
0
 

Author Comment

by:Russellbrown
ID: 39636478
Hi Rainer,

Thank you for the prompt response. It works great once again. Just a bit of fine-tuning. In the description the text maybe have "reserve character" e.g. "This list contains contact details
& other matters". The code is not able to parse "&". Is there a work around please?

Otherwise it is all perfect.

Kind regards,
Russ
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39636641
Hi,
ah, sorry.
You will have to HTML/XML encode values like ampersands, quotes, lower/greater than ...

Therefore I just wrote a small helper function:
Function EncodeForXML(sToEncode) As String
   Dim sEncoded As String
   If IsNull(strXmlValue) Then
     EncodeForXML = ""
   Else
     sEncoded = CStr(sToEncode)
     ' 1. Replace the ampersand (as all other replacements will create ampersands as well
     sEncoded = Replace(sEncoded, "&", "&amp;")
     ' 2. Replace single quote
     sEncoded = Replace(sEncoded, "'", "&apos;")
     ' 3. Replace double quote
     sEncoded = Replace(sEncoded, """", "&quot;")
     ' 4. Replace lower
     sEncoded = Replace(sEncoded, "<", "&lt;")
     ' 5. Replace greater
     sEncoded = Replace(sEncoded, ">", "&gt;")
     ' Return the encoded value
     EncodeForXML = sValue
   End If
End Function

Open in new window


Now just call this function at the place where you set the description like
sEnv = sEnv & "      <listProperties><List Description=""" & EncodeForXML(Range("B2").Value) & """/></listProperties>"

Open in new window


HTH
Rainer
0
 

Author Comment

by:Russellbrown
ID: 39637790
Perfecto! Thank you very much, Rainer.

I  did minor adjustments ( lines 4 and 19 ) to align the variables and get function to run:
Function EncodeForXML(sToEncode) As String
   Dim sEncoded As String
   
   If IsNull(sToEncode) Then ' strXmlValue align variable #1
     EncodeForXML = ""
   Else
     sEncoded = CStr(sToEncode)
     ' 1. Replace the ampersand (as all other replacements will create ampersands as well
     sEncoded = Replace(sEncoded, "&", "&amp;")
     ' 2. Replace single quote
     sEncoded = Replace(sEncoded, "'", "&apos;")
     ' 3. Replace double quote
     sEncoded = Replace(sEncoded, """", "&quot;")
     ' 4. Replace lower
     sEncoded = Replace(sEncoded, "<", "&lt;")
     ' 5. Replace greater
     sEncoded = Replace(sEncoded, ">", "&gt;")
     ' Return the encoded value
     EncodeForXML =sEncoded  ' sValue   align variable # 2
   End If
End Function

Open in new window


Best regards,
Russ
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now