Thursday, January 27, 2011

Programatically create Power Pivot Gallery

Have you ever tried creating Power Pivot Gallery?

It isn't straight forward.You cannot just use the 
web. ListTemplates["ReportGalleryLibrary"]. I found that the ListTemplates had the required template but it wasn't accepting the Internal name of ReportGalleryLibrary.

The error I was getting was "Value does not fall within the expected range."

Here is the code to Create a Pivot Gallery on a given Web. 

public void CreatePowerPivotGallery(SPWeb web, string galleryName, string description)
        {
            web.AllowUnsafeUpdates = true;

            SPListTemplate template = null;
            foreach (SPListTemplate temp in web.ListTemplates)
            {
                if (temp.InternalName == "ReportGalleryLibrary")
                {
                    template = temp;
                    break;
                }
            }

            if (template == null)
            {
                throw new Exception();
            }

            web.Lists.Add(galleryName, description, template);

            web.Update();
            web.AllowUnsafeUpdates = false;
        }


Usage:
SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(@"http://mymachine:30000/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        CreatePowerPivotGallery(web, "SomeNewGallery", "Power Pivot Gallery");
                    }
                }
            });


Note: Ensure that the Site Collection has the "PowerPivot Feature Integration for Site Collections" feature enabled.

Monday, January 24, 2011

Troubleshooting in SharePoint 2010 - ULS Logs : Correlation ID

I am sure all of you have worked with Logs to figure out what is happening within your SharePoint application. However, there are cases where you will need to know what is happening within the SharePoint services, that is causing a failure in your application. Especially when you see a message with a Correlation ID that is a pointer to SharePoint logs. 
SharePoint 2010 provides interfaces to Read/Write into the common logging system.

All the SharePoint logs are by default created under "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS" with file names like "$machine$-$date$-$time$.log"

You can configure what needs to be logged in the Central Administration > Monitoring > Reporting > Configure Diagnostics Logging.
Select the Service you are interested in with the required sub category and set the "least critical event to report to the trace log" to the level you intend to monitor. Medium would suffice in most of the case.

You can download a codeplex solution to make your life easier in reading these logs from
The Viewer is self explanatory, just load the log file from the LOGS folder of 14 hive and you are all good to go. I used the Notifications List & Filters very often to see a snap shot of the most critical errors.
Use the Toggle Correlation Tree icon on the top right to see a list of Correlation IDs. 
Ctrl + Shift + I can be used for monitor a particular Correlation ID.

Good news is that SharePoint allows you to write your log messages into it's own framework so that you do not have to maintain two different logs one for your application and another for SharePoint.

Code to write to the SharePoint ULS Logs is as below:

public static void Write(string message)
{
    SPDiagnosticsService logger = SPDiagnosticsService.Local;
    logger.WriteTrace(0,
        new SPDiagnosticsCategory("MyApp",
            TraceSeverity.Monitorable,
            EventSeverity.Error),
            TraceSeverity.Monitorable,
           "Application Message : {0}",
           new object[] {message});
}

That's it for now. Good luck on your next troubleshooting!

Saturday, January 8, 2011

Logging using Enterprise Library... How simple is that?

I am sure you will agree with me how much a logging class is useful to help you isolate that problem. Especially when you are going through a lot of workflows which to simulate and debug becomes a nightmare.



Every project will require some kind of logging either you just want to see if your calls are made as expected OR if you want to take special actions like sending a mail based on the criticality of the log entry.

How long do you think it is going to take to do the following:


Add a class LogginManager.cs


using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Diagnostics;



public static class LoggingManager

{
    public static void Write(string message, TraceEventType eventType)
    {
        if (Logger.IsLoggingEnabled())
        {
            LogEntry entry = new LogEntry
           {
                 Message = message,
                 Severity = eventType
            };
       
            Logger.Write(entry);
        }
    }
}


Add the following libraries in GAC
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll

Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
Microsoft.Practices.EnterpriseLibrary.Common.dll


Now start using Logging class,
LoggingHandler.Write(ex.Message, System.Diagnostics.TraceEventType.Critical);



