If you are developing web parts then you probably find you are doing the same thing over and over, how many times have you coded the GetToolParts routine ? How many times have you coded it differently ?  Many times and hardly any retrospectively.  How many common routines do you have in your web parts relating solely to webparts.

 

If the answer is lots you need a new spangly web part base class.

 

Consider writing a base class for you web parts.  I have done this and it does a variety of tasks.  Initially it only did the GetToolParts routine and a new AddToolPart Routine (I needed someway to add custom tool parts when I used them).

 

As time went by the class grew.  It now implements a common way of web part versioning (I will discuss this later) it also has routines to determine SPS areas so in your web part you need just type this.ContextIsSPS or from the webpart you can get the current area by calling this.CurrentSPSArea.

 

I later added routines to ensure webparts are only placed in Areas, not on Team Sites, or web parts that are only allowed on the Home page not anywhere else, this forces content managers to place real content in their areas instead of duplicating functionality to fill the space.  All of this controlled by values in the dwp.

 

Here is the some initial code, you need to make the call on whether you cut and paste into projects or like me make a library and have it installed.

 

The next couple of posts build on this an recover some of my previous posts only hopefully in a more concise manner.

 

Some code stolen from (thanks)

    http://www.paraesthesia.com/blog/comments.php?id=460_0_1_0_C

 

Code:-

using System;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Portal;
using Microsoft.SharePoint.Portal.SiteData;
using Microsoft.SharePoint.Portal.Topology;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
namespace BinaryJam.Sharepoint.WebPartPages
{

    /// <summary>
    /// Replacement base class for Microsoft.SharePoint.WebPartPages.WebPart.
    /// </summary>
    ///
    [ToolboxData(“<{0}:ServiceStatusPart runat=server></{0}:ServiceStatusPart>”),
    XmlRoot(Namespace=http://BinaryJam/SharePoint.WebPartPages.WebPartBase&#8221;)]
    public abstract class WebPartBase: Microsoft.SharePoint.WebPartPages.WebPart
    {

        private Queue toolPartQueue=new Queue();

        /// <summary>
        /// Constructor.
        /// </summary>
        protected WebPartBase () : base()
        {
        }

        #region Do the ToolPartThing
        /// <summary>
        /// AddToolParts is to allow inherited WebParts to add ToolParts as GetToolParts is sealed.
        ///
        /// You should add toolparts in the constructor of the inherited web part.
        /// </summary>
        /// <param name=”customToolPart”></param>
        protected void AddToolParts(ToolPart newToolPart)
        {

            //Check for muppets
            if ( newToolPart is WebPartToolPart )
            {
                throw new ToolPartException(“A WebPartToolPart is already added”);
            }
            if ( newToolPart is CustomPropertyToolPart )
            {
                throw new ToolPartException(“A CustomPropertyToolPart is already added”);
            }

            toolPartQueue.Enqueue(newToolPart);
        }

        public sealed override ToolPart[] GetToolParts()
        {
            int i=0;
            ToolPart[] toolPartsArray = new ToolPart[2 + this.toolPartQueue.Count];
            WebPartToolPart webPartToolPart = new WebPartToolPart();
            CustomPropertyToolPart customPropertyToolPart = new CustomPropertyToolPart();

            toolPartsArray[i++] = customPropertyToolPart;
            toolPartsArray[i++] = webPartToolPart;

            while (this.toolPartQueue.Count>0)
            {
                try
                {
                    ToolPart nextToolPart=(ToolPart)this.toolPartQueue.Dequeue();
                    toolPartsArray[i++] = nextToolPart;
                }
                catch{}
            }

            return toolPartsArray;

        }
        #endregion

        #region SPS Methods
        ///Thanks to http://www.paraesthesia.com/blog/comments.php?id=460_0_1_0_C
        /// <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 bool IsContextSPS()
        {
            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
                        return true;
                    }
                }
                else
                {
                    // We couldn’t bind to the Portal assembly, so we’re not on an SPS box
                    return false;
                }
            }
            catch
            {
                //Something went very wrong so catch the error else the webpart will die
                //If there is something really wrong then the whole of Sharepoint will die
                //so I am not worrying about this specific catch
                return false;
            }
            finally
            {
                assemblyInstance = null;
            }
        }

        /// <summary>
        /// Return the current SPS Area, if this WSS though it will return null.
        /// </summary>
        public Area CurrentSPSArea
        {
            get
            {
                if (IsContextSPS())
                {
                    Microsoft.SharePoint.Portal.WebControls.PageInfo pageInfo = (Microsoft.SharePoint.Portal.WebControls.PageInfo)Context.Items[“SPS_PageInfo”];
                    Guid currentAreaGuid = pageInfo.CategoryId;
                    Area currentArea = AreaManager.GetArea(PortalContext.Current, currentAreaGuid);

                    return currentArea;
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion

    }//End of Class

    /// <summary>
    /// ToolPart exception class, if someone tries to add the standard ToolParts again, this is thrown.
    /// </summary>
    [Serializable()]
    public class ToolPartException : System.Exception
    {
        public ToolPartException()
        {
        }

        public ToolPartException(string message): base(message)
        {
        }
        public ToolPartException(string message, Exception innerException): base (message, innerException)
        {
        }
        protected ToolPartException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
    }//End of class

}//End of Namespace

Full version of this code can be found in a later post with more functionality

http://blog.binaryjam.com/PermaLink,guid,fa61431f-ecb8-4696-91dc-62a9c0454715.aspx

 

Advertisements