Sunday, 8 February 2015

How to create custom webpart properties in SharePoint 2013

(With Static Data and Dynamic Data Source)
Ø  To create a custom webpart properties in SharePoint we should follow one stranded approach.
Ø  Basically we can create following custom properties with only  inheriting  of webpart class
1.       check box
2.       dropdown
3.       textbox
Ø  To create any above controls we should write the following property  attributes of visual webparts
   [Category("Extended Settings"), //we can provide any name as category
   Personalizable(PersonalizationScope.Shared), 
   WebBrowsable(true),
   WebDisplayName("Sample Drop Down"), //here we have to provide control display name(opnl)
   WebDescription("Please Choose a Sample DropDown")] //optional

·         Category – This will group your property according to category. If not declared, “Miscellaneous” will be used as the default.
·         Personalizable – How the WebPart is configured, which can be per-user (PersonalizationScope.User), or for everyone (PersonalizationScope.Shared). For this example, we have chosen all users.
·         WebBrowsable – This will hide or show the property on the tool pane.
·         WebDisplayName – Label for the property.
·         WebDescription – Description for the property.

Ø  In webPart properties by declaring of property, the controls will be initialized.
Ø  the deferent types of property types and equivalent control initializations on pane as follows.




The above property types are provide limited controls but if you want to create more or any user control and provide initialization values dynamically, we have to inherit and implement few abstract classes and interfaces. we can discuss about them after these controls creation.



  BOOL (Check Box): 
        public Boolean _property; // this line creates check box
        [System.Web.UI.WebControls.WebParts.
WebBrowsable(true),
         System.Web.UI.WebControls.WebParts.
WebDisplayName("Check box property"),
         System.Web.UI.WebControls.WebParts.
WebDescription(""),
         System.Web.UI.WebControls.WebParts.
Personalizable(
         System.Web.UI.WebControls.WebParts.
PersonalizationScope.Shared),
         System.ComponentModel.
Category("Gowtham Custom Properties"),
         System.ComponentModel.
DefaultValue("")]
        public Boolean _Property   // the check box value available here.
        {
           
 get { return _property; }
           
 set { _property = value; }
        }
v  Enum (Dropdown):

        public enum Dropdownvalues { List1, List2, List3 };
       
 protected Dropdownvalues  selected_value  ;
          [System.Web.UI.WebControls.WebParts.
WebBrowsable(true),
         System.Web.UI.WebControls.WebParts.
WebDisplayName("Select the List"),
         System.Web.UI.WebControls.WebParts.
WebDescription(""),
         System.Web.UI.WebControls.WebParts.
Personalizable(
         System.Web.UI.WebControls.WebParts.
PersonalizationScope.Shared),
         System.ComponentModel.
Category("Gowtham Custom Properties"),
         System.ComponentModel.
DefaultValue("")]
        public Dropdownvalues  selected_value  // selected value available here
        {
           
 get { return _list; }
           
 set { _list = value; }
        }
v  TextBox (String, int,Date):

          public static string/int/Date  _value;
        [System.Web.UI.WebControls.WebParts.
WebBrowsable(true),
         System.Web.UI.WebControls.WebParts.
WebDisplayName("Enter the Value"),
         System.Web.UI.WebControls.WebParts.
WebDescription(""),
         System.Web.UI.WebControls.WebParts.
Personalizable(
         System.Web.UI.WebControls.WebParts.
PersonalizationScope.Shared),
         System.ComponentModel.
Category("Gowtham Custom Properties"),
         System.ComponentModel.
DefaultValue("")]
        public string/int/Date _Value
        {
           
 get { return _value; }
           
 set { _value = value; }
        }
Ø  In the Above scenario we can provide values statically especially for dropdown control.
Ø  and we can't generate   events for respected controls and those are limited controls to overcome these limitations we have to inherit few abstract classes and we have to override few methods from those abstract classes.

 
Abstract classes to customize webpart properties are :
             
o   Editor Part                            one approach
o   IWebEditable
o   Microsoft.SharePoint.WebPartPages.ToolPart    
                                                                                        other approach


With EditorPart Class and IWebeditable Interface :
Ø  first we have to create one separate .cs file for that webpart to create control in editor webpart pane.
Ø  that class must inherited from Editorpart Class.
Ø  From that class we should override following methods:
ü  CreateChildControls()
ü  SyncChanges()
ü  ApplyChanges()
ü  we have to override other methods from different classes  as per requirement.

*      The controls are added in the CreateChildControls method. This is where you should place most of your control logic, in editor parts, web parts etc.
*      The SynChanges method is used by the EditorPart to get the values from the Web Part into the Editor Part.
*      The ApplyChanges method is executed when you click Ok or Apply and is used to set the property values of your Web Part. SyncChanges is always called directly after the ApplyChanges method, to make sure that the properties are in sync.


Ø  From IWebeditable interface we have to override CreateEditorparts()

