Writing custom properties for web parts in Sharepoint


For adding custom properties to our web part we need to do the following

1) Create property.

2) Decorate the property with the following attributes

WebBrowsable – To allow your property to be visible within SharePoint.

WebDisplayName– To provide display name to the property.

WebDescription– To provide description for that property.

Personalizable – To define the scope of it i.e either User or Shared through PersonalizationScope enumeration.

Let’s take a simple example wherein we have 2 properties defined, user will enter value for them and finally when the web part is rendered, we would be displaying their sum within the web part.

namespace AdditionWebPart

{

public class SumWebPart : WebPart{

private int firstVariable;

[WebBrowsable(true),

WebDisplayName(“First Value”),

WebDescription(“Enter value for first variable”),

Personalizable(PersonalizationScope.User)]

public int FirstVariable

{

get { return firstVariable; }

set { firstVariable = value; }

}

private int secondVariable;

[WebBrowsable(true),

WebDisplayName(“Second Value”),

WebDescription(“Enter value for second variable”),

Personalizable(PersonalizationScope.User)]

public int SecondVariable

{

get { return secondVariable; }

set { secondVariable = value; }

}

protected override void Render(System.Web.UI.HtmlTextWriter writer){

writer.Write(“The total is “ +this.calcTotal(this.firstVariable,this.secondVariable));

}

private int calcTotal(int a, int b){

return a + b;

}}}

Put the following attribute in your assemblyinfo.cs file

[assembly: AllowPartiallyTrustedCallers]


Strong sign the assembly and install it in GAC.

Open the web.config of your site where you want this webpart

Make a safecontrol entry within the web.config for your webpart.

<SafeControl Assembly=AdditionWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5f7492d3f59e0c4b Namespace=AdditionWebPart TypeName=* Safe=True />

The name of the assembly,it’s version, culture and public key token information can be found by right clicking the assembly within gac and selecting properties.

Bye….

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

5 thoughts on “Writing custom properties for web parts in Sharepoint”

  1. Nice, you just solve me half of the problem, thanks

    Now I would like this new custom property to be a dropdown menu (choice)

    How can this be achieved?

    Regards,

    Like

  2. Hi Mario,
    For showing the drop down you can define your property as enum.
    public enum myChoiceEnum
    {
    choice1,
    choice2,
    choice3,
    choice4
    };

    private myChoiceEnum _myEnum;

    [WebBrowsable(true),
    WebDisplayName(“My Choice”),
    WebDescription(“Select a value”),
    Personalizable(PersonalizationScope.User)]
    public myChoiceEnum MyEnum
    {
    get { return _myEnum; }
    set { _myEnum = value; }
    }

    Try it!!

    Like

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM