1

I am updating a document and I try to check it out first. If it is already checked out by the current user I do nothing. If it is not checked out I check it out. For this I have the following code to get the checked out user -

var item = list.GetItemById(ItemId);
clientContext.Load(item.File, f => f.CheckedOutByUser.LoginName);
clientContext.ExecuteQuery();

Since the document is not checked out the server side object is null and 'ClientObject.m_setAsNull' base member is set to true. After checking out the document I try to get the 'CheckedOutByUser.LoginName' value using the following code

var item = list.GetItemById(ItemId);
clientContext.Load(item.File.CheckedOutByUser);
clientContext.ExecuteQuery();

This loads all the scalar properties of the 'User' object but the 'ClientObject.m_setAsNull' is not reset to false. Hence even though the LoginName property exists in the internal dictionarty I get the ServerObjectNullReferenceException.

Any idea how I should achieve what I want - Get the checkedout user when the value was null previously?

2

The code that you have provided works for me in SharePoint 2013.

You could also try to retrieve Checkout User by Id:

context.Load(item);
context.ExecuteQuery();
var checkoutUserValue = item["CheckoutUser"] as FieldUserValue;
if (checkoutUserValue != null)
{
     var checkoutUser = context.Site.RootWeb.GetUserById(checkoutUserValue.LookupId);
     context.Load(checkoutUser);
     context.ExecuteQuery();
     Console.WriteLine(checkoutUser.LoginName);
}

How to determine CSOM version

using (var context = new ClientContext(url))
{             
      context.ExecuteQuery();
      if (context.ServerVersion.Major == 16)
      {
           Console.WriteLine("SharePoint Online CSOM");
      }
}
  • Seems that this will not work against 2010. Can you confirm this? I get an error - Method "GetUserById" does not exist. I want to maintain common code for 2010 and 2013 and use the 2013 assemblies – alwayslearning Mar 20 '14 at 17:00
  • I conform that Web.GetUserById method is not available in SharePoint 2010 CSOM, so I believe this code only works in SPS 2013 – Vadim Gremyachev Mar 20 '14 at 17:06
  • do you know how I can get the SharePoint version via code so that I can have both pieces of code and can switch based on the version – alwayslearning Mar 20 '14 at 17:17
  • The answer has been updated to demonstrate how to determine CSOM version, ClientContext.ServerVersion Property gets version of the current SharePoint Server msdn.microsoft.com/en-us/library/… – Vadim Gremyachev Mar 20 '14 at 17:26
  • Thanks for your help so far. My problem still is maintaining a single code base that works against both 2010 and 2013. Can I use the 2013 Client Object Model assemblies against SP 2010? – alwayslearning Mar 21 '14 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.