Showing posts with label sharepoint controls. Show all posts
Showing posts with label sharepoint controls. Show all posts

Monday, March 28, 2011

Web Part Public Properties, Attributes and Type of Attributes

You can decorate the public properties of the web part class with the following attributes from System.ComponentModel so that your web part can runs in a stateless environment, like SharePoint:

  • WebDisplayName - This string shows as the label for the control in the Editor pane.
  • WebDescription - This string shows as a tooltip over the display name.
  • WebBrowsable - When this Boolean is set to true, the end user will be able to see the property in the Editor pane. Set it to false to imperatively set the property’s value on behalf of the end user.
  • Personalizable - This enum has two settings:
    • PersonalizationScope.Shared indicates that SharePoint should only store one value for everyone.
    • PersonalizationScope.User indicates that SharePoint should still store one common value for everyone, but allow anyone who has permission to personalize to change that value to whatever he or she would like it to be.

image

The following code example demonstrates how to use these attributes:

    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        private string _jobTypeName = string.Empty;

        [WebDisplayName("Job Type"),
        WebDescription("Specify your job."),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared)]
        public string JobTypeName
        {
            get { return _jobTypeName; }
            set { _jobTypeName = value; }
        }
    }

Each public property can have a unique set of attributes. SharePoint supports several different types:

  • string - Rendered as a textbox
  • bool - Rendered as a checkbox
  • datetime - Rendered as a textbox
  • int - Rendered as a numeric-only textbox
  • float - Rendered as a numeric-only textbox
  • knowncolor - Rendered as a drop-down list
  • enum - Rendered as a drop-down list

The following figure depicts how SharePoint render different types of public properties:

image

Code sample as shown below:

[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
    private string _stringType = string.Empty;
    [Personalizable(), WebDisplayName("String Type"), WebBrowsable(true)]
    public string StringType
    {
        get { return _stringType; }
        set { _stringType = value; }
    }

    private bool _boolType = false;
    [Personalizable(), WebDisplayName("Bool Type"), WebBrowsable(true)]
    public bool BoolType
    {
        get { return _boolType; }
        set { _boolType = value; }
    }

    private DateTime _datetimeType;
    [Personalizable(), WebDisplayName("Datetime Type"), WebBrowsable(true)]
    public DateTime DatetimeType
    {
        get { return _datetimeType; }
        set { _datetimeType = value; }
    }

    private int _intType = 0;
    [Personalizable(), WebDisplayName("Int Type"), WebBrowsable(true)]
    public int IntType
    {
        get { return _intType; }
        set { _intType = value; }
    }

    private float _floatType = 0;
    [Personalizable(), WebDisplayName("Float Type"), WebBrowsable(true)]
    public float FloatType
    {
        get { return _floatType; }
        set { _floatType = value; }
    }

    private KnownColor _knowncolor;
    [Personalizable(), WebDisplayName("KnownColor Type"), WebBrowsable(true)]
    public KnownColor Knowncolor
    {
        get { return _knowncolor; }
        set { _knowncolor = value; }
    }

    public enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri };
    private Days _enumType;
    [Personalizable(), WebDisplayName("Enum Type"), WebBrowsable(true)]
    public Days EnumType
    {
        get { return _enumType; }
        set { _enumType = value; }
    }
}

Monday, February 7, 2011

How to Show Favicon In SharePoint 2010

Favicons are the little shortcut icons that appear in most modern browsers next to bookmarks, as well as in the address bar next to the site’s URL and on browser tabs - check out Wikipedia about Favicon.

SharePoint 2010 makes it very easy to add a favicon to custom master pages using the following code:

<SharePoint:SPShortcutIcon runat=”server” IconUrl=”/Style Library/Images/SPMalayaFavIcon.ico”/>

Favicon

Please note that when testing favicons Internet Explorer, if you are having trouble seeing a new favicon and you are certain that the code and the image are set up correctly, it may help to clear your browser cache, make a new bookmark, or even close and
reopen the browser.

Saturday, July 18, 2009

Adding Drop-Down Menu Items to SPGridView Using SPMenuField Control

