Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘list’

SharePoint 2010 – Create List Definition using Visual Studio

Posted by JP on June 4, 2012

In this article we can explore the List Definition creation using Visual Studio. Please note that the same can be achieved through SharePoint Designer as well.

Create List Definition Project

For starting use the List Definition project inside New Project dialog.

clip_image002

Enter the name as Coders. Select the site from the wizard as shown below.

clip_image004

Choose Custom List in the next step.

clip_image006

Enter the details as shown above and click the Finish button to close the wizard. Now the Solution Explorer looks like below:

clip_image008

Open the ListDefinition1>ListInstance1>Elements.xml (Please note that there are 2 Elements.xml inside the Solution Explorer)

Change the Title and Url attributes as following:

<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/"&gt;

<ListInstance Title="Coders"

OnQuickLaunch="TRUE"

TemplateType="10000"

Url="Lists/Coders"

Description="My List Instance">

</ListInstance>

</Elements>

Now click the Build button and on succeed, right click the Project and click Deploy. If no error messages are there, the Deployment succeeded and you can see the new List inside SharePoint.

clip_image010

On clicking the Coders list you can see the new list opened in SharePoint.

Now we need to add a custom column to the List Definition. Our column is of Single line and name as CoderName. Following are the activities included in the Column Creation.

· Create a new Field

· Specify the properties like ID (GUID), Name etc.

· Add the Field Reference inside Default View

Inside the Fields section of Schema.xml add the following Field definition

<Field Type="Note" Name="CoderName" DisplayName="CoderName" Required="FALSE" ID="{1D54AAC6-DBB8-4454-91B7-E2FAE7697D6A}"></Field>

Inside the ViewFields section of the second View, add the following FieldRef node.

<FieldRef Name="CoderName"></FieldRef>

You can go back to SharePoint and Refresh the Coders List. The changes are visible there:

clip_image012

The new CoderName property is visible now. This concludes our List creation using Visual Studio. For adding more columns, use the Schema.xml file.

References

http://msdn.microsoft.com/en-us/library/ms466023.aspx

http://onlinecoder.blogspot.com/2011/03/simple-way-of-creating-sharepoint-2010.html

Summary

In this article we have explored the List Definition creation using Visual Studio. The attached source code contains the example we discussed. You can try building and deploying it.

Advertisements

Posted in C#, SharePoint | Tagged: , , , | Leave a Comment »

SharePoint 2010 – List – Add, Edit, Delete using code

Posted by JP on June 4, 2012

In this article we can experiment List manipulations through code. Following are the list operations to perform:

1. Add

2. Edit

3. Delete

Pre-Requisite

For proceeding with we need to create a List named Tasks using template Tasks.

clip_image002

Now create a new SharePoint Console Application project inside Visual Studio.

clip_image004

Make sure you changed the Application Target Framework to .Net 3.5 version.

Adding an Item

For adding a new Task Item execute the following code:

using (SPSite site = new SPSite("http://appes-pc&quot;))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items.Add();

item["Title"] = "New Task";

item["Description"] = "Description of Task";

item.Update();

}

}

Now you can check the Tasks list inside SharePoint and you can see the new item there.

clip_image006

Editing an Item

For editing an existing Task use the following code. Here we are changing the first item Title and Description.

using (SPSite site = new SPSite("http://appes-pc&quot;))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items[0];

item["Title"] = "Edited Task";

item["Description"] = "Description of Task (edited)";

item.Update();

}

}

Going back to SharePoint you can see the Edited Task

clip_image008

Deleting an Item

For deleting an item use the following code.

using (SPSite site = new SPSite("http://appes-pc&quot;))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items[0];

item.Delete();

}

}

Now you can go back to SharePoint and see that the item was deleted.

clip_image010

References

http://suguk.org/blogs/tomp/archive/2006/09/28/Adding_SPListItems.aspx

Summary

In this article we have experimented List manipulations Add, Edit, Delete through code. In real life we will be needed to automate List manipulations and programming will be a help in this regard.

Posted in C#, SharePoint | Tagged: , , , | Leave a Comment »

SharePoint 2010 – List – Add, Edit, Delete using code

Posted by JP on June 2, 2012

In this article we can experiment List manipulations through code. Following are the list operations to perform:

