0

There is a note in the developer road map from December of 2013 saying, "Lock/Unlock – We’ve added support for locking and unlocking files into the V2 API."

I've been all through the V2 API (for c#) and cannot find it anywhere. I expected to find something in the BoxFilesManager class or as something you would pass to UpdateInformationAsync within the BoxFileRequest class.

So is there a way to lock/unlock a file?

kberson
  • 25
  • 3

2 Answers2

0

Great question. In order to see the current lock status of a file do a

GET https://api.box.com/2.0/files/7435988481/?fields=lock

If there is no lock on the file, you'll get something like this back:

{
    "type": "file",
    "id": "7435988481",
    "etag": "0",
    "lock": null
}

If you want to lock a file, you need to do a PUT (update) on the /files/ endpoint with a body that tells us what type of lock, and when to release it. Like this:

PUT https://api.box.com/2.0/files/7435988481/?fields=lock

{"lock": {
"expires_at" : "2014-05-29T19:03:04-07:00",
  "is_download_prevented": true
}
}

You'll get a response confirming your lock was created:

{
    "type": "file",
    "id": "7435988481",
    "etag": "1",
    "lock": {
        "type": "lock",
        "id": "14516545",
        "created_by": {
            "type": "user",
            "id": "13130406",
            "name": "Peter Rexer gmail",
            "login": "prexer@gmail.com"
        },
        "created_at": "2014-05-29T18:03:04-07:00",
        "expires_at": "2014-05-29T19:03:04-07:00",
        "is_download_prevented": true
    }
}
Peter
  • 2,551
  • 1
  • 14
  • 20
  • That's how to determine the status using GET/PUT. Is there a C# API call as well? – kberson Jun 02 '14 at 15:11
  • Uh... C# gurus of the world? Anyone feel like adding it to the SDK? Probably should file a feature request.. either https://github.com/box/box-windows-sdk-v2 or https://github.com/jhoerr/box-csharp-sdk-v2 – Peter Jun 06 '14 at 19:09
  • JHoerr is out, stopped maintain that library when Box came out with their own. – kberson Jun 08 '14 at 19:25
  • That's because JHoerr has started helping maintain the Box OSS one – Peter Jun 10 '14 at 18:53
  • @PeterSmith, I'm confused; I thought that the C# Box V2 API was maintained by Box. It sounds like NOT, if you're asking for help with it. – kberson Jun 12 '14 at 19:03
  • What does the GET look like if there is a lock? – kberson Jun 13 '14 at 14:24
  • @kberson GET response is shown in the answer above in the case of a lock (2nd) and without a lock (first response). Box JSON is roughly in-out symmetric. Box will ignore the fields you aren't allowed to set (like created_at). You'll get the same response from a GET as you do from the lock-creating PUT. – Peter Jun 13 '14 at 19:12
  • yep, sorry, brain fart that I didn't see the nose in front of my face. – kberson Jun 16 '14 at 13:35
0

Since there isn't a lock/unlock yet, I created a Lock Manager based on the existing managers:

class BoxCloudLockManager : BoxResourceManager
{
    #region Lock/Unlock Classes
    [DataContract]
    internal class BoxLockRequestInfo
    {
        [DataMember(Name = "status")]
        public string Status { get; set; }
        //[DataMember(Name = "expires_at")]
        //public string ExpiresAt { get; set; }
        [DataMember(Name = "is_download_prevented")]
        public bool IsDownloadPrevented { get; set; }
    }

    [DataContract]
    internal class BoxLockRequest
    {
        [DataMember(Name = "lock")]
        public BoxLockRequestInfo Lock { get; set; }
    }
    #endregion

    const string LockFileString = "{0}/?fields=lock";

    public BoxCloudLockManager(IBoxConfig config, IBoxService service, IBoxConverter converter, IAuthRepository auth) 
        : base(config, service, converter, auth)
    {
    }

    public async Task<BoxLockInfo> LockAsync(string documentId,bool isDownloadPrevented = true)
    {
        var lockRequest = new BoxLockRequest { Lock = new BoxLockRequestInfo { Status = "lock", IsDownloadPrevented = isDownloadPrevented } };

        BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
            .Method(RequestMethod.Put)
            .Payload(_converter.Serialize(lockRequest));

        IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);

        return response.ResponseObject;
    }

    public async Task<BoxLockInfo> UnlockAsync(string documentId)
    {
        BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
            .Method(RequestMethod.Put)
            .Payload("{\"lock\":null}");

        IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);

        return response.ResponseObject;
    }

    public async Task<BoxLockInfo> GetLockInfoAsync(string documentId)
    {
        BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
            .Method(RequestMethod.Get);

        IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);

        return response.ResponseObject;
    }
}

I derived a class from BoxClient, adding a LockManager and instantiate it within the Constructor.

Here is the Lock Info:

[DataContract]
public class BoxLockedBy
{
    [DataMember(Name = "type")]
    public string Type { get; set; }
    [DataMember(Name = "id")]
    public string Id { get; set; }
    [DataMember(Name = "name")]
    public string Name { get; set; }
    [DataMember(Name = "login")]
    public string Login { get; set; }
}

[DataContract]
public class BoxLockDetails
{
    [DataMember(Name = "type")]
    public string Type { get; set; }
    [DataMember(Name = "id")]
    public string Id { get; set; }
    [DataMember(Name = "created_by")]
    public BoxLockedBy CreatedBy { get; set; }
    [DataMember(Name = "created_at")]
    public string CreatedAt { get; set; }
    [DataMember(Name = "expires_at")]
    public string ExpiresAt { get; set; }
    [DataMember(Name = "is_download_prevented")]
    public bool IsDownloadPrevented { get; set; }
}

[DataContract]
public class BoxLockInfo
{
    [DataMember(Name = "type")]
    public string Type { get; set; }
    [DataMember(Name = "id")]
    public string Id { get; set; }
    [DataMember(Name = "etag")]
    public string Etag { get; set; }
    [DataMember(Name = "lock")]
    public BoxLockDetails LockDetails { get; set; }
}
kberson
  • 25
  • 3