When displaying custom data through SPGridView you won’t get the same look and feel of the default SharePoint List View. SPMenuField provides a data bound drop-down menu to be used in place of BoundField, TemplateField, or similar controls.

The following describe steps required to add drop-down menu items to a SPGridView control in Web Part.

  • Create a Web Part
    • Add project references: Microsoft.SharePoint and System.Web
    • Add the following namespaces to your class:

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Generic;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

    • Inherits your class with Microsoft.SharePoint.WebPartPages.WebPart

public class SampleGrid : WebPart
{
}

  • Override CreateChildControls method, create the ObjectDataSource, SPGridView, SPGridViewPager and SPMenuField controls as shown below:

protected override void CreateChildControls()
{
    // Why UseDefaultStyles = false?
    // See http://sharepointmalaya.blogspot.com/2009/07/how-to-make-spgridview-to-have-same.html for details
    this.UseDefaultStyles = false;

    // ObjectDataSource
    ObjectDataSource dataSource = new ObjectDataSource();
    dataSource.ID = "dataSource";
    dataSource.SelectMethod = "SelectData";
    dataSource.TypeName = this.GetType().AssemblyQualifiedName;
    dataSource.ObjectCreating += new ObjectDataSourceObjectEventHandler(objectCreating);
    this.Controls.Add(dataSource);

    // SPGridView
    spGridView = new SPGridView();
    spGridView.ID = "spGridView";
    spGridView.DataSourceID = dataSource.ID;
    spGridView.AutoGenerateColumns = false;
    spGridView.AllowPaging = true;
    spGridView.PageSize = 10;
    spGridView.AllowSorting = true;
    spGridView.EmptyDataText = string.Empty;
    this.Controls.Add(spGridView);

    // SPGridViewPager
    pager = new SPGridViewPager();
    pager.GridViewId = spGridView.ID;
    this.Controls.Add(pager);

    // SPMenuField - Name field
    SPMenuField nameCol1 = new SPMenuField();
    nameCol1.HeaderText = "Name";
    nameCol1.TextFields = "Name";
    nameCol1.NavigateUrlFields = "Name,Email,WebSite";
    nameCol1.NavigateUrlFormat = "/_layouts/sharepointmalaya/DispDetails.aspx?WebPartID=" + this.ID + "&FilterName={0}&ReturnUrl=" + Page.Request.Url.ToString();
    nameCol1.TokenNameAndValueFields = "KEYNAME=Name,KEYEMAIL=Email,KEYWEBSITE=WebSite";
    nameCol1.SortExpression = "Name";
    nameCol1.MenuTemplateId = "menuTemplate";

    // MenuTemplate
    MenuTemplate menuTemplate = new MenuTemplate();
    menuTemplate.ID = "menuTemplate";
    this.Controls.Add(menuTemplate);
    spGridView.Columns.Add(nameCol1);

    // MenuTemplate - View Item
    MenuItemTemplate menuItemTemplate0 = new MenuItemTemplate("View Item", "~/_layouts/images/LIST.GIF");
    menuItemTemplate0.ClientOnClickNavigateUrl = "/_layouts/sharepointmalaya/DispDetails.aspx?WebPartID=" + this.ID + "&FilterName={%KEYNAME%}&ReturnUrl=" + Page.Request.Url.ToString();
    menuTemplate.Controls.Add(menuItemTemplate0);

    // MenuTemplate - Seperator
    MenuSeparatorTemplate menuSepTemplate = new MenuSeparatorTemplate();
    menuTemplate.Controls.Add(menuSepTemplate);

    // MenuTemplate - Open WebSite
    MenuItemTemplate menuItemTemplate1 = new MenuItemTemplate("Open WebSite", "~/_layouts/images/ASP16.GIF");
    menuItemTemplate1.ClientOnClickNavigateUrl = "javascript:window.open('%KEYWEBSITE%');";
    menuTemplate.Controls.Add(menuItemTemplate1);

    // MenuTemplate - Send Email
    MenuItemTemplate menuItemTemplate2 = new MenuItemTemplate("Send Email", "~/_layouts/images/EML16.GIF");
    menuItemTemplate2.ClientOnClickScript = "javascript:document.location.href='MailTo:%KEYEMAIL%;'";
    menuTemplate.Controls.Add(menuItemTemplate2);

    // BoundField - Email field
    BoundField nameCol2 = new BoundField();
    nameCol2.DataField = "Email";
    nameCol2.SortExpression = "Email";
    nameCol2.HeaderText = "Email";
    spGridView.Columns.Add(nameCol2);

    // BoundField - WebSite field
    BoundField nameCol3 = new BoundField();
    nameCol3.DataField = "WebSite";
    nameCol3.SortExpression = "WebSite";
    nameCol3.HeaderText = "WebSite";
    spGridView.Columns.Add(nameCol3);

    this.ChildControlsCreated = true;
}

  • Code above create three (3) drop-down menu items and a separator menu as shown below:

