Thursday, December 27, 2012

Hyperlink to ‘Open Edit Tool Pane’ for SharePoint 2010 VisualWebPart

The following post will show you how to use SharePoint’s built-in JavaScript function 'ShowToolPane2Wrapper' to put the WebPartPage into edit mode and have the web parts properties editable.

All that you need is to add ShowToolPane2Wrapper takes 3 parms.

<a href=\"javascript:ShowToolPane2Wrapper('Edit', this,'" + this.ID + "')\">some text</a>

Note: "ID" is the webpart ID, an VisualWebPart uses a UserControl to hold the WebParts code, so use Parent.ID.

All Code
using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace SomeVisualWebPart.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        public string ListName { get; set; }
        private Label openToolPaneMessage;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Controls.Clear();

            if (isWebPartConfigured(this.ListName))
            {
                // Add Controls as normal
            }
            else
            {
                this.Controls.Add(this.openToolPaneMessage);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.openToolPaneMessage = new Label();
            this.openToolPaneMessage.Style.Add(HtmlTextWriterStyle.PaddingTop, "5px");
            this.openToolPaneMessage.Text = string.Format(CultureInfo.CurrentCulture, "Web part is not configured. <a href=\"javascript:ShowToolPane2Wrapper('Edit', this,'{0}')\">Open the tool pane</a> to set the web part properties.", this.Parent.ID);

            base.OnInit(e);
        }

        private bool isWebPartConfigured(string listName)
        {
            bool isWebPartConfigured = false;

            if (!string.IsNullOrEmpty(listName))
            {
                SPList tmpList = SPContext.Current.Web.Lists.TryGetList(listName);
                if (tmpList != null)
                {
                    isWebPartConfigured = true;
                }
            }

            return isWebPartConfigured;
        }
    }
}

Note:
In SharePoint 2007 this has a 'MSOTlPn_' prefix, MSOTlPn_ShowToolPane2Wrapper.


Result:


Monday, December 17, 2012

Adding CustomProperties to your VisualWebPart

When creating WebParts you sometimes need some specific configuration settings, this post will show you how to add a custom property to your webpart.

It’s an easy two-step process:

First we will add a public variable 'ListName' to the VisualWebPart's UserControl, *.ascx.cs

public string ListName { get; set; }

We do this in preparation, as we need to use the value here in the usercontrol code.

All Code:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SomeVisualWebPart.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        public string ListName { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            // use ListName
        }
    }
}

The second and final step is to add code to the WebPart to add the custom property to it.

We'll start by adding a personalizable public variable to the WebParts *.cs code.

[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("List Name"),
WebDescription("The name of the List to display."),
Category("Custom Property")]
public string ListName
{
     get;
     set;
}

Then under the CreateChildControls section, change your code to implement 'controls' as your usercontrol and set the variable(s) needed to be passed to the usercontrol.

VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
control.ListName = this.ListName;
Controls.Add(control);


All Code:
using System;
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;

namespace SomeVisualWebPart.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/SomeVisualWebPart/VisualWebPart1/VisualWebPart1UserControl.ascx";

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        WebDisplayName("List Name"),
        WebDescription("The name of the List to display."),
        Category("Custom Property")]
        public string ListName
        {
            get;
            set;
        }

        protected override void CreateChildControls()
        {
            VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
            control.ListName = this.ListName;
            Controls.Add(control);
        }
    }
}

Result:




Friday, December 7, 2012

Change your WebParts group displayed category

By default when creating a new SP2010 WebPart or VisualWebPart it gets added to the 'Custom' group category.
 
 
There is two ways of changing this:
 
The first is by using the UI:
  1. Click Site Actions then Site Settings
    .
  2. Under the Galleries group click the Web parts link.
  3. Find and open the webpart in questions settings.
  4. In the Group section, under the Specify your own value option, type in a group name.
 
The second is via code:
 
In your VS solution, under the relevant project, expand your WebPart to expose the Elements.xml file.


You need to change the value of the Group tag under the Property element that sits under root\Elements\Module\File\



<Property Name="Group" Value="Custom" />

Result: