CFLib.org – Common Function Library Project

collectionExists(collection)

Last updated March 10, 2006

author

Dan G. Switzer, II

Version: 2 | Requires: CF6 | Library: UtilityLib

Description:
Call this function, passing in a collection name, to see if that named Verity colleciton exists. The previous version of this UDF used the <cfcollection action="list" /> tag to determine if the collection exists. The problem is, this action takes a very long time to return a result if there are a lot of collection registered (appx 1 second for every 3 collections.)

Return Values:
Returns a boolean.

Example:

<cfif NOT collectionExists("catalog")>
    <cfcollection
        action="create"
        collection="catalog"
        path="#pathToVerityCollection#"
        language="english"
            />
</cfif>

Parameters:

Name Description Required
collection Name of collection Yes

Full UDF Source:

<!---
 Call this function, passing in a collection name, to see if that named Verity colleciton exists.
 Version 1 by Pete Ruckelshaus, pruckelshaus@yahoo.com
 Raymond Camden modified version 2 a bit.
 
 @param collection      Name of collection (Required)
 @return Returns a boolean. 
 @author Dan G. Switzer, II (dswitzer@pengoworks.com) 
 @version 2, March 10, 2006 
--->
<cffunction name="collectionExists" returnType="boolean" output="false" hint="This returns a yes/no value that checks for the existence of a named collection.">
    <cfargument name="collection" type="string" required="yes">

    <!---// by default return true //--->
    <cfset var bExists = true />
    <cfset var searchItems = "">
    
    <!---// if you can't search the collection, then assume it doesn't exist //--->
    <cftry>
        <cfsearch
            name="searchItems"
            collection="#arguments.collection#"
            type="simple"
            criteria="#createUUID()#"
            />
        <cfcatch type="any">
            <!---// if the message contains the string "does not exist", then the collection can't be found //--->
            <cfif cfcatch.message contains "does not exist">
                <cfset bExists = false />
            <cfelse>
                <cfrethrow>
            </cfif>
        </cfcatch>
    </cftry>

    <!---// returns true if search was successful and false if an error occurred //--->
    <cfreturn bExists />

</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson