web 2.0

List Event Example Code

Sharepoint lets us tap into quite a substantial number of List Events. Some of them are:

SPEventReceiverType.FieldAdded;

SPEventReceiverType.FieldAdding;

SPEventReceiverType.FieldDeleted;

SPEventReceiverType.FieldDeleting;

SPEventReceiverType.FieldUpdated;

SPEventReceiverType.FieldUpdating;

SPEventReceiverType.InvalidReceiver;

SPEventReceiverType.ItemAdded;

SPEventReceiverType.ItemAdding;

SPEventReceiverType.ItemAttachmentAdded;

SPEventReceiverType.ItemAttachmentAdding;

SPEventReceiverType.ItemAttachmentDeleted;

SPEventReceiverType.ItemAttachmentDeleting;

SPEventReceiverType.ItemCheckedIn;

SPEventReceiverType.ItemCheckedOut;

SPEventReceiverType.ItemCheckingIn;

SPEventReceiverType.ItemCheckingOut;

SPEventReceiverType.ItemDeleted;

SPEventReceiverType.ItemDeleting;

SPEventReceiverType.ItemFileConverted;

SPEventReceiverType.ItemFileMoved;

SPEventReceiverType.ItemFileMoving;

SPEventReceiverType.ItemUncheckedOut;

SPEventReceiverType.ItemUncheckingOut;

SPEventReceiverType.ItemUpdated;

SPEventReceiverType.ItemUpdating;

 

Listening to an event is very easy, simply create a new class file, and create a class that derieves from the 'SPItemEventReceiver' class as shown below:

namespace SaturnVibes.EventsExample

{

    public class ListEvents : SPItemEventReceiver

    {

        static ListEvents()

        {

            // Add Static constructor code here....

        }

 

        public override void ItemAdding(SPItemEventProperties properties)

        {

            // Add Code to execute when an Item is being added

        }

 

        public override void ItemAdded(SPItemEventProperties properties)

        {

            // Add Code to execute after an Item has being added

        }

 

        public override void ItemUpdating(SPItemEventProperties properties)

        {

            // Add Code to execute when an Item is being updated

        }

    }

In the above code sample, we have created a class in which the list's 'ItemAdding', 'ItemAdded' and the 'ItemUpdating' events were overriden. We can now execute our code in these list events.

The SPItemEventProperties object, will give us the properties that the listitem will have, both before and after the the event. An important thing to note here from the above sample is that, in an 'ItemAdding' event, since the List Item has not yet been created, 'properties.ListItem' will be null. To retrieve the values that a user may have entered while creating a list item, use 'properties.AfterProperties["Internal Name of the List Column"]'. Remember to provide the internal name of the List column inside the '[]'. (See how to get the Internal name of a List Column)

If the List event needs to be cancelled, we can set the 'properties.Cancel' to true and provide an error message in the 'properties.ErrorMessage' property.

Eg:

properties.ErrorMessage = "Your error message";

properties.Cancel = true;

 

Before the above code can be used however, the List event has to be registered to look for our code sample whenever an event is fired. This is done by:

SPWeb spWeb = new SPSite("URL of Site").OpenWeb();

list = spWeb.Lists["List Name"];

list.EventReceivers.Add(eventType, assemblyName, className);

where,

eventType is an enum of 'SPEventReceiverType'. Eg: 'SPEventReceiverType.ItemAdding'

assemblyName is the entire name of the assembly

className is the name of the class containing the event reciever. Eg: SaturnVibes.EventsExample.ListEvents

 

Happy Sharepointing :)

Retrieve a List's LookupValue ListItem

When using a lookup value in a list column, we sometimes need the entire item that is being looked up, in addition to it's value. Here's how to do it:

SPSite spsite = new SPSite("http://SiteAddress")
SPWeb spWeb = spsite.OpenWeb();

// Remember to get the ListItem through your preffered method :)

// Use the  'SPFieldLookupValue' class to create an object......

SPFieldLookupValue lookupValue = new SPFieldLookupValue(listItem["Insert Column Name here"].ToString());

// Get the LookUp's List item's ID (which is of an integer datatype)

int itemID = lookupValue.LookupId;

// Get the LookupValue's List Item....
SPListItem lookupValueListItem = spWeb.Lists["ListName"].GetItemById(itemID);

// Get the value in the lookup.

// This might be of interest,

// because, using 'listItem["Insert Column Name here"].ToString()' gives us

// some unnecessary values like '#'.

string value = lookupValue.LookupValue;

 

So, now we have the LookupValue's list item in 'lookupValueListItem' and the lookup's value in 'value'.

 

Happy Sharepointing :)

Create a Sharepoint list through code

Here's an article to create Sharepoint Lists through code:

http://www.sharepointblogs.com/michael/archive/2009/05/15/how-to-create-a-sharepoint-list-programmatically.aspx

List's column internal name - Update

An easier way to get the internal name of a List Item field (than the one mentioned here) is by using the 'Internal Name' property of an item field.

For example:

