Programmatically access SharePoint file MetaData

My goal is to write a VB script that will let me upload a file and set the custom metadata/properties of the file.  So I started with a simple Windows Form application that permits me to upload files into SharePoint.  Here's a code snippet of the important logic:

Dim context As New Microsoft.SharePoint.Client.ClientContext("http://OurSharepoint/")
context.Credentials = New System.Net.NetworkCredential("username", "password")

Dim fileUrl As String = "/Library/Folder/filename.pdf"
Dim fs As New System.IO.FileStream("C:\filename.pdf", FileMode.Open, FileAccess.Read)
Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fileUrl, fs, True)

Question #1: Is there a better way to upload the file than using SaveBinaryDirect()?

Question #2: How do I retrieve the metadata fields that this particular file/library has?

Question #3: How do I set the values of these metadata fields for this file?

September 1st, 2015 11:39am

Hi,

The following code for your reference:

Public Function UploadDocument(ByVal localFile As String, _
                ByVal remoteFile As String) As String
    '// Read in the local file
    On Error GoTo handler
    Dim r As Byte()
    Dim Strm As System.IO.FileStream = New System.IO.FileStream(localFile, _
                System.IO.FileMode.Open, System.IO.FileAccess.Read)
    Dim reader As System.IO.BinaryReader = New System.IO.BinaryReader(Strm)
    Dim filecontents As Byte() = reader.ReadBytes(CInt(Strm.Length))
    reader.Close()
    Strm.Close()
    Dim sSPURL As String = ConfigurationManager.AppSettings("SharePointServer")
    Dim sDocLib As String = ConfigurationManager.AppSettings("DocLibrary")
    Dim sUser As String = ConfigurationManager.AppSettings("User")
    Dim sPwd As String = ConfigurationManager.AppSettings("Pwd")
    Dim sDomain As String = ConfigurationManager.AppSettings("Domain")
    Dim sRemoteFileURL As String
    Dim NC As System.Net.NetworkCredential = _
        New System.Net.NetworkCredential(sUser, sPwd, sDomain)
    sRemoteFileURL = sSPURL & "/" & sDocLib & _
                     "/" & Trim(LTrim(RTrim(remoteFile)))
   
    sRemoteFileURL = Replace(sRemoteFileURL, " ", "%20")
    sRemoteFileURL = Replace(sRemoteFileURL, "\", "/")
    Dim m_WC As WebClient = New WebClient
    m_WC.Credentials = NC
    r = m_WC.UploadData(sRemoteFileURL, "PUT", filecontents)
    Return "TRUE"
    Exit Function
handler:
    Return Err.Description
End Function

Public Function WSSUpdateFile(ByVal sFileName As String, ByVal sSiteDoc As String, _
                              ByVal sTestCol As String) As String
    Dim sUser As String = ConfigurationManager.AppSettings("User")
    Dim sPwd As String = ConfigurationManager.AppSettings("Pwd")
    Dim sDomain As String = ConfigurationManager.AppSettings("Domain")
    Dim sFileIDinList As String
    Dim strBatch As String = ""
    sSiteDoc = Replace(sSiteDoc, "%20", " ")
    sSiteDoc = Replace(sSiteDoc, "\", "/")
    Dim sFinalFilePath As String
    Dim sSPURL As String = ConfigurationManager.AppSettings("SharePointServer")
    Dim sDocLib As String = ConfigurationManager.AppSettings("DocLibrary")
    Try
        Dim netAccess As System.Net.NetworkCredential = _
            New System.Net.NetworkCredential(sUser, sPwd, sDomain)
        Dim listService As New SPLists.Lists
        listService.Url = sSPURL & "/_vti_bin/lists.asmx"
        listService.Credentials = netAccess
        sFileIDinList = sGetID(listService.Url, sDocLib, sFileName)
        If sFileIDinList <> "" Then
            sFinalFilePath = sSPURL & "/" & sDocLib & "/" & sFileName
            'Now we have FileID so update the list
            strBatch = "<Method ID='1' Cmd='Update'>" + _
                "<Field Name = 'ID'>" & sFileIDinList & "</Field>" + _
                "<Field Name = 'FileRef'>" & sFinalFilePath & "</Field>" + _
                "<Field Name = 'TestCol'>" & sTestCol & "</Field>" + _
                "</Method>"
            Dim xmlDoc = New System.Xml.XmlDocument
            Dim elBatch As System.Xml.XmlElement = xmlDoc.createelement("Batch")
            elBatch.InnerXml = strBatch
            Dim ndreturn As System.Xml.XmlNode = _
                listService.UpdateListItems(sDocLib, elBatch)
        End If
        Return "TRUE"
    Catch ex As Exception
        Return ex.Message
    End Try
End Function

Private Function sGetID(ByVal sURL As String, ByVal sListGUID As String, _
                 ByVal sFileName As String) As String
    Dim sUser As String = ConfigurationManager.AppSettings("User")
    Dim sPwd As String = ConfigurationManager.AppSettings("Pwd")
    Dim sDomain As String = ConfigurationManager.AppSettings("Domain")
    Dim netAccess As System.Net.NetworkCredential = _
        New System.Net.NetworkCredential(sUser, sPwd, sDomain)
    Dim L As New SPLists.Lists
    L.Credentials = netAccess
    L.Url = sURL
    Dim xmldoc As XmlDocument = New XmlDocument
    Dim query As XmlNode = xmldoc.CreateNode(XmlNodeType.Element, "Query", "")
    query.InnerXml = "<OrderBy><FieldRef Name='Modified' " & _ 
                     "Ascending='False'></FieldRef></OrderBy>"""
    Try
        Dim caml As XmlNode = L.GetListItems(sListGUID, Nothing, query, _
                                             Nothing, "1", Nothing)
        Dim id As String = caml.ChildNodes(1).ChildNodes(1).Attributes("ows_ID").Value
        Return id
    Catch ex As Exception
        Return ex.Message
    End Try
End Function

More information is here:

Uploading files to the SharePoint Document Library and updating any metadata columns

http://www.codeproject.com/Articles/19717/Uploading-files-to-the-SharePoint-Document-Library

Best Regards,

Dennis

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 3:21am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics