using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Runtime.InteropServices;

namespace CloseFile
{
    class Program
    {
      
        static void Main(string[] args)
        {
             
            Console.WriteLine("Please input the file url:");
            string weburl = Console.ReadLine();
            using (SPSite site = new SPSite(weburl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                  
                    SPListItem file = web.GetListItem(weburl);
                    string status = file.File.CheckOutStatus.ToString();
                    switch (status)
                    {
                        case "None":
                             Console.WriteLine(file.Name  + " didn't open,you should edit it!");
                            break ;
                        case "ShortTerm":
                            Console.WriteLine(file.Name  + " is locked");
                            break;
                        case "LongTerm":
                            Console.WriteLine(file.Name  + " is checked out");
                            break;
                        case "LongTermOffline":
                            Console.WriteLine(file.Name  + " is checked out");
                            break;
                    }

                 
                  if (status!="None")
                  {
                      Console.WriteLine("Please input '1' to unlock the file,or any key to exit.");
                      string unlock = Console.ReadLine();
                      if (unlock == "1")
                      {

                          if (file.File.CheckOutStatus.ToString() == "ShortTerm")
                          {
                              try
                              {
                                  UpdateItemCheckoutExpiration(file);
                              }
                              catch (Exception ex)
                              {
                                  Console.Write(ex.Message);
                                  return;
                              }

                          }
                          if (file.File.CheckOutStatus.ToString() == "LongTerm" || file.File.CheckOutStatus.ToString() == "LongTermOffline")
                          {
                              file.File.UndoCheckOut();
                              file.Update();
                          }
                      }
                      Console.WriteLine("Waiting for processing......");
                      System.Threading.Thread.Sleep(2000);
                  }
                  status = file.File.CheckOutStatus.ToString();
                  switch (status)
                  {
                      case "None":
                          Console.WriteLine(file.Name + "  status is ok now!");
                          break;
                      case "ShortTerm":
                          Console.WriteLine(file.Name + " is locked");
                          break;
                      case "LongTerm":
                          Console.WriteLine(file.Name + " is checked out");
                          break;
                      case "LongTermOffline":
                          Console.WriteLine(file.Name + " is checked out");
                          break;
                  }

                   Console.WriteLine("Press any key to exit.");
                   Console.ReadKey();
                }
              
            }
        }


        private static void UpdateItemCheckoutExpiration(SPListItem item)
        {
            SqlConnection contentDatabaseConnection = null;
            try
            {
                contentDatabaseConnection = new SqlConnection(item.Web.Site.ContentDatabase.DatabaseConnectionString);
                contentDatabaseConnection.Open();

                string UpdateCommandText = string.Format("UPDATE dbo.AllDocs SET CheckoutExpires = '{0:yyyy-MM-dd HH:mm:ss:fff}' WHERE Id = '{1}'", DateTime.Now.ToUniversalTime(), item.UniqueId.ToString());
                SqlCommand UpdateCommand = new SqlCommand(UpdateCommandText, contentDatabaseConnection);
                SqlDataAdapter contentDataAdapter = new SqlDataAdapter();
                contentDataAdapter.UpdateCommand = UpdateCommand;
                contentDataAdapter.UpdateCommand.ExecuteNonQuery();
                contentDatabaseConnection.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (contentDatabaseConnection != null && contentDatabaseConnection.State != ConnectionState.Closed)
                    contentDatabaseConnection.Close();
            }
        }     
       

    }
}