Web Part Context

For those writing web parts for SharePoint Portal Server 2003 or Windows SharePoint Services, you may want to determine what context your web part is running in - is it WSS or SPS? I wrote a web part that had optional use of Audiences in SPS, but I wanted it to work (and ignore Audience settings) in WSS, too.

Here's a static method to help you out.



using System.Reflection;

/// <summary>
/// Returns a Boolean indicating whether we're running in SharePoint Portal Server
/// or not.
/// </summary>
/// <returns>True if we're running in SPS; false otherwise.</returns>
public static bool ContextIsSPS(){
   Assembly assemblyInstance = null;
   try{
      // Try to bind to the Microsoft.SharePoint.Portal assembly.
      // If it isn't there, we're not in SPS.
      assemblyInstance = System.Reflection.Assembly.LoadWithPartialName("Microsoft.Sharepoint.Portal");
      if(assemblyInstance != null){
         // We've successfully bound to the assembly, so now let's try to determine
         // the current PortalContext.
         System.Type oType = assemblyInstance.GetType("Microsoft.SharePoint.Portal.PortalContext");
         PropertyInfo oInfo = oType.GetProperty("Current");
         object result = oInfo.GetValue(null, null);
         if (result == null) {
            // SPS Installation, but WSS context
            return false;
         }
         else {
            // Running in SPS context
            // To determine SPS site URL:
            // PropertyInfo oSiteURLInfo = result.GetType().GetProperty("SiteUrl");
            // string siteurl = (string)oSiteURLInfo.GetValue(result,null);
            return true;
         }
      }
      else{
         // We couldn't bind to the Portal assembly, so we're not on an SPS box
         return false;
      }
   }
   catch (Exception err){
      System.Diagnostics.Debug.WriteLine("ContextIsSPS(): Exception determining SPS context; returning FALSE.");
      System.Diagnostics.Debug.WriteLine(err.Message);
      return false;
   }
   finally{
      assemblyInstance = null;
   }
}

Print | posted @ Friday, January 16, 2004 5:48 PM

Comments on this entry:

Gravatar # Simon Tocker's BinaryJam - Web Part Base
by blog.binaryjam.com at 8/11/2005 8:36 AM

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 4 and 1 and type the answer here: