SQL Reporting Services Switch Statement
29. August 2007 04:38

SSRS has a switch function, which mirrors the functionality of a Case statement, and can really save you some time.

Most common use for me are setting colors for parts of my report based on the value of a field.

For example, if we have a field: Orders (Fields!Orders.Value) which carries an integer representing the number of orders this week, we may want to set the background color of our report text box based on the order performance.

=Switch(Fields!Orders.Value < 100, "Red", Fields!Orders.Value < 500, "Green")

In the above example, if orders are < 100, we return Red, if they are 100 to 499, we return Green.


How does this work?


The switch statement takes an expression, followed by the return value if that expression evaluated to true. Additionally, it will always take the first expression that evaluated to true, that is why it's OK that a value of 40 (for example) would satisfy both cases. 40 will always give us red, becuase it's the first expression satisfied.


If you want a 'default return value', just take advantage of the fact that the first true expression is evaluated, and modify your code like this:

=Switch(Fields!Orders.Value < 100, "Red", Fields!Orders.Value < 500, "Green", 1=1, "Black")

Now, if our value doesn't satisfy the other expressions, we know the last expression will evaluate to true (1 always equals 1), hence "Black" is our default return value.

Tags: Comments (0) | Permalink
MOSS Recursive Search and Replace
23. August 2007 16:05

Ever find yourself needing to make an edit across every page and every site in your SharePoint 2007 portal? It does seem a bit of a brutal operation, but it has saved me many times doing everything from fixing navigation links to fixing upgrade errors.

I've put together a bit of code to recursively iterate through all the site collections and pages in your farm (or below the specified URL), check out the page, do a replace in the page source, and check it back in. This is a heck of a lot faster than sitting in front of SharePoint Designer for days at a time!

The actual find and replace is done something like this. Keep in mind we've anticipated the publishing features are enabled, if your not using them you'll likely have to make some tweaks.

if (myItem.Properties["PublishingPageLayout"].ToString().Contains(Find))
{
PublishingPage myPage = PublishingPage.GetPublishingPage(myItem);
myPage.CheckOut();
myItem.Properties["PublishingPageLayout"] = myItem.Properties ["PublishingPageLayout"].ToString().Replace(Find, Replace);
myItem.Update();
myPage.CheckIn(string.Empty);
myPage.ListItem.File.Publish(string.Empty);
if (myPage.ListItem.ParentList.EnableModeration)
{
myPage.ListItem.File.Approve(string.Empty);
}

}


The constructor for the class looks like this, and takes 4 parameters:

public void FindReplace(string BaseURL, string Find, string Replace, string WebApp)
{
SPWebService myWebService = SPWebService.ContentService;
SPWebApplication myWebApp = myWebService.WebApplications[WebApp];
SPSite mySite = myWebApp.Sites[BaseURL];
SPWeb myWeb = mySite.OpenWeb();

FixRecursive(myWeb, Find, Replace, "Pages");
Fix(myWeb, Find, Replace, "Pages");
}

BaseURL is the root node at which we will start the replace. Anything below this will be processed. WebApp is the actual string name of the web application in IIS. This was the best way I could find to handle load-balanced farms. For example "SharePoint - 80", etc. You can modify this to get your Web object however you like.


And here is the full class I'm using, hopefully it will get you headed towards your own solution. I'm hesitant to provide an executable or full project, as someone is likely to really fry their installation ;)


public class FindReplace
{
 
    public FindReplace(string BaseURL, string Find, string Replace, string WebApp)
    {
        SPWebService myWebService = SPWebService.ContentService;
        SPWebApplication myWebApp = myWebService.WebApplications[WebApp];
        SPSite mySite = myWebApp.Sites[BaseURL];
        SPWeb myWeb = mySite.OpenWeb();
 
        FixRecursive(myWeb, Find, Replace, "Pages");
        Fix(myWeb, Find, Replace, "Pages");
    }
 
    private void FixRecursive(SPWeb myWeb, string Find, string Replace, string ListTitle)
    {
        foreach (SPWeb web in myWeb.Webs)
        {
            FixRecursive(web, Find, Replace, ListTitle);
            Fix(web, Find, Replace, ListTitle);
        }
    }
 
    public bool Fix(SPWeb myWeb, String Find, String Replace, String ListTitle)
    {
        try
        {
            Console.WriteLine(myWeb.Url);
            SPListItemCollection myItems = myWeb.Lists[ListTitle].Items;
            foreach (SPListItem myItem in myItems)
            {
                if (myItem.Properties["PublishingPageLayout"].ToString().Contains(Find))
                {
                    Console.WriteLine("   -> " + myItem.Title);
                    PublishingPage myPage = PublishingPage.GetPublishingPage(myItem);
                    myPage.CheckOut();
                    myItem.Properties["PublishingPageLayout"] = myItem.Properties["PublishingPageLayout"].ToString().Replace(Find, Replace);
                    myItem.Update();
                    myPage.CheckIn(string.Empty);
                    myPage.ListItem.File.Publish(string.Empty);
                    if (myPage.ListItem.ParentList.EnableModeration)
                    {
                        myPage.ListItem.File.Approve(string.Empty);
                    }
                    Console.WriteLine("Done.");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR: " + ex.Message);
            return false;
        }
        return true;
    }  
}

Tags: Comments (4) | Permalink