We help IT Professionals succeed at work.

How can I checkin\checkout a document using the sharepoint client object model

Is there a way to checkin and checkout a document using the sharepoint client object model and vb.net/c# code?
Comment
Watch Question

Commented:
Yes, almost anything is possible via the SharePoint object model.

Check out SPFile.CheckOut Method - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.checkout.aspx
Top Expert 2011

Commented:
Elliott WardSoftware Developer

Author

Commented:
Thank you those examples were some what helpful.

I see that the file object has a method for checking in/out of a file, but how do I instanctiate the file object in the first place in order to do the check-in and out?  An example would be great if possible.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Elliott WardSoftware Developer

Author

Commented:
I created this function based on the above links (see attached code). It fails however with a error code of:

     PropertyOrFieldNotInitializedException

So I obviously missed something.
   
Function CheckOutDocument(LibraryName As String, DocumentName As String) As Boolean
   Dim fileURL As String = String.Format("{0}{1}\{2}", m_siteURL, LibraryName, DocumentName)
   Dim file As SP.File = Nothing

   Try
     file = m_rootWeb.GetFileByServerRelativeUrl(fileURL)
     If file.Exists Then
        file.CheckOut()
        Return True
    End If
    Return False
Catch ex As Exception
    Return False
End Try
    End Function

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
Are you able to get to the file using that URL in browser?
Elliott WardSoftware Developer

Author

Commented:
I ended up replacing the above code with the following example that does appear to work. I does however loop through all of the list elements instead of just retrieving the one I need, which is my next step. I must confess at this point I am not sure how to do that.

The code is listed below, please advise if there is something wrong or there is a better way.
Function CheckOutDocument(LibraryName As String, DocumentName As String) As Boolean
    Dim info As New IO.FileInfo(DocumentName)
    Dim thisList As SP.List = Nothing
    Dim query As New SP.CamlQuery
    Dim AllItems As ListItemCollection = Nothing

    Try
        'Prepare the list
        thisList = m_rootWeb.Lists.GetByTitle(LibraryName)
        m_clientContext.Load(thisList)

       'Prepare a query for all items in the list
       query.ViewXml = "<View/>"
       AllItems = thisList.GetItems(query)
       m_clientContext.Load(AllItems, Function(items) items.include(Function(item) item.DisplayName))

      'Execute the prepared commands against the target ClientContext
      m_clientContext.ExecuteQuery()

      For Each item As SP.ListItem In AllItems
          Dim Text As String = item.DisplayName.ToLower
          If Text = info.Name.Replace(info.Extension, "").ToLower Then
             item.File.CheckOut()
             m_clientContext.ExecuteQuery()
             Return True
          End If
      Next
      Return False
  Catch ex As Exception
      Return False
  End Try
End Function

Open in new window

Most Valuable Expert 2012
Top Expert 2014
Commented:
Yeah it should be fine if it works but you can also try to use caml queries(line 13)  to reduce the number of items to loop or you can also change function in line 15 to include fewer items.
Elliott WardSoftware Developer

Author

Commented:
Thanks mate.