SPList spList = spWeb.Lists["List Name"];

string fieldInternalName = spList.Fields["Field Name"].InternalName

This will give you the internal name of the column in the list. The internal name is used in CAML queries while querying on a column in the list.

This means you don't have to bother converting your List column name special characters to get the internal name, since you get the internal name that has already converted the special characters for you.

Tags: ,

CAML | Lists

List's column internal name

Update: An easier method to extract a List Column's internal name is mentioned here. Click to open

Sharepoint list columns have an internal as well as external name to them. The external name is visible to the user, whereas, the internal name is used by sharepoint and never changes once it has been created, no matter how many times the external name is changed.

Some special characters used as a column name is converted to a special code which involves prefixing the values '_x' and '_' to the front and end of the special character's UTF-16 value.

Tool to view the special character's UTF-16 value

Function to convert these special characters...... (Thanks to Abstract Spaces for list of column name special characters)

 

        public static string ConvertSpecialColumnNameCharacters(string stringToConvert)

        {

            stringToConvert = stringToConvert.Replace("~","_x007e_");

 

            stringToConvert = stringToConvert.Replace("!","_x0021_");

 

            stringToConvert = stringToConvert.Replace("@","_x0040_");

 

            stringToConvert = stringToConvert.Replace("#","_x0023_");

 

            stringToConvert = stringToConvert.Replace("$","_x0024_");

 

            stringToConvert = stringToConvert.Replace("%","_x0025_");

 

            stringToConvert = stringToConvert.Replace("^","_x005e_");

 

            stringToConvert = stringToConvert.Replace("&","_x0026_");

 

            stringToConvert = stringToConvert.Replace("*","_x002a_");

 

            stringToConvert = stringToConvert.Replace("(","_x0028_");

 

            stringToConvert = stringToConvert.Replace(")","_x0029_");

 

            stringToConvert = stringToConvert.Replace("_","_x005F_");

 

            stringToConvert = stringToConvert.Replace("+","_x002b_");

 

            stringToConvert = stringToConvert.Replace("-","_x002d_");

 

            stringToConvert = stringToConvert.Replace("=","_x003d_");

 

            stringToConvert = stringToConvert.Replace("{","_x007b_");

 

            stringToConvert = stringToConvert.Replace("}","_x007d_");

 

            stringToConvert = stringToConvert.Replace(":","_x003a_");

 

            stringToConvert = stringToConvert.Replace("“","_x0022_");

 

            stringToConvert = stringToConvert.Replace("|","_x007c_");

 

            stringToConvert = stringToConvert.Replace(";","_x003b_");

 

            stringToConvert = stringToConvert.Replace("‘","_x0027_");

 

            stringToConvert = stringToConvert.Replace("\\","_x005c_");

 

            stringToConvert = stringToConvert.Replace("<","_x003c_");

 

            stringToConvert = stringToConvert.Replace(">","_x003e_");

 

            stringToConvert = stringToConvert.Replace("?","_x003f_");

 

            stringToConvert = stringToConvert.Replace(",","_x002c_");

 

            stringToConvert = stringToConvert.Replace(".","_x002e_");

 

            stringToConvert = stringToConvert.Replace("/","_x002f_");

 

            stringToConvert = stringToConvert.Replace("`","_x0060_");

 

            stringToConvert = stringToConvert.Replace(" ","_x0020_");

 

            return stringToConvert;

        }

Tags:

Lists

CAML query for retrieving ListItems where fieldType is 'User'

Had a hard time searching for a CAML query to retrieve ListItems where Column Type was 'Person or Group'. The below piece of code will retrieve ListItems containing the user based on the user's Display name. I'm still looking for a way to search based on the user's Logon Name 

string camlQuery = "<Where><Eq><FieldRef Name='User_x0020_Name'/><Value Type='User'>" + userDisplayName + "</Value></Eq></Where>"

SPList spList = spWeb.Lists["List Name"];

SPQuery spQuery = new SPQuery(spList.Views["All Items"]); 

spQuery.Query = camlQuery;

SPListItemCollection listItemCollection = spList.GetItems(spQuery);

  

Update: Lists can be searched on the Logon Name if the Type of Display for the 'Person or Group' Column type is changed to 'Account' (which basically displays the Logon Name in the 'DOMAIN NAME\Logon Name' format)

Tags: ,

CAML | Lists

Sharepoint Lists - Hide list column in the New Item (NewForm.aspx) page

To hide a column in the 'NewForm.aspx' from a user when he/she creates a new list item, Go to 'Settings' -> 'List Settings', under the 'General Settings' section, click on 'Advanced Settings'. Set the value of 'Allow Management of content Types' to 'Yes'. Click on OK and return to the 'Settings' page. There will be a new section - the 'Content Types' section. In the 'Content Type' column below, click on 'Item', click on the column that you wish to hide, in the 'Column Settings' section, choose the 'Hidden (Will not appear in forms)' option and click on the OK button.

This will prevent the user from seeing the column in the new item page.