SPS NOLA 2011 Presentation Materials

The materials (code and PowerPoint deck) from my presentation at SharePoint Saturday New Orleans 2011 can be downloaded using the following links:

Code: https://jeremyknight.wordpress.com/2011/02/26/sps-nola-2011-presentation-materials/
Slides: https://docs.com/knight0323/6081/developing-custom-sharepoint-web-parts

SharePoint Web Part XML Properties

When building a web part for SharePoint, you can use the .webpart file to set default values for almost all of the properties of that web part. But many people don’t know what those properties are or what they pertain to. Below is a listing of all the properties of an empty web part:

    <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
            <type name=yourTypeHere" />
            <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
                <property name="AllowClose" type="bool">True</property>
                <property name="AllowConnect" type="bool">True</property>
                <property name="AllowEdit" type="bool">True</property>
                <property name="AllowHide" type="bool">True</property>
                <property name="AllowMinimize" type="bool">True</property>
                <property name="AllowZoneChange" type="bool">True</property>
                <property name="CatalogIconImageUrl" type="string" />
                <property name="ChromeState" type="chromestate">Normal</property>
                <property name="ChromeType" type="chrometype">Default</property>
                <property name="Description" type="string">My WebPart</property>
                <property name="Direction" type="direction">NotSet</property>
                <property name="ExportMode" type="exportmode">All</property>
                <property name="Height" type="unit" />
                <property name="HelpMode" type="helpmode">Navigate</property>
                <property name="HelpUrl" type="string" />
                <property name="Hidden" type="bool">False</property>
                <property name="Title" type="string">Web Part Title</property>
                <property name="TitleIconImageUrl" type="string" />
                <property name="TitleUrl" type="string" />
                <property name="Width" type="unit" />

For a definition and list of possible values for each of these properties, you can visit the MSDN pages for:

What about custom properties?

You can use SharePoint to create a reference for the properties of your web part. Install your web part, setup the web part the way you would like it by default, and then use the Verbs menu to Export the web part. Locate the exported file, open it in your favorite editor, and your XML is there!  Simple right?

Programmatically Update Web Part Properties

Have you ever wanted to give your users access to the personalized properties of a web part through a custom interface built into a web part? In ASP.NET, updating these properties and more importantly persisting the changes was a pretty straight forward process. Unfortunately, the same straight forward techniques can not be used when developing a SharePoint web part.

The code below is a very simple web part inheriting from System.Web.UI.WebControls.WebParts.WebPart. It consists of a TextBox and a Button but it illustrates a complete solution for persisting personalization changes to the SharePoint database programmatically.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace UpdatePropertiesWithCode.UpdateMeWebPart
    public class UpdateMeWebPart : System.Web.UI.WebControls.WebParts.WebPart
        protected TextBox MessageTextBox;
        protected Button SaveButton;

        [Category("My Configuration")]
        [WebDescription("Message to display.")]
        public string Message { get; set; }

        protected override void CreateChildControls()
            MessageTextBox = new TextBox();
            MessageTextBox.Text = this.Message;

            SaveButton = new Button();
            SaveButton.Text = "Save";
            SaveButton.Click += new EventHandler(SaveButtonClick);


        protected void SaveButtonClick(object sender, EventArgs e)
            this.Message = MessageTextBox.Text;

            SPWeb web = SPContext.Current.Web;
            SPFile file = web.GetFile(HttpContext.Current.Request.Url.ToString());
            SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.User);
            System.Web.UI.WebControls.WebParts.WebPart webPart = manager.WebParts[this.ID];
            ((UpdateMeWebPart)webPart).Message = this.Message;

                web.AllowUnsafeUpdates = true;
                web.AllowUnsafeUpdates = false;