简体   繁体   中英

Using c# how can I programmatically check if a folder exists on my reporting server

I Have spent hours looking but without any success.

I am programmatically creating snap shots of reports in SSRS using c# which creates report folders. The reports are created in these folders but to prevent errors occurring I am deleting the whole folder structure and then re-creating the reports to prevent SSRS throwing an exception.

I am using ReportingService2010.

ReportingService2010.DeleteItem(deleteFolderPath);

ReportingService2010.CreateFolder(folder, parentFolder, null);

-- This is the line where I need to check if the folder and report exist

var ret = CheckExist(linkedReportName, newParent);

var param = GetReportParameters(existingReportPath);

ReportingService2010.SetItemParameters(existingReportPath, param);

-- If I do not delete the folder structure the error will be thrown after this in a try/Catch

ReportingService2010.CreateLinkedItem(linkedReportName, newParent, existingReportPath, props);

I need to add a method to see if the report and report folder has already been created

Another possible alternative is to directly work with the path, type, and parentid fields of the Catalog table in your report server database. If you open SQL Server and look at the table, it should be pretty clear what you need to do.

You can use C# to run a SQL command such as

SELECT COUNT(*) FROM CATALOG WHERE PATH LIKE '%TheFolderNameImLookingFor%'

If the count is greater than zero, then the folder exists.

You can vary this for your needs. Folder items have a type of 1; Report items have a type of 2, so you can use this to distinguish between reports and folders.

How to delete folders if they exist using ListChildren ;

        var items = ReportingService2010.ListChildren(parentFolder, false);
        if (items.Where(x => x.TypeName == "Folder").Any(x => x.Name == folder))
        {
            ReportingService2010.DeleteItem(folder, parentFolder, null);
        }

I think the nicest way is ReportService2010.GetItemType() method. This returns a string ( ReportingService2010.ListItemTypes() method), including possible values "Unknown" for non-existent (rather than throwing an exception) or "Folder" for a folder.

Directory.Exists() might work for you.

if (Directory.Exists(folderString))
{
    // Do something
}

Don't forget the System.IO namespace:

using System.IO;

UPDATE : In the future, if you're looking for a file, you can do the same thing with File.Exists() , which is also in the System.IO namespace.

if (!Directory.Exists(folder))
{
    ReportingService2010.CreateFolder(folder, parentFolder, null);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM