Saturday 2 February 2008

Programmaticaly checkin all documents in a document library

Today i wrote some small peace of code but it saved me a lot of (click) work. I uploaded 200 documents to a document library but all documents were checked out. I could do this boring job manualy but as a Sharepoint developer i wrote a quick console application to do the job.

This is the code (you can copy past it to you console application):

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace CheckinDocuments
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please fill in the web url : ");
string webUrl = Console.ReadLine();

Console.WriteLine("Please fill in the name of the document library : ");
string documentLibraryName = Console.ReadLine();

if (webUrl != "")
{
using (SPSite site = new SPSite(webUrl))
{

SPWeb web = site.OpenWeb();
SPFile file = null;

//select the correct document library
foreach (SPList list in web.Lists)
{
if (list.Title.ToLower() == documentLibraryName.ToLower())
{
//loop all files and checkin if needed
foreach(SPListItem item in list.Items)
{
try
{

file = item.File;

if (file != null)
{
if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
file.CheckIn("some comment");
file.Update();
Console.WriteLine("File checked-in = " + file.Name);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Could not check-in file = " + file.Name + " - ERROR = " + ex.ToString());
}
}
}
}
}
}
Console.WriteLine("All documents are checked in, press enter to finish this program.");
Console.ReadLine();
}
}
}

14 comments:

Anonymous said...

Why didn't you use Manage Content And Structure? :p

Bart Vandenheede said...

That's also a possible solution if you have few documents in one folder. If you have a document library with 3000 documents in 100 different folders and subfolders then i should prefer this solution instead of clicking.

2000MPH said...

Great, nice little piece of code, that will be very handy.

Cheers.

Gene Vangampelaere said...

thx Bart!

Daigoro said...

You can't use "Manage content and structure" in this case, cause "there is no check-in version of these items".

Anonymous said...

hey
Does any one know how to check out multiple documents in Sharepoint 2007?

P.S I need this asap

cheers

Anonymous said...

Hi,

I begin on internet with a directory

Anonymous said...

As being a rule of thumb, I prefer to go by lifting weights to build muscle
each and every other day.

Also visit my site: English (US)

Anonymous said...

The item includes an adjustable options which means you can easily choose your
desired lifting fat between 5 to fifty two.

my blog post; used dumb bells

Anonymous said...

There are actually a substantial number of people who need to get in shape, but
you should not much like the thought of figuring
out in manifeste If you belong to that team maybe you've been informed that a superb alternative would be to get yourself a body weight bench and work out at your home.

My web page bowflex selecttech 552 dumbbells

Anonymous said...

Quite simply, it's a good idea to participate in a range of weight-bearing work outs.

Stop by my website - bowflex dumbbells 552

Anonymous said...

These swift transform dumbbells from Bowflex appear which has a
2 yr guarantee on pieces, 5 years fat plates and 1
12 months guarantee for labor.

Here is my blog ... simply click the up coming web site

Anonymous said...

The foam will cushion your arms, absorb a number
of the sweat, and protect your arms from nicks and scratches inside the plastic.


Here is my blog post - click through the up coming web site

Anonymous said...

If your about to box you have got to perform common
boxing physical exercises and rely upon all those to have
you in the form you need to be in to the ring.

Also visit my page just click the following page