There is a tool to configure your web/app.config. Follow these basic steps to get you going.



To configure:


1. Open the web.config/app.config from the Enterprise Library Configuration tool. There is also a Visual Studio Add-in to configure directly from Visual Studio.
2. Blocks > Add Logging Settings
3. Add a Logging Target Listener > Add Flat File Trace Listener – Change its formatter to Text Formatter.
4. Change the Listener for the General Category to Flat File Trace Listener
5. Change the Listener for the Logging Errors & Warnings to Flat File Trace Listener
6. Add a Logging Filter > Logging Enabled Filter. Set All Logging to true
7. Save and you are good to go.


You should be able to see the logs in a file called Trace.log under your bin directory. You can change what you see as part of the Logs by modifying the Text Formatter contents.


Recommend using this for logging, there are lot more to offer from the Enterprise Library. Go through,

http://entlib.codeplex.com/wikipage?title=EntLib5&referringTitle=Home


Saturday, January 1, 2011

All about Claims based Authentication, Power Pivot Setup and Data Refresh

There are already tons of articles to help you out in setting up the PowerPivot, however each setup has its own set of challenges. In this article I am going to highlight the issues I faced and I am sure it can save someone that 1 week of time that I spent on it. 

At the end of the article I will also publish a few references that helped me in my setup.

Claims based authentication is the only way to create your web application if you want to support Forms Authentication. i.e. any Authentication provider that is not tied to Active Directory (in which case you could chose your web application to be Classic mode)

If you are reading this article thinking that you can setup Power Pivot on your Claims based Web application, you would be sorry to hear that it is NOT supported. I learnt it the hard way.
You can still view the Power Pivot reports published without any trouble but, you will not be able to take the advantage of the Data Refresh (On Demand/Scheduled) capabilities of Power Pivot.

However, I was able to get it working on my laptop (DC, SP Server, DB & Analysis Server). But, with a limitation that I needed to be a Domain Admin & Farm Admin. It worked only through a unattended account which was tied to a Secure Store Service Application ID configured with the Domain Account.
The same did not work on a multi box environment. Not able to understand why? I was working with a Microsoft Tech support to resolve this though. Which I think now is futile.

For the installation problems, you can refer to my previous article:


Either you are setting up the PowerPivot on Claims/Classic based Web applicaiton, you will definitely want to take care of the following:
1. Ensure that you have deployed the PowerPivotFarm.wsp globally & PowerPivotWebApp.wsp for the Web Application you are enabling Power Pivot capabilities.
See Central Administration > System Settings > Farm Management > Manage Farm Solutions >
2. Ensure that Secure Store Service, SQL Server Analysis Services, SQL Server PowerPivot System Service, Timer Service are started. 
See Central Administration > System Settings > Servers > Manage Services on Server
3. Few Features have to be activated in the Site collection. 
PowerPivot Feature Integration for Site Collections
SharePoint Server Publishing Infrastructure
SharePoint Server Publishing 

See, Site Settings > Site Collection Administration > Site Collection Features >
4. If you have changed the master page then you may end up failing to load the power pivot gallery. Add the following in the VirtualDirectory\{app port}\web.config
<SafeControl Src="~/_layouts/powerpivot/*" IncludeSubFolders="True" Safe="True" AllowRemoteDesigner="True" SafeAgainstScript="True" />
5. For data refresh ensure that the application pool account of the Power Pivot Service Application has enough privileges. 
6. You may need to setup Kerberos (or enable for Delegation in Active Directory) when you have multiple box setup. This one I am not sure yet.:)

Some settings that you need to be aware of for the Power Pivot.
1. Creating Secure Store Service Application ID
2. Linking that in the Power Pivot Excel Workbook while publishing it. I may write another article very soon.
3. Central Administration > PowerPivot > Configure Service Application Settings
4. Central Administration > Monitoring >  Timer Jobs > Review Job Definitions > PowerPivot Data Refresh Timer Job


References:




Hope this information was useful. Look forward for your experiences in setup.