Wednesday, April 18, 2012

Display user image or beside Welcome control in sharepoint foundation

You can find relevant document  Display user profile picture next to welcome Control here.

out come is

Tuesday, April 17, 2012

Create a custom editor for a WebPart

There are situations where you want to use your own custom WebPart editor rather than use the default WebPart editor.
In my scenario i have created Version history webaprt. Here i am binding all lists to dropdown which is having content type either Item or document

Download sample code here

Step 1: The first the thing is to create the WebPart. Then set property attributeWebBrowsable (false)” so that it does not use the default ‘WebPart’ editor. Below is the code snippet for the same font size ‘WebPart’ which has ‘WebBrowsable’ value as false.
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 System.Text;
using System.Collections.Generic;

namespace MyWebparts.CustomEditorPart
{
    [ToolboxItemAttribute(false)]
    public class CustomEditorPart : WebPart
    {
        StringBuilder str;
        Label lblDisplay;

        protected string _WebpartTitle = "Version History";
        [Personalizable(), WebBrowsable(false), Category("Webpart Properties"), WebDisplayName("Webpart Header/Title"), WebDescription("Specify the Webpart Header/Title")]

        public string WebpartTitle
        {
            set
            {
                _WebpartTitle = value;
            }
            get
            {
                return _WebpartTitle;
            }
        }


        protected string _listName;
        [Personalizable(), WebBrowsable(false), Category("Webpart Properties"), WebDisplayName("Enter List Name "), WebDescription("Specify the list name to display items history")]

        public string ListName
        {
            set
            {
                _listName = value;
            }
            get
            {
                return _listName;
            }
        }
        protected override void CreateChildControls()
        {
            str = new StringBuilder();
            lblDisplay = new Label();
            this.Controls.Add(lblDisplay);
            str.Append("
");
            str.Append("
");
            try
            {
                str.Append("
");
                str.Append("
");
                using (SPSite siteColl = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb curWeb = siteColl.RootWeb)
                    {
                        SPList docLib = curWeb.Lists.TryGetList(_listName);

                        if (docLib != null)
                        {
                            SPListItemCollection docColl = docLib.Items;
                            if (docColl.Count > 0)
                            {
                                foreach (SPListItem docItem in docColl)
                                {
                                    str.Append("
");
                                    SPListItemVersionCollection docVerColl = docItem.Versions;
                                    str.Append("
");
                                    foreach (SPListItemVersion docVer in docVerColl)
                                    {
                                        str.Append("
");
                                    }
                                    str.Append("
");
                                }
                            }
                            else
                            {
                                str.Append("
");
                            }
                        }
                        else
                        {
                            str.Append("
");
                        }
                    }
                }
                str.Append("
" + _WebpartTitle + "
  
Document/Item Name : " + docItem.Name + "
Versions
" + docVer.VersionLabel + "
  
No items found..
Please configure list name/correct list name
");
                str.Append("
");
                lblDisplay.Text = str.ToString();
            }
            catch (Exception ex)
            {
            }

        }
        protected override void OnPreRender(EventArgs e)
        {
                            //set webpart crometype to None programitically
            this.ChromeType = PartChromeType.None;
        }

        public override EditorPartCollection CreateEditorParts()
        {
            List<EditorPart> editorParts = new List<EditorPart>(1);
            EditorPart part = new CustomEditorPartEditor();
            part.ID = this.ID + "MyWebpart Editor";
            editorParts.Add(part);
            EditorPartCollection baseparts = base.CreateEditorParts();
            return new EditorPartCollection(baseparts, editorParts);
        }
    }
}


Step 2: The other thing which we need to do in the WebPart is override the ‘CreateEditorParts’ method and add our custom WebPart editor. We will be seeing in the next step how we can create the web part editor. For this example, we have created ‘CustomEditorPartEditor’.
        public override EditorPartCollection CreateEditorParts()
        {
                               //create a list collection in which we will be adding the webpart editor
            List<EditorPart> editorParts = new List<EditorPart>(1);
            EditorPart part = new CustomEditorPartEditor();
            part.ID = this.ID + "MyWebpart Editor";
            editorParts.Add(part);
                         //add the same to the collection and return it so SP Environment can understand which webpart editor needs to be used for this webpart
            EditorPartCollection baseparts = base.CreateEditorParts();
            return new EditorPartCollection(baseparts, editorParts);
        }
Step 3: In order to create your custom web part editor, the first step we need to do is inherit from the EditorPart class. Currently we have created a custom web part editor class called CustomEditorPartEditor. In this custom class, we have created text box and dropdownlist. Using textbox will get webpart title and dropdownlist used to bind list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using Microsoft.SharePoint;
namespace MyWebparts.CustomEditorPart
{
   public class CustomEditorPartEditor:EditorPart
    {
       TextBox txtWebPartTitle;
       DropDownList drList;
       //TextBox txtListName;
       Label lblWebpartTitle;
       Label lblListName;
       protected override void CreateChildControls()
       {
           txtWebPartTitle = new TextBox();
           //txtListName = new TextBox();
           drList = new DropDownList();
           drList.ID = "drList";

           lblWebpartTitle = new Label();
           lblWebpartTitle.Text = "Webpart Tile/Hearder :";

           lblListName = new Label();
           lblListName.Text = "Select Listname :";
           SPWeb curWeb = SPContext.Current.Web;
           SPListCollection listColl = curWeb.Lists;
           foreach (SPList list in listColl)
           {
               foreach (SPContentType ctype in list.ContentTypes)
               {
                   if (ctype.Name == "Item" || ctype.Name == "Document")
                   {
                       drList.Items.Add(list.Title);
                   }
               }
             
                 
              
           }

           this.Controls.Add(lblWebpartTitle);
           this.Controls.Add(new LiteralControl("
"
));
           this.Controls.Add(txtWebPartTitle);
           this.Controls.Add(new LiteralControl("
"
));
           this.Controls.Add(lblListName);
           this.Controls.Add(new LiteralControl("
"
));
           this.Controls.Add(drList);  
       }
       public override bool ApplyChanges()
       {
           this.EnsureChildControls();
           CustomEditorPart myWebPart = (CustomEditorPart)this.WebPartToEdit;
           myWebPart.WebpartTitle = txtWebPartTitle.Text;
           myWebPart.ListName = drList.SelectedValue;
           return true;
       }
       public override void SyncChanges()
       {
           this.EnsureChildControls();
           CustomEditorPart myWebpart = (CustomEditorPart)this.WebPartToEdit;
           txtWebPartTitle.Text = myWebpart.WebpartTitle;
           drList.SelectedValue = myWebpart.ListName;
       }
     
    }
}

Step 4: Two activities take place in general, one is when we the user gives customization values which customizes the web part and the other when we need to synchronize the customization values with the web part.

The web part customization values are stored in a content database according to the user logged in. So when customization data is provided for a web part through our custom web part editor, we need to override theApplyChanges method and push the customization data to the web part object. In other words, the data is pushed from the web part editor to the content database on a per user basis.

Now comes the second aspect where we need to synch the web part data from the content database with the web part itself. For this, we need to override the syncchanges method and push the data from the web part object into the web part editor user interface.
Now if you edit your web part, you can see how the custom editor looks like.