1. Add

2. Edit

3. Delete

Pre-Requisite

For proceeding with we need to create a List named Tasks using template Tasks.

clip_image002

Now create a new SharePoint Console Application project inside Visual Studio.

clip_image004

Make sure you changed the Application Target Framework to .Net 3.5 version.

Adding an Item

For adding a new Task Item execute the following code:

using (SPSite site = new SPSite("http://appes-pc&quot;))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items.Add();

item["Title"] = "New Task";

item["Description"] = "Description of Task";

item.Update();

}

}

Now you can check the Tasks list inside SharePoint and you can see the new item there.

clip_image006

Editing an Item

For editing an existing Task use the following code. Here we are changing the first item Title and Description.

using (SPSite site = new SPSite("http://appes-pc&quot;))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items[0];

item["Title"] = "Edited Task";

item["Description"] = "Description of Task (edited)";

item.Update();

}

}

Going back to SharePoint you can see the Edited Task

clip_image008

Deleting an Item

For deleting an item use the following code.

using (SPSite site = new SPSite("http://appes-pc&quot;))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items[0];

item.Delete();

}

}

Now you can go back to SharePoint and see that the item was deleted.

clip_image010

References

http://suguk.org/blogs/tomp/archive/2006/09/28/Adding_SPListItems.aspx

Summary

In this article we have experimented List manipulations Add, Edit, Delete through code. In real life we will be needed to automate List manipulations and programming will be a help in this regard.

Posted in C#, SharePoint | Tagged: , , , | 2 Comments »

SharePoint 2010 – Lists and Event Handlers

Posted by JP on May 31, 2012

In this article we can explore the Event Handling features of List through code. Using the SPList in Server Object Model we can access event handling for a list.

We can use the event to perform the following activities:

· Validate the Item

· Log the information

· Create associated items

There are multiple Event Types for a List:

· List Events

· List Item Events (Add, Edit, Delete)

· Web Events

· Feature Events

· Workflow Events

Create Event Receiver Project

For starting with create an Event Receiver project inside Visual Studio.

clip_image002

You will be prompted for the site:

clip_image004

By default the machine site will be shown in the dialog box. Leave the default option of Sandboxed solution and click the Next button.

clip_image006

Select the List Item Events and use event source as Contacts. Check the check boxes for added, updated and deleted events and click the Finish button.

Note: The site url can be changed later. For time being we are using hard coded urls.

On clicking Finish button, the code will be generated for the List Event Receiver and you can place a break point in the ItemAdding event as shown below.

clip_image008

Now execute the application and your Event will be added and activated.

Try adding a new contact inside SharePoint:

clip_image010

On clicking the Save button, the breakpoint inside Visual Studio will get hit:.

clip_image012

SPItemEventProperties

You can cancel the operation by using the Cancel property.

You can report an error message using the ErrorMessage property.

Cancel for Delete

You can cancel and item delete by setting the properties.Cancel = true;

public override void ItemDeleting(SPItemEventProperties properties)

{

properties.ErrorMessage = "Deleting Item is not permitted!";

properties.Cancel = true;

}

Try deleting an item from the Contacts inside SharePoint:

clip_image014

You will get the following message prompted:

clip_image016

Deploying the Event Receiver

You can take a note that while you stop Visual Studio, the item deletion is allowed inside SharePoint. To make the event receiver permanent, use the project Deploy option.

clip_image018

Now try Deleting an item without Visual Studio Debugging. You will get the same Error Message Dialog and this concludes the Event Creation and Deployment to a SharePoint site.

Properties of Project

You can change the Url and other properties created through the Wizard. Use Project > Properties to access/modify these properties.

clip_image020

Features

The event receiver is actually deployed as a feature inside SharePoint. You can view the Feature properties and the associated xml file under Features special folder as shown below.

clip_image022

The Event code and Elements.xml reside inside the EvntReceiver1 group as shown below.

clip_image024

References

http://msdn.microsoft.com/en-us/library/bb736146(v=office.12).aspx

Summary

In this article we have explored the List Event facility of SharePoint. In advanced List Management these flexibilities are great tool for a SharePoint Developer.

Posted in C#, SharePoint | Tagged: , , , | 2 Comments »