Showing posts with label MOSS. Show all posts
Showing posts with label MOSS. Show all posts

Wednesday, May 23, 2012

SPDocumentLibrary.CheckedOutFiles? A Common Misunderstanding

What is a checked out file?
Any file which locked for changes...so the changes are not visible to the other users. In SharePoint, we have a option to check out a file.
Go to the library, open a file's ECB (Edit Control Block) menu options and clik "Check Out" menu item.

So any file, which is checked out this way, is a checked out file. Isn't it? Yes, it is checked out file.

Now, try this.
Write a small console application, which will display count of SPDocumentLibrary.CheckedOutFiles. In case of SharePoint 2010, a small PowerShell script will do.

Execute the utility and note down the count of Checked Out files. Go to a document library and check out one file. Re-execute the utility and read the count...It does not change.Try again with two-three files. It does not change the count of SPDocuemntLibrary.CheckedOutFiles.

Why?
 MSDN describes "SPDocumentLibrary.CheckedOutFiles" as "the collection of files that are uploaded to the document library but are not checked in."

If the document library requires check out before editing any file and you upload a file to this library...the file will added in a checked out state. The file does not have a checked in version available. The file will not be visible to any one before check in.

SPDocumentLibrary.CheckedOutFiles represent the collection of files, which do not have a checked in version.

"Requires Check out" setting is available in "Version Settings" of library.

Can we override Check out of these files from Object Model?
SPDocumentLibrary.CheckedOutFiles is a collection of SPCheckedOutFile objects. SPCheckedOutFile does not derive from any class. SPCheckedOutFile does not have any member to check in the file.

SPCheckedOutFile has a property "ListItemId". If we exploit it to retrieve SPFile object, then we can check in the file. SPFile has a method "CheckIn". Code must be executed with the credentials of the user who has the ownership of these files. As the files are not visible to others, code with other user credentials fails to retrieve the SPFile object for these files.

SPCheckedOutFile type has a method "TakeOverCheckOut". If we use this method, you will have the file ownership and then we can retrieve the SPFile object for check in purpose.

Can we override check out of these files from UI?
These files are only visible to the user who has uploaded the files. Then how to override the check out for these files.
Go to the library settings, you will see a link "Manage Checked Out Files" in "Permissions and Management" category. MOSS 2007 and SharePoint 2010 has different versions of the link text as  follows.
MOSS 2007:
SPS 2010:
SharePoint 2010 corrected the confusing text.

So click on this link, and you can see the files which do not have checked in version. The UI provide the option to take the ownership of these files. Take the ownership of this files and from library view, you can check in/ delete the files. Taking ownership of these files, makes the files visible to you only and other users can not see the files.

Files checked out which have checked in version
Any file which is explictly checked out from the library...is visible to all with check out status. It can be retrieved as SPFile object. SPFile has properties to read the details of check out. These details are also presented in UI.

SPFile.CheckOutStatus property indicates whether the file is checked out or not. There are other members of SPFile which gives details about check out. e.g.CheckedOutBy, CheckedOutDate etc.

As the files are visible to all, we can easily override the check out of these files from UI or Object Model code.

To summarize
SPDocumentLibrary.CheckedOutFiles represents files which does not have checked version. This property does not count the files, which are explictly checked out. This property should not be used to report the number of checked out files.

Use loop in object model and count the files with CheckedOutStatus turned ON.  Then add the count of SPDocumentLibrary.CheckedOutFiles.

In UI, create a view of checked files, note the count of files. Visit "Manage Checked out files" link in library settings, count the files. Add the counts of these files.

Tuesday, August 2, 2011

SharePoint: Site Columns and Content Types

There are various posts on the internet describing the site columns and content types. In this post I am co-relating these with concepts of objects and classes. This will help the developer visualize these concepts differently.

Content and Object

Object has data. Class provides the set of attributes to detail the object instances.

Content is data. Content has two categories: Structured and Unstructured.

Content comprises of documents and list items. Content does not include list or library itself. List or library helps user to organize the content.

Structured content is the one which separates its storage from its display. e.g. List Items can be sorted/filtered and viewed in List web part or data row in a SQL data table.

Unstructured content is the one which cannot be viewed separately the format in which they are stored. e.g. Word document cannot be viewed without the MS Office Word.

Content type provides the set of attributes to define the metadata about content. The attributes are provided in the form of columns/fields in the list or at the site level.

Site Column

When the column is defined at site level, it is a site column. The site column is reusable across all lists and libraries in the site.

This is very similar to have property declared in an interface and will be available across all objects implementing the interface.

Content Type

Content type is a set of site columns. The site column helps in describing the content. The content type can be used across the lists and libraries in the site.

This is just like a complex data type. The data types/ classes has properties to describe the object.

Wednesday, April 13, 2011

Document Library Creation with Specific Document Template Using Server Object Model

Introduction: There is no way in SharePoint UI to create a document library with blank Excel/PowerPoint document template. But it is possible to create a document library with such document templates programmatically.

Technicalities:
I will first take you through the classes involved in development.


  • SPListCollection

  • SPListTemplateType

  • SPListTemplate

  • SPDocTemplate


SPListCollection has Add method with 7 overloads. Look for the method signatures here.



We will concentrate on the following signature:



public virtual Guid Add(
string title, //Title for the library
string description,//Description about the library
SPListTemplate template,//The list template. We are interested in Document Library.
SPDocTemplate documentTemplate //The document template for library.e.g. Excel, Word or Powerpoint etc.
)

SPListTemplateType is a enumeration. The enumeration has underlying integer values which match the Type attribute of ListTemplate element. Please visit here for details.


SPListTemplate represents the list definition or list template for the list. The list definition/template has the views and fields defined. SPWeb.ListTemplates returns the list definitions for the web site. SPSite.GetCustomListTemplates method returns the list template collection for the site collection. More details.



SPDocTemplate represents the document template. Whenever we create a document library, it is created by using blank Word Document template. There are many document templates available in SharePoint 2010 OOB. The document templates have IDs. The ID is used to filter the document template collection presented by SPWeb.DocTemplates property. Document Template IDs list follows:












Document Template IDDescription
101A blank Microsoft Word 97-2003 document.
103A blank Microsoft Excel 97-2003 document.
104A blank Microsoft PowerPoint 97-2003 document.
105A blank Microsoft basic page ASPX document.
106A blank Microsoft Web Part Page ASPX document.
111A basic Microsoft OneNote 2010 Notebook.
121A blank Microsoft Word document.
122A blank Microsoft Excel document.
123A blank Microsoft PowerPoint document.


Code:

using (SPSite site = new SPSite(serverUrl)) {
using (SPWeb web = site.OpenWeb()) {
SPListTemplate lstTemplate = web.ListTemplates["Document Library"];
SPDocTemplate docTemplate =(from SPDocTemplate dt in web.DocTemplates
where dt.Type == 122
select dt).FirstOrDefault();
Guid newLibID =
web.Lists.Add("Expense Claims", "Excel Expenses", lstTemplate, docTemplate);
SPDocumentLibrary newLib = web.Lists[newLibID] as SPDocumentLibrary;
newLib.OnQuickLaunch = true;
newLib.Update();
}
}



The code is for console application.(I know you are aware, just for clarity).