Tag: sharepoint

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")]
        [WebBrowsable(false)]
        [WebDescription("Message to display.")]
        [WebDisplayName("Message")]
        [Personalizable(PersonalizationScope.User)]
        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);

            Controls.Add(MessageTextBox);
            Controls.Add(SaveButton);
        }

        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;

            try
            {
                web.AllowUnsafeUpdates = true;
                manager.SaveChanges(webPart);
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }
    }
}
Advertisements

Setup a ‘Get Public Key’ Tool

If you’re a SharePoint developer, you’ve run into public key tokens but what you might not know is that Visual Studio comes with an executable that can make retrieving them a much easier process.

The following steps will add a “Get Public Key’ option to your Tools menu in Visual Studio:

  • Click ‘Tools > External Tools’
  • Click ‘Add’ and a new entry will appear
  • In the ‘Title’ field type: Get Public Key
  • Click the ‘…’ button and navigate to the appropriate sn.exe
  • If VS 2008: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn.exe
  • If VS 2010*: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe
  • In the ‘Arguments’ field type: -Tp “$(TargetPath)”
  • Select ‘Use Output window’ checkbox
  • Click ‘OK’

External Tools screenshot

This will save you a ton of time if you are still using tools like Reflector to get the public key.

* This path is from a 64-bit version of Windows.