This document describes how to check whether a document in a SharePoint shared document library is checked out before downloading the document.

Introduction

Instances may arise where an attempt to access or update documents in SharePoint fails because the documents have already been checked out by another user. The K2.net process would therefore be unable to perform an update to the documents.

This condition can be handled as follows:

  1. Add a default Activity to your K2.net process
  2. Add a SharePoint 2003 Document Event to the Activity and then step through the Wizard providing all the necessary information to "Get" and "Check out" a document.
  3. Once the wizard has completed, open the properties of the SharePoint 2003 Document Event
  4. Generate the necessary code and select the "Use code" radio button
    1. The generated code should look similar to the code sample shown below except for the underlined sections which you must add manually.
    2. The code sample below will first download the specified document to the specified location on the local computer and then check-out the document in SharePoint if it is not currently checked out.
  5. If you want to see whether the document is checked out BEFORE you download it to the specified location, place the ### Download the Document ### section inside the sample (below) block as indicated in the comments:
ElseIf ErrorMessage.Length() = 0 Then
See the code sample as shown below:
 

VB .NET Code Sample

Public Sub Main(ByRef K2 As ServerEventContext)
' Set up variables
Dim Temp as String, ErrorMessage As String = ""
Dim Server As String
Server = "http://sps.k2mega.local"
 
If Not Server.EndsWith("/") Then Server &= "/"
 
Dim Site As String
Site = "sites/sales"
 
If Not Site.EndsWith("/") And Site.Trim <> "" Then Site & = "/"
 
Dim Folder As String
Folder = "Shared Documents"
If Not Folder.EndsWith("/") Then Folder &= "/"
 
Dim File As String
File = "test.txt"
 
Dim LocalFolder As String
LocalFolder = "C:\Documents and Settings\Administrator\Desktop"
 
Dim LocalFile As String
LocalFile = "newtest.txt"
 
Dim SpsList As New K2SPSList()
' Set Url for Web Service
SpsList.Url = Server & "_vti_bin/K2SpsList.asmx"
 
' Set Credentials
Dim SpsUtils As New SourceCode.K2SPUtilities.SPSUtilities
SpsList.Credentials = SpsUtils.GetCredentials(Server)
 
'##########################################################
'# Download the document
'##########################################################
Dim oByte() As Byte
 
' Call Web Service to Download Document
If Not SpsList.GetDocument(Server, Site, Folder, _ File, oByte, ErrorMessage) Then ' Error Occurred in GetDocument - Raise Error
Throw New System.Exception(ErrorMessage)
End If
 
Dim K2B64 As New K2Base64.K2Base64()
' Build The file path
Dim LocalFilePath As String
If Not LocalFolder.EndsWith("\") Then
 LocalFilePath = LocalFolder & "\" & LocalFile
Else
 LocalFilePath = LocalFolder & LocalFile
End If
 
K2B64.ByteArrayToFile(oByte, LocalFilePath)
 
'##########################################################

'##########################################################
'# Check out the document
'##########################################################
 
' Call Web Service to check out Document
If Not SpsList.CheckOutDocument(Site, Folder, _ File, ErrorMessage)
' Error Occurred in CheckOutDocument - Raise Error
 
' If the document has been checked out by somebody else,
' ErrorMessage will typically contain:
'  The file "Shared Documents/test.txt" is checked out or
'  locked for editing by K2MEGA\administrator.
If ErrorMessage.IndexOf("The file", 0) = 0 And _
ErrorMessage.IndexOf("is checked out or locked for editing", 0) > 0 Then
 
' Here you can typically set a process variable which can be
' tested in the Succeeding Rule of the Activity.
Console.WriteLine("The document is checked out or locked")
 
ElseIf ErrorMessage.Length() = 0 Then
 
' If you want to download the document ONLY if the document
' is not checked out, put the # Download the document # code
' here.
 
Else
 Throw New System.Exception(ErrorMessage)
 End If
End If
 
'##########################################################
End Sub