Hello All,
Perhaps someone out there can help with this assumingly simple task of getting the document id of a file that was uploaded.
Back ground:
I have a separate system which retains records of transactions. Paperwork is often received as a part of these transactions and my intention is to store those documents into SharePoint. I want to store the document id on the record to facilitate easy retrieval of the document. No need to search by meta tags or the like; I will have the specific document id which will point to the document.
Environment:
I currently have SharePoint 2013 online. The program that creates and maintains the records is an in-house VB.Net application running on 4.5 framework. The document repository is defined as a record center and contains several content types and meta tags (columns) already defined. Again the application is written in VB.
Goal:
Upload a given document, get the document id, and store that id to the record.
What I have done:
I have created a function that will take a given file name and proceed to upload the document to the given SharePoint list. It then proceeds to update the meta tags with information that will help find it using alternative methods. I have also spend hours
upon hours researching every corn of every forum to find an answer. But to no avail.
The problem:
I cannot find how to get the document id of the document that I uploaded. Either it is so easy that no-one has bothered to post how it is done. Or it is not doable. I say this because I can find absolutely no reference to how to get the document id. There is lots of references on how to get the name, the url, the meta tags, etc... But NO example on getting the document id. Very frustrating. Below is the code that I am using. Can someone suggest how I can go about getting the document id. Please treat me like I am a noob and spell it out. Sample code would be great! I have already burnt way too much time on this seemingly simple task. I would greatly appreciate any help.
Thanks
Mike
Imports Microsoft.VisualBasic
Public Class Class1
Private mVarTitle As String
Private mVarStockCode As String
Public Property DocTitle() As String
Get
Return mVarTitle
End Get
Set(value As String)
mvartitle = value
End Set
End Property
Public Property StockCode() As String
Get
Return mVarStockCode
End Get
Set(value As String)
mVarStockCode = value
End Set
End Property
Public Function UploadFile(FileName As String, Path As String, ListName As String, ContentType As String)
'For each record, copy the file from knowledgetree to the local directory and then upload it with the metatags.
Dim mKTFile As String = String.Empty
Dim mFileName As String = String.Empty
Dim mSource As String = String.Empty
Dim mTarget As String = String.Empty
Dim mTitle As String = String.Empty
Dim mStockCode As String = String.Empty
Dim mDocId As String = String.Empty
Dim mCTid As String = String.Empty
Dim mBaseKTFolder As String = "\\<computerName>\c$\Program Files\ktdms\documents\"
mFileName = FileName
mKTFile = Path
mTitle = mVarTitle
mStockCode = mVarStockCode
mKTFile = Replace(mKTFile, "/", "\")
mSource = mBaseKTFolder & mKTFile
mTarget = ".\temp\" & mFileName
UpdateStatus("uploading " & lrows.ToString)
' Copy the file to a new folder and rename it.
My.Computer.FileSystem.CopyFile(
mSource,
mTarget,
Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
Dim mpassword As New System.Security.SecureString()
Dim mp As String = "<adminPassword>"
For k = 1 To Len(mp)
mpassword.AppendChar(Mid(mp, k, 1))
Next
'*************************************************************************************************
Dim mycred As New Microsoft.SharePoint.Client.SharePointOnlineCredentials("<AdminUserId>", mpassword)
Dim url As String = "https://<SharePoint URL>/sites/rms"
Dim listTitle As String = ListName
Using clientContext = New ClientContext(url)
clientContext.Credentials = mycred
Using fs = New System.IO.FileStream(mTarget, System.IO.FileMode.Open)
Dim fi = New System.IO.FileInfo(mTarget)
Dim list = clientContext.Web.Lists.GetByTitle(listTitle)
clientContext.Load(list.RootFolder)
clientContext.ExecuteQuery()
Dim fileUrl = [String].Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name)
fileUrl = Replace(fileUrl, "#", " ")
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, True)
Dim web As Web = clientContext.Web
Dim newFile As Microsoft.SharePoint.Client.File = web.GetFileByServerRelativeUrl(fileUrl)
clientContext.Load(newFile)
clientContext.ExecuteQuery()
'*****this section finds the content type and sets
Dim contentTypeColl As ContentTypeCollection = clientContext.Web.ContentTypes
clientContext.Load(contentTypeColl)
clientContext.ExecuteQuery()
mCTid = String.Empty
'/ Display the content type names that are available in the website
For Each ct As ContentType In contentTypeColl
If ct.Name = ContentType Then
mCTid = ct.Id.StringValue
End If
Next
'***************End content type find.
newFile.ListItemAllFields("ContentTypeId") = mCTid
newFile.ListItemAllFields("Title") = mTitle
newFile.ListItemAllFields("StockCode") = mStockCode
'******This is the section where I have tried to get the uploaded document Id.
'******None of these statements work.
mDocId = newFile.ListItemAllFields("Document ID").ToString
mDocId = newFile.ListItemAllFields("_dlc_DocId").ToString
mDocId = newFile.ListItemAllFields("DocID").ToString
newFile.ListItemAllFields.Update()
clientContext.Load(newFile)
clientContext.ExecuteQuery()
End Using
End Using
' Delete the file .
My.Computer.FileSystem.DeleteFile(mTarget)
UploadFile = mDocId
End Function
End Class