Sharepoint Events

ItemAdding – Syncronous – can be cancelled
ItemAdded – Asynchronous – user may see the data even before even processing is done.

Create a new Survey called “Is Sahil la good boy?”. Choice, required response, drop down menu.

Create a new VS 2010 project (Empty Sharepoint Project) call EventReceivers, sandbox solution. Add a new Event Receiver item, rename to Surveyrigger, then:

Open Surveyrigger.cs, and add the folliwng code under: base.ItemAdded(properties);

SPListItem item = properties.ListItem;
string fieldName = “Is_x0020_Sahil_x0020_a_x0020_goo”;
if (item[fieldName].ToString() == “No”)
{
item[fieldName] = “Yes”;
item.Update();
}

What it does – after each item (Survey) is added, it looks for the fieldname of “Is…..”, and find the value (answer). If answer is No, then change it to Yes, and save it again.

(But why Is_X0020_Sahil_x0020……
?)

Change the scope of the feature to “Site”.

Open element.xml under Surveyrigger – it has:

<Receivers
ListTemplateId=102>

<Receiver>

<Name>SurveyriggerItemAdded</Name>

<Type>ItemAdded</Type>

<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

<Class>EventReceivers.Surveyrigger.Surveyrigger</Class>

<SequenceNumber>10000</SequenceNumber>

</Receiver>

</Receivers>

in the Receivers element, you can specify these arrtibutes:

Scope – Web or Site
RootWebonly – True/False
ListURL – Allow to attach this receiver to a specific list.

Because we only want to use this listener in the “Sahil Survey”, so we delete ListTemplate=”102″, and add the ListURL:

Build and deploy.

Since the ItemAdded event is asynchronous, so there is a chance that the user will still see a No in the screen. (although asynchronourly it has been changed to ‘Yes’), so by adding this line (hightlight in yellow) to elements.xml of the EventReceiver, it will be executed synchronouly:

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

<Elements
xmlns=http://schemas.microsoft.com/sharepoint/>

<Receivers
ListUrl=/Lists/Sahil Feedback>

<Receiver>

<Name>SurveyriggerItemAdded</Name>

<Type>ItemAdded</Type>

<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

<Class>EventReceivers.Surveyrigger.Surveyrigger</Class>

<SequenceNumber>10000</SequenceNumber>


<Synchronization>Synchronous</Synchronization>

</Receiver>

</Receivers>

</Elements>

 

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: