How to "Check In" uploaded document on SharePoint?

284 Views Asked by At

I have VBA code that uploads a new document on SharePoint.
It uploads new row on SharePoint, but it doesn't start a workflow for new item, because it needs to be "Checked In".

When I do it manually, I click Upload, choose a file, fill required fields, and after that I press "Check In" which adds new item and automatically starts a workflow.

When I do it by VBA, it adds new item with described fields, but it doesn't start a workflow because it needs "Check In".

I tried with codes like that from official documentation of Microsoft

Sub CheckInOut(strWkbCheckIn As String) 
 
    ' Determine if workbook can be checked in. 
    If Workbooks(strWkbCheckIn).CanCheckIn = True Then 
        Workbooks(strWkbCheckIn).CheckIn 
        MsgBox strWkbCheckIn & " has been checked in." 
    Else 
        MsgBox "This file cannot be checked in " & _ 
          "at this time. Please try again later." 
    End If 
 
End Sub

When I open a workbook it's in read-only mode so I can't use the "Check In" method.

1

There are 1 best solutions below

3
Michael Han On

You could try the following to upload and check in files:

Dim oWebsite As Web = clientContext.Web
Dim folder As Folder = web.GetFolderByServerRelativeUrl("/sites/michael/Shared%20Documents")
Dim fileCreateInfo As FileCreationInformation = New FileCreationInformation()
fileCreateInfo.Overwrite = True
fileCreateInfo.Url = folder.ServerRelativeUrl & "/" & fileName
fileCreateInfo.Content = System.IO.File.ReadAllBytes(filePath)
Dim uploadFile As File = folder.Files.Add(fileCreateInfo)
If uploadFile.CheckOutType <> CheckOutType.None Then
    uploadFile.CheckIn("Initial Upload", CheckinType.MajorCheckIn)
End If

clientContext.Load(uploadFile)
clientContext.ExecuteQuery()