Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Programmatically Check in\Check out documents in Sharepoint 2010

Programmatically Check in\Check out documents in Sharepoint 2010

In this post we will see an example of how to check out and check in one or all the documents in a SharePoint 2010 document library programmatically.

I have created a console application to the task

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["Mydocumentlibrary"]; -> Your Document library name

foreach (SPFile file in docs.RootFolder.Files)
{
if (file.CheckOutType == SPFile.SPCheckOutType.None)
{
file.CheckOut(); -> Checking out the file
}
}

// Getting the above Checked Out file.
foreach (SPCheckedOutFile file in docs.CheckedOutFiles)
{
Console.WriteLine(file.LeafName); -> Writing names of checked out files
}

// Check in and add a comment.
foreach (SPFile file in docs.RootFolder.Files)
{
if (file.CheckOutType != SPFile.SPCheckOutType.None)
{
file.CheckIn(“Programmatically Checked In”); -> checking In and adding a comment
}
}}}

No comments:

Post a Comment