Wednesday, November 11, 2020

Check Out and In Document Library in SharePoint using ECMAScript, C#

Due to the closure of "the blog TechNet" I transfer all my articles in the blog :)

Checking out — and its counterpart, checking in — is just good document library etiquette. What better way to let others know that you’re making changes to a document than by checking it out? Checking out a document sets the Checked Out flag to Yes and stores the name of the person who checked out the document.
In this article add code how to check out and check in file from a document library in SharePoint server using ECMAScript or C#. I have a document library named "Shared Documents" which has the following document.

Pre-requisites:
1. SharePoint server 2007 and higher,
2. Visual studio (only using C#).

Check Out


Check In

JavaScript:
<script type="text/javascript">
var list;
var item;
var file;
//Check Out document
  function fileCheckOut() {
   var clientContext = SP.ClientContext.get_current();
   if (clientContext != undefined && clientContext != null) {
   var webSite = clientContext.get_web();
   this.list = webSite.get_lists().getByTitle("Shared Documents");
   this.item = list.getItemById(1);
   this.file = this.item.get_file();
   this.file.checkOut();
   clientContext.load(this.file)
   clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
    }
}
//Check In document
  function fileCheckIn() {
   var clientContext = SP.ClientContext.get_current();
   if (clientContext != undefined && clientContext != null) {
   var webSite = clientContext.get_web();
   this.list = webSite.get_lists().getByTitle("Shared Documents");
   this.item = list.getItemById(1);
   this.file = this.item.get_file();
   this.file.checkIn();
   clientContext.load(this.file)
   clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
    }
}
  function OnLoadSuccess(sender, args) {
   alert("Successfully operation!");
   window.location = window.location.pathname;
}
  function OnLoadFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
<input id="btnFileCheckOut" onclick="fileCheckOut()" type="button" value="File Check Out" />
<input id="btnFileCheckIn" onclick="fileCheckIn()" type="button" value="File Check In" />

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace CheckinCheckoutDemo
{
class MyDemo
{
static void Main(string[] args)
{
	using (SPSite site = new SPSite("http://SPSite"))
	{
		using (SPWeb web = site.OpenWeb())
		{
			SPDocumentLibrary docs = (SPDocumentLibrary)web.Lists["Shared Documents"];

			foreach (SPFile file in docs.RootFolder.Files)
			{
				if (file.CheckOutType == SPFile.SPCheckOutType.None)
				{
				file.CheckOut();
				}
			}
			// Getting the above Checked Out file.
			foreach (SPCheckedOutFile file in docs.CheckedOutFiles)
			{
				Console.WriteLine(file.LeafName); 
			}
			// Check in and add a comment.
			foreach (SPFile file in docs.RootFolder.Files)
			{
				if (file.CheckOutType != SPFile.SPCheckOutType.None)
				{
				file.CheckIn("Programmatically Checked In"); 
			}
		}
	}
}

Happy Coding!

No comments:

Post a Comment