RE: Checking to see if a document library has CheckOut set to on

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Peter,

You can checkout the document library by using following code:
if (item.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
item.File.CheckOut();
}

But the problem with above approach comes if you have enabled versioning on
document library.
Though your item gets updated successfully but it will create a new version
for the updated item in doc library.

Workaround for above versioning problem is:
item.SystemUpdate(false);

Do remember to set SPweb object AllowUnsafeUpdates property to true before
doing item update.

Amit

"Peter M" wrote:

Hi all,

I have some code that trawls through all document libraries and adds a new
field to it. It then fills in a value for all documents within the library.
The code works fine until it hits a document library that requires the
document to be checked out and then in crashes

Now I may have done this the wrong way (I have used SPItem instead of
SPFile) but I need to be able to check to see if the document needs to be
checked out or not and am struggling to sort it out quickly.

The application accepts two parameters, the site collection URL and then a
switch (/run, /norun and /backout)

Here is my code...

Cheers - Peter

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

namespace CustomSiteColumnLoad
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 2)
{
string siteUrl = args[0];
string strSwitch = args[1];

// Check for valid strSwitch values
if (strSwitch == "/norun" || strSwitch == "/run" ||
strSwitch == "/backout")
{
// do nothing
}
else
{
Console.WriteLine("Invalid argument: /switch \n\nSyntax:
sitespider http://sitename /switch\n\nValid /switch values : /norun /run
/backout");
return;
}

// Check to make sure siteUrl is a valid URL
if (siteUrl.Contains("http://";) == false)
{
Console.WriteLine("Invalid argument: siteurl \n\nSyntax:
sitespider http://sitename /switch\n\nValid /switch values : /norun /run
/backout");
return;
}
else
{
AddColumntoList(siteUrl, strSwitch);
}
}
if(args.Length == 1)
{
Console.WriteLine("Insufficient arguments: siteurl /switch
\n\nSyntax: sitespider http://sitename /switch\n\nValid /switch values :
/norun /run /backout");
return;
}
if (args.Length == 0)
{
Console.WriteLine("Missing arguments: siteurl /switch
\n\nSyntax: sitespider http://sitename /switch\n\nValid /switch values :
/norun /run /backout");
return;
}
}

public static void AddColumntoList(string siteColl, string sw)
{
string siteCollection = siteColl;
int listcount = 0;

using (SPSite sites = new SPSite(siteCollection))
{
// All site collections in parent web application

foreach (SPSite site in sites.WebApplication.Sites)
{
Console.WriteLine(site.Url);

// All web sites in site collections
foreach (SPWeb web in site.AllWebs)
{
if (web.ParentWeb == null)
{
Console.WriteLine(" " + web.Title);
}
else
{
// Indent web sites
Console.WriteLine(" " + web.Title + "
(a subsite of " + web.ParentWeb + ")");
}

// Display all lists within each web site
SPListCollection listcollection = web.Lists;
for (int i = 0; i < listcollection.Count; i++)
{
SPList list = listcollection[i];
if (list.BaseTemplate.ToString() ==
"DocumentLibrary")
{
if (list.Title == "Site Collection
Documents" || list.Title == "Site Collection Images" || list.Title == "Form
Templates" || list.Title == "Style Library" || list.Title == "Reporting
Templates")
{
// do nothing
}
else
{
Console.WriteLine(" " +
list.Title);

bool boolFieldExists = true;

// Check to see if the field is already
in the list
if
(list.Fields.ContainsField("Confidentiality Level") == false)
{
boolFieldExists = false;
}

if (boolFieldExists == true)
{
if (sw == "/backout")
{
list.Fields["Confidentiality
Level"].Delete();
Console.WriteLine(" *** /backout
switch active : Field deleted *** ");
}
else
{
Console.WriteLine(" *** Field
already exists, list ignored *** ");
}
}

if (boolFieldExists == false)
{
if (sw == "/run")
{
// Add Confidentiality Category
field
string fieldName =
list.Fields.Add("Confidentiality Level", SPFieldType.Choice, true);
SPFieldChoice categoryField =
(SPFieldChoice)list.Fields["Confidentiality Level"];


categoryField.Choices.Add("Public");

categoryField.Choices.Add("Internal");

categoryField.Choices.Add("Confidential");

categoryField.Choices.Add("Highly Confidential");
categoryField.DefaultValue =
"Highly Confidential";
categoryField.Description =
"Public: Non critical and is in the public domain.\n\nInternal: Not intended
to be disclosed outside of the Group.\n\nConfidential: Where loss or
disclosure could be detrimental to the Group, its employees or customers or
result in limited reputation or financial impact.\n\nHighly Confidential: The
loss disclosure or modification of the document could extensively damage the
Group, its employees or customers either in terms of reputation, financial or
other significant impact. This would include anything containing
personal/customer/staff data.\n\nFor further information:
http://transinterchangeco.lloydstsb.co.uk/cso/GIS/ITEC2/ITEC2_Default.shtm";;
categoryField.Update();
listcount++;

for (int x = 0; x <
list.Items.Count; x++)
{
// check for Check Out goes
here!

SPListItem document =
list.Items[x];

for (int y = 0; y <
document.Fields.Count; y++)
{
SPField field =
document.Fields[y];

if (field.Title ==
"Confidentiality Level")
{


document["Confidentiality Level"] = "Highly Confidential";
document.Update();

Console.WriteLine("*** Field set to 'Highly Confidential' ***");
}
}
}

Console.WriteLine(" ***
Confidentiality Level has been added to " + list.Title + " ***");
}
if (sw == "/norun")
{
foreach (SPItem document in
list.Items)
{
foreach (SPField field in
document.Fields)
{
if (field.Title ==
"ConfidentialityLevel")

Console.WriteLine("Found it!");
}
}

Console.WriteLine(" *** /norun
switch active : Confidentiality Level would have been added to " + list.Title
+ " ***");
return;
}
}
}
}
}
}
Console.WriteLine("\n");
}
}
Console.WriteLine("Number of lists updated: " +
listcount.ToString());
}
}
}

.



Relevant Pages

  • Checking to see if a document library has CheckOut set to on
    ... I have some code that trawls through all document libraries and adds a new ... // All web sites in site collections ... "Confidentiality Level") ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: Sharing of documents between document libraries
    ... Do you mean that you want to Share a document library with another WSS ... > document libraries. ... > We operate some project web sites via Windows SharePoint Services. ... > site includes libraries of sharing documents. ...
    (microsoft.public.sharepoint.windowsservices)
  • Notice about every change on doc. library
    ... we have Sharepoint portal server 2003 with about 100 web sites. ... presents architectonic project with 2 document libraries. ...
    (microsoft.public.sharepoint.portalserver)
  • Re: Sharing of documents between document libraries
    ... There's no add-on tool. ... view of the contents of a document library that is stored elsewhere in WSS. ... > document libraries. ... > We operate some project web sites via Windows SharePoint Services. ...
    (microsoft.public.sharepoint.windowsservices)