0

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.

Community
  • 1
  • 1
Yanger
  • 3
  • 1
  • 2
  • @clomee What do You mean? What settings exactly? – Yanger Jan 21 '21 at 13:25
  • If You mean that it opens in read only mode - then it's not fault of excel, but looks like sharepoint automatically do that for uploaded files... but i can do it manually by sharepoint site, click there "Check In" and thats what makes me mad about it.. – Yanger Jan 21 '21 at 13:32

1 Answers1

0

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()
Michael Han
  • 3,475
  • 1
  • 6
  • 8
  • Thank You for a reply. I pasted Your code but it shows on red some lines, are there any references required? I will post here my code, maybe that will help us. Very important thing here is that this sharepoint is not just a library where i put a files, but it's a place where we put documents by "Upload" button. I had big problems to make that, but after few days i found some code on google and i use it now. Here it is: https://social.msdn.microsoft.com/Forums/SECURITY/en-US/5965619e-8d58-42f0-9504-d27b1eaf6b5b/vba-code-to-connect-to-sharepoint-online?forum=sharepointdevelopment – Yanger Jan 22 '21 at 15:10
  • Yes, you need to intall the package: https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/ – Michael Han Jan 25 '21 at 05:52
  • Everyone to use this vba code has to install the package? or is it just one thing to do when writing code? it's not official, right? – Yanger Jan 25 '21 at 11:54