I was doing some work around AMO and SSAS and I need to load a partition from database and read some of its properties . I was able to do most of them but the <QueryDefinition> was a bit of issue. I thought I will write a small blog on it. It might be useful for someone.

But if this Partition uses a dsvTable binding than this wont work. Anyway this is a small console application that does that plus it also serves a intro to how to use AMO to connect to Analysis service Server and use the Object model to program against the SSAS Objects.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AnalysisServices;
using System.IO;

namespace TestBed
{
    class Program
    {
        static void Main(string[] args)
        {
            Server srv = new Server();
            srv.Connect("Data source=IMGLT119;Initial catalog=Adventure Works DW");
            Database db = srv.Databases.FindByName("Adventure Works DW");
            Console.WriteLine("Connected to Database {0} on server {1}", db.Name, srv.Name);
            foreach (Cube cb in db.Cubes)
            {
                if (cb.Name == "Adventure Works")
                {
                    foreach (MeasureGroup mg in cb.MeasureGroups)
                    {
                        foreach (Partition pt in mg.Partitions)
                        {
                            
                                //You can access the partition properties . Well Most of it.
                                Console.Write(" partition name is {0} partition slice before is {1}", pt.Name, pt.Slice);
                                QueryBinding qb = new QueryBinding();
                                //I had a bit of glitch with finding the Query Definition . If you have table binding than this wont work . 
                                qb = (QueryBinding)pt.Source;
                                Console.WriteLine("Query Definition {0}", qb.QueryDefinition.ToString()  );
                                break;
                            }

                        }
                    }

                    break;
            }
            Console.Read();
        }
    }
}