image

  • Clicking on the “View Item” redirects to custom page in the _layouts folder. When clicking on the “Open WebSite”, it opens web site and “Send Email” menu open client email form.
  • Please note SPMenuField has a property TokenNameAndValueFields which can be used to store token names and values in the following format:

SPMenuField.TokenNameAndValueFields = "KEYNAME=Name,KEYEMAIL=Email,KEYWEBSITE=WebSite";

  • To consume SPMenuField token values, you must specify token name within the % sign as shown below:

“%TOKEN_NAME%” OR “%KEYWEBSITE%”

  • As shown in code example above, ObjectDataSource control require “SelectData” method. You need to create a public method SelectData which returns a System.Data.DataTable – a dummy data which has three (3) columns which are “Name”, “Email” and “WebSite”. See below:

public DataTable SelectData()
{
    DataTable table = new DataTable();
    table.Columns.Add("Name");
    table.Columns.Add("Email");
    table.Columns.Add("WebSite");

    table.Rows.Add("SharePoint Malaya","sharepointmalaya@gmail.com","http://sharepointmalaya.blogspot.com");
    table.Rows.Add("Faris","faris@gmail.com","http://sharepointmalaya.blogspot.com");
    table.Rows.Add("Farhana","farhana@gmail.com","http://sharepointmalaya.blogspot.com");

    return table;
}

  • In addition, ObjectDataSource control implement event handler ObjectCreating – You also need to create instance containing the event data as shown below:

private void objectCreating(object sender, ObjectDataSourceEventArgs e)
{
    e.ObjectInstance = this;
}

  • Next, bind the data with the SPGridView control as shown below:

protected override void Render(HtmlTextWriter writer)
{
    if (ChildControlsCreated)
        spGridView.DataBind();
    base.Render(writer);
}

  • Build and deploy to SharePoint site. That’s it – All Done. See below for screenshot:

image

Get sample code here:

Friday, July 17, 2009

How to Make SPGridView To Have Same Look And Feel Of The Out Of The Box SharePoint List View

When you attempt to develop a Web Part that uses an SPGridView control, you’ll notice the following differences to your SPGridView look and feel compared to the out of the box SharePoint List View:

  • SPGridView font type is Verdana, out of the box SharePoint List View font type is Tahoma
  • Font size and colour different
  • No alternating styles for items
  • Header style, pager styles, etc.

Screenshot below shows the differences:

 image

If you’re good in CSS and HTML, then you’ll find CSS Reference Chart for SharePoint 2007 is helpful and you should be able to change your SPGridView styles to have the same look and feel of of the out of the box SharePoint List View.

Instead of doing above you can assign the WebPart.UseDefaultStyles of your Web Part to false. See code below:

protected override void CreateChildControls()
{
    this.UseDefaultStyles = false;

By setting the UseDefaultStyles to false, your SPGridView styles will have the same look and feel of the out of the box SharePoint List View. See screenshot below:

image

Please note that UseDefaultStyles only applicable for SharePoint Web Part and not ASP.NET Web Part – for ASP.NET Web Part please use SPChromeSettings.UseDefaultStyles.