Creating an application using the SharePoint Portal Object Model -- accessing PortalSite
Just as with the WSS OM, I will start out simple here. I am going to illustrate three different ways you might arrive at a PortalSite object. PortalSite could be considered to be your starting point in a SPS OM application. From PortalSite you can establish PortalContext which is required for many OM calls. As such, PortalSite becomes important because it allows you to point (or not to point) to where you wish to operate against.
You may encounter a definition of PortalContext that does not depend on PortalSite. If this is the case you will be on an ASP.NET page that is within a portal. The declaration of PortalContext in this condition would look something like:
Microsoft.SharePoint.Portal.PortalContext myPortalContext = Microsoft.SharePoint.Portal.PortalApplication.GetContext();
From here you can move directly into the OM call you are looking to utilize.
For the case that you are writing a standalone application however, I am showing some functions to get you this far. In order for the following code to work you will need to add a reference to Microsoft.SharePoint.Portal. I also am utilizing items which would require System.Web and Microsoft.SharePoint, but depending on what you are doing you may not need both of those. First add the portal reference:
Then add the using statement:
Then you'll need to add some things in the designer. These functions require:
- a listbox named listBox2 (which I'm logging errors/etc to)
- a listbox named listBox1 (which we will use for output)
- one textbox named textBox1 for when we specify a specific site for button2
- a button called button1 which will enumerate all portals
- a button called button2 which will bind to a specific portal per textBox1
- a button called button3 which will enumerate all virtual servers and portals (such that you can examine the virtual server)
You can pretty-up the interface however you want beyond this and you should be able to drop this code in place. You'll want to register the button clicks to match these functions. Any of these methods or a combination of them may allow for what you specifically need. From this point you can call most SharePoint Portal OM base objects. In my examples, I will favor the button2 example due to its simplicity and assume that if you need to operate against multiple portals or discover the appropriate portal that these samples give you what you would need to adjust accordingly.
private void button1_Click(object sender, System.EventArgs e)
{
TopologyManager myTopologyManager = new TopologyManager();
listBox1.Items.Clear();
//Lets go through all available PortalSites on all VirtualServers
foreach (PortalSite myPortalSite in myTopologyManager.PortalSites)
{
try
{
listBox1.Items.Add(myPortalSite.Url);
}
catch (Exception ex)
{
listBox2.Items.Add(ex.Message);
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
TopologyManager myTopologyManager = new TopologyManager();
//We know our site and it is in textBox1. Lets just go to this specific site
PortalSite myPortalSite = myTopologyManager.PortalSites[new Uri(textBox1.Text)];
//You have to add System.Web to use PortalContext
PortalContext myPortalContext = PortalApplication.GetContext(myPortalSite);
try
{
listBox1.Items.Clear();
listBox1.Items.Add(myPortalSite.Url);
//This is the GUID for this search app
listBox1.Items.Add(myPortalContext.SearchApplicationName);
}
catch (Exception ex)
{
listBox2.Items.Add(ex.Message);
}
}
private void button3_Click(object sender, System.EventArgs e)
{
TopologyManager myTopologyManager = new TopologyManager();
VirtualServerCollection myVirtualServerCollection = myTopologyManager.VirtualServers;
listBox1.Items.Clear();
foreach (VirtualServer myVirtualServer in myVirtualServerCollection)
{
//At this point we could test the virutalserver and/or test for version number as I do next
//This would be useful if you want to check for specific version/etc
listBox1.Items.Add(myVirtualServer.Name);
try
{
//You have to add Microsoft.SharePoint to access GetSPVirtualServer
listBox1.Items.Add(myVirtualServer.GetSPVirtualServer().Version.ToString());
}
catch (Exception ex)
{
listBox2.Items.Add(ex.Message);
}
foreach (PortalSite myPortalSite in myVirtualServer.PortalSites)
{
try
{
//A WSSOnly Site will not return a value here...
listBox1.Items.Add(myPortalSite.Url);
}
catch (Exception ex)
{
listBox2.Items.Add(ex.Message);
}
}
}
}
For some additional information about the SharePoint Portal OM, start at:
www.microsoft.com/technet/prodtechnol/sppt/reskit/c3461881x.mspx