-
Return to the Visual Studio solution and right-click it.
Select Add and then click New Project.
-
Add an Empty SharePoint Project, provide a name for the
project (such as ListAndEventReceiver), select Deploy As A
Farm Solution, and click Finish.
-
Add the service reference similar to the WCF Windows Azure
service by right-clicking the project and selecting Add Service
Reference. Add the Windows Azure service endpoint URI in the
Address field. Click Go, and then when the service is loaded,
provide a namespace for the service (for example, AzureMortgageService).
-
Right-click the new SharePoint project and select Add |
New Item.
-
Select List Definition and provide a name (such as Mortgages), as shown in the following
graphic.
-
Click Add. Rename the default
ListInstance1 to MortgagesList. In the new solution,
you’ll see two instances of Elements.xml as shown in the
following graphic. Navigate to the Elements.xml within
MortgagesList and double-click it to open
it.
-
Amend this first Elements.xml so it looks like the
following code listing:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListInstance Title="Mortgages"
OnQuickLaunch="TRUE"
TemplateType="10001"
Url="Lists/MortgagesList"
Description="List that stores information about mortgage customers and
rates.">
</ListInstance>
</Elements>
-
Amend the second Elements.xml file (under Mortgages) so it
looks like the code here:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field Type="Text" DisplayName="Offering" Required="FALSE"
NumLines="6" RichText="FALSE" Sortable="FALSE"
ID="{3A811B2A-EEF5-44A7-BB5F-0E925A53D6CE}"
StaticName="Offering" Name="Offering"
Group="Custom Columns" />
<Field Type="Text" DisplayName="Rate" Required="FALSE"
MaxLength="255" ID="{78089CAD-A90C-42F1-8240-E8EFD2CA7191}"
StaticName="Rate" Name="Rate"
Group="Custom Columns" />
<Field Type="Text" DisplayName="APR" Required="FALSE"
MaxLength="255" ID="{7D4CC8E4-C7A7-486E-8185-E07BA646CAE6}"
StaticName="APR" Name="APR"
Group="Custom Columns" />
<ContentType ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" Name="Mortgage Record"
Group="Mortgages" Description="Mortgage content type." Version="0">
<FieldRefs>
<FieldRef ID="{3A811B2A-EEF5-44A7-BB5F-0E925A53D6CE}" />
<FieldRef ID="{78089CAD-A90C-42F1-8240-E8EFD2CA7191}" />
<FieldRef ID="{7D4CC8E4-C7A7-486E-8185-E07BA646CAE6/>
</FieldRefs>
</ContentType>
<ListTemplate
Name="Mortgages"
Type="10001"
BaseType="0"
OnQuickLaunch="TRUE"
SecurityBits="11"
Sequence="410"
DisplayName="Mortgages"
DisallowContentTypes="FALSE"
Description=
"List that provides information on customers and current mortgage rates."
Image="/_layouts/images/itgen.png"/>
</Elements>
-
Open the Schema.xml file and amend the code in the file as
per the bolded code shown here:
<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" Title="Mortgages" EnableContentTypes="TRUE"
FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/Mortgages"
BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
<MetaData>
<ContentTypes>
<ContentTypeRef ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" />
<ContentTypeRef ID="0x01">
<Folder TargetName="Item" />
</ContentTypeRef>
<ContentTypeRef ID="0x0120" />
</ContentTypes>
<Fields>
<Field Type="Text" DisplayName="Offering" Required="FALSE"
NumLines="6" RichText="FALSE" Sortable="FALSE"
ID="{3A811B2A-EEF5-44A7-BB5F-0E925A53D6CE}"
StaticName="Offering" Name="Offering"
Group="Custom Columns" />
<Field Type="Text" DisplayName="Rate" Required="FALSE"
MaxLength="255" ID="{78089CAD-A90C-42F1-8240-E8EFD2CA7191}"
StaticName="Rate" Name="Rate"
Group="Custom Columns" />
<Field Type="Text" DisplayName="APR" Required="FALSE"
MaxLength="255" ID="{7D4CC8E4-C7A7-486E-8185-E07BA646CAE6}"
StaticName="APR" Name="APR"
Group="Custom Columns" />
</Fields>
<Views>
<View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="LinkTitleNoMenu"></FieldRef>
</ViewFields>
<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>
</Query>
<ParameterBindings>
<ParameterBinding Name="AddNewAnnouncement"
Location="Resource(wss,addnewitem)" />
<ParameterBinding Name="NoAnnouncements"
Location="Resource(wss,noXinviewofY_LIST)" />
<ParameterBinding Name="NoAnnouncementsHowTo"
Location="Resource(wss,noXinviewofY_ONET_HOME)" />
</ParameterBindings>
</View>
<View BaseViewID="1" Type="HTML" WebPartZoneID="Main"
DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;"
DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE"
SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png"
Url="AllItems.aspx">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="Attachments"></FieldRef>
<FieldRef Name="LinkTitle"></FieldRef>
<FieldRef Name="Offering"></FieldRef>
<FieldRef Name="Rate"></FieldRef>
<FieldRef Name="APR"></FieldRef>
</ViewFields>
<Query>
<OrderBy>
<FieldRef Name="ID"></FieldRef>
</OrderBy>
</Query>
<ParameterBindings>
<ParameterBinding Name="NoAnnouncements"
Location="Resource(wss,noXinviewofY_LIST)" />
<ParameterBinding Name="NoAnnouncementsHowTo"
Location="Resource(wss,noXinviewofY_DEFAULT)" />
</ParameterBindings>
</View>
</Views>
<Forms>
<Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx"
WebPartZoneID="Main" />
<Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx"
WebPartZoneID="Main" />
<Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx"
WebPartZoneID="Main" />
</Forms>
</MetaData>
</List>
-
Now that you’ve completed the list definition, right-click
the SharePoint project and select Add | New Item.
-
Select the Event Receiver, provide a name (such as MortgageRecordEvent), and click
Add.
-
When prompted, select List Item Events as the type of
event receiver and the MortgagesList list
as the event source. Select the An Item Is Being Added check box
as the event to trap. The following graphic illustrates these
options.
-
When the event receiver has been added to the project,
double-click the event receiver class file and amend the code,
as per the bolded text here:
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using ListAndEventReceiver.AzureMortgageService;
namespace ListAndEventReceiver.MortgageRecordEvent
{
public class MortgageRecordEvent : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
this.EventFiringEnabled = false;
LogMortgageRecord(properties);
this.EventFiringEnabled = true;
}
private void LogMortgageRecord(SPItemEventProperties properties)
{
AzureServiceClient myAzureSvc = new AzureServiceClient();
double[] returnProductInfoFromAzure = new double[6];
returnProductInfoFromAzure = myAzureSvc.getDailyInterestRate();
double returnProductCosts = returnProductInfoFromAzure[0];
double mortgageAPR = .18 * 100;
string mortgageOffering = "30 YR Fixed";
double dblDailyInterestRate = 0.13;
returnProductCosts = dblDailyInterestRate += .24 * 100;
string mySPSite = "http://blueyonderdemo";
using (SPSite site = new SPSite(mySPSite))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList mortgageList = web.GetList("Lists/MortgagesList");
SPQuery myCAMLQuery = new SPQuery();
myCAMLQuery.Query = "<Query>" +
"<Where><Eq>" +
"<FieldRef Name='Title'></FieldRef>" +
"<Value Type='Text'>Contoso
</Value>" +
"</Eq></Where></Query>";
SPListItemCollection collItem = mortgageList.GetItems(myCAMLQuery);
foreach (SPListItem oItem in collItem)
{
if (oItem["Title"].ToString() == "Contoso")
{
oItem["Offering"] = mortgageOffering;
oItem["Rate"] = returnProductCosts.ToString() + "%";
oItem["APR"] = mortgageAPR.ToString() + "%";
oItem.Update();
}
}
web.AllowUnsafeUpdates = false;
}
}
}
}
}
When it finds that list item, the code updates three fields by
first creating a collection of items
(collItem) and then iterating through each
item and filling in Offering, Rate and
APR:
...
SPQuery myCAMLQuery = new SPQuery();
myCAMLQuery.Query = "<Query>" +
"<Where><Eq>" +
"<FieldRef Name='Title'></FieldRef>" +
"<Value Type='Text'>Contoso</Value>" +
"</Eq></Where></Query>";
SPListItemCollection collItem = mortgageList.GetItems(myCAMLQuery);
...
The Update method then updates the
list. You might recognize that you are using the SharePoint
server object model to interact with the list and add the data
retrieved from the Windows Azure WCF service endpoint.
-
When done, press F6 to build the project to ensure that
there are no errors.
-
Next, right-click the project and select Deploy.
-
After the project has deployed successfully, you can
navigate to the new list and add a single record where the title
is Contoso. Adding the new list
item triggers a call to the WCF service and then updates the
three fields with values calculated from the data returned from
the service. The following graphic shows the result of this
service call.