Monday, September 26, 2011

Create a Custom Sharepoint List Defination

Microsoft Visual Studio 2010 provides a project type that makes it easier to create list definitions in XML and to create list instances that are based on those definitions. A field definition, defines the columns that appear inside of the list template. For more information about the XML that is used to define a field type, see Field Types XML. Let's follow the steps below to create custom list fields with different data types at a SharePoint custom list definition...
Open Visual Studio.Net 2010 -> File (Menu) -> New -> Project:
1


Select "2010" for SharePoint under "Installed Templates" from the left side panel -> Select "Empty SharePoint Project" from the right side panel. Type a name for your project, then click the OK button.
2
Type the URL of your local SharePoint site at the "What local site do you want to use for debugging?" textbox and choose "Deploy as a farm solution" (Read about the differences between Farm & Sandbox Solutions):
3
Click on the "View" menu -> Solution Explorer -> right-click at the created solution -> Add -> New Item:
4
Select "2010" for SharePoint under "Installed Templates" from the left side panel. -> Select "List Definition" from the right side panel:
5
Keep/Edit the display name of the list definition. Select "Custom List" as the type of the list definition and check "Add a list instance for this list definition":
6
Here is our solution with the created custom list definition and added list instance:
            7
Let's add some custom list fields with different data types. Open the "Schema.xml" file under created the "customListDefinition" directory. Select the "ContentTypes" tag section under "MetaData" and remove it. (We don't need for now in this tip to create the custom content type and associate it to our custom list definition.
7.1
Then add the fields below under the "Fields" tag section:
7.2
 "Type = Text" : Single line of text. (Plain Text).
"Type = Note" : Multiple line of text.
"Type = Image" : Publishing rollup image. (Image Picker).
"Type = URL" : Hyperlink (with URL and description fields).
"Type = Boolean" : Check Box.
"Hidden = TRUE" : In order to hide the default created column "Title" field for any custom list.
"ID" : Identifier value representing a GUID "Global Unique Identifier" which can be uniquely created as follows:
7.3 
 7.4
Then, let's show the above created fields at the listing view "AllItems". Search for the "View" tag where "DefaultView=TRUE" in the "Schema.xml" file:
7.5

In the "ViewFields" tag section, add the "FieldRef" fields below, then save all:
7.6
Right click on the "ListDefViaVS2010" project, then select "Deploy"  in order to deploy our solution to the SharePoint site. Then wait until we check that the output is successfully deployed:
8

9
Now let's go to the SharePoint site to check the deployed custom list definition. Click the "Site Actions" menu then "View All Site Content":
10
As we can see below a new added list instance to our custom list definition "ListDefViaVs2010 - ListInstance1" :
11
Click on "ListDefViaVS2010 - ListInstance1" to go to the listing view "AllItems" form:
12
We can see all defined "FieldRef" fields under "ViewFields" tag section at "Schema.xml" list definition file.
Then , Let's click on the "Add new item" link:
13
Now we can see all defined custom fields under the "Fields" tag section in the "Schema.xml" list definition file.

Reference link: http://msdn.microsoft.com/en-us/library/ee231593.aspx

Saturday, September 3, 2011

Custom properties in SharePoint Webparts

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;

}}}