Ø  The CreateEditorParts method is used to tell the web part zone what editor parts you will show, in this case we only have one. But more advanced web parts may require that you have several. (this method must used in webpart.ascx.cs file)
Step by Step Procedure to Create Custom Webpart properties Webpart :
1.       Create New Visual WebPart of sharepoint2013
2.       add WPEditor.cs file to the webpart(another .cs file )
3.       write code in WPEditor with inheriting the class Editorpart.
4.       Override Onint() to initialize user controls with static/dynamic values from sources
   

  protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
       DropDownList     ddlTop = new DropDownList();

            SPWeb oWeb = SPContext.Current.Web;
            SPList list = oWeb.Lists["QPDES Top Navigation"];
            SPQuery Topquery = new SPQuery();
            Topquery.Query = "<Where><IsNull><FieldRef Name='parentLink' /></IsNull></Where>";
            SPListItemCollection Topcol = list.GetItems(Topquery);
            foreach (SPListItem item in Topcol)
            {
                ddlTop.Items.Add(item.Title);
            }
}
v  In above code we get the values from SPList to the dropdown
this control is added in criateChildControl(). in this we can provide any data, here we take dropdown but you can take any control.
5.       Then override the CriateChildcontrols()                                                                                 protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.Controls.add(UserControlObj);    
        }
6.       Then override ApplyChanges()
 public override bool ApplyChanges()
        {
   QPDES_ContentDisplayWP webPart = this.WebPartToEdit as QPDES_ContentDisplayWP;
            if (webPart != null)    //Webpart class object to assign values to proporties of                                                                    webpart main class
            {
     webPart.Top =User control  object.value attribute //top is property in main webpart class
            }
            return true;
        }
7.       Then Override Sycchanges()
    public override void SyncChanges() 
        {
      QPDES_ContentDisplayWP webPart = this.WebPartToEdit as QPDES_ContentDisplayWP;
            if (webPart != null)
            {
       UserControlOBJ.Value Atribute = webPart.Top;  // top is the property from main                                                                                                                                      webpart class
            }}
8.       Then implement the IWebEditable to the .ascx.cs class of the webpart
9.       Create Properties to get the values from user controls
            public string Top { get; set; }
10.   Then override the CreateEditorParts()
  public override EditorPartCollection CreateEditorParts()
        {
            List<EditorPart> editorParts = new List<EditorPart>();
            WPEditor oWPEditor = new WPEditor();   //object of controls created class
            oWPEditor.ID = this.ID + "_sampleEditorPart";
            oWPEditor.Title = "Personalize";
            editorParts.Add(oWPEditor);
            return new EditorPartCollection(base.CreateEditorParts(), editorParts);
        }     

11.   Then override the Webbrowsable() from IWebeditable
object IWebEditable.WebBrowsableObject
        {
            get { throw new NotImplementedException(); }
        }


v  Now you can assign that property value (public string Top { get; set; }) ,where you want and  this is selected value of dropdown in webpart properties.








50 comments:

  1. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    iot-training-in-chennai

    ReplyDelete
  2. I have visited this blog first time and i got a lot of informative data from here which is quiet helpful for me indeed. 
    python training in velachery
    python training institute in chennai

    ReplyDelete
  3. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  4. You blog post is just completely quality and informative. Many new facts and information which I have not heard about before. Keep sharing more blog posts.

    angularjs Training in chennai
    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  5. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    AWS Training in Bangalore |Best AWS Training Institute in Bangalore BTM, Marathahalli

    AWS Training in Chennai | AWS Training Institute in Chennai Velachery, Tambaram, OMR

    ReplyDelete
  6. I am happy to find this post Very useful for me, as it contains lot of information

    bloggydirectory
    Guest posting sites

    ReplyDelete
  7. Brilliant ideas that you have share with us.It is really help me lot and i hope it will help others also.update more different ideas with us.
    Salesforce Training in Perambur
    Salesforce Training in Mogappair
    Salesforce Training in Ashok Nagar
    Salesforce Training in Nungambakkam

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. I think this is one of the most significant information for me. And i’m glad reading your article. Thanks for sharing!

    Learn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Softgen Infotech provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
    Big Data Analytics Training in Bangalore
    Tableau Training in Bangalore
    Data Science Training in Bangalore
    Workday Training in Bangalore

    ReplyDelete
  10. Thank you for sharing such a nice and interesting blog with us regarding Java. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  11. Thanks for a marvelous posting! I seriously enjoyed reading it, you are
    a great author.I will be sure to bookmark your blog and will often come back in the future.
    I want to encourage yourself to continue your great job, have a
    nice weekend!

    sap training in chennai

    sap training in velachery

    azure training in chennai

    azure training in velachery

    cyber security course in chennai

    cyber security course in velachery

    ethical hacking course in chennai

    ethical hacking course in velachery

    ReplyDelete
  12. When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.
    data science training in chennai

    data science training in tambaram

    android training in chennai

    android training in tambaram

    devops training in chennai

    devops training in tambaram

    artificial intelligence training in chennai

    artificial intelligence training in tambaram

    ReplyDelete
  13. You completed certain reliable points there. I did a search on the subject and found nearly all people will agree with your blog.
    data science course in hyderabad

    ReplyDelete