The Chapter 7 Challenge

Still musing around and working trough the previously already mentioned book (Sahil Malik; Building Solutions for SharePoint 2010). This time I have been facing another challenge formulated in Chapter 7. The author wants you to extend and alter the EventReceiver solution in order to display to the utterly mislead end user Smile who is answering “No”, some nice info message like “You nitwit, the right answer is YES! ”. Unless you would like to solve this challenge at your own, here are the steps extending the original solution (code download is at the end of this article).

First of all let’s start with the necessary aspx SitePage in order to display the end user the clear message. The extra challenge here is not breaking the originally started sandboxed solution. This means we cannot simply add a new “Application Page” item to the project as this would copy the aspx page to the file-system and such behavior is not allowed only for farm solutions.

Hence for let’s add rather a new module to the project and name it like ModuleSitePageErrorMsg meaning, this module is intended to hold a SitePage for displaying an error (or informational) message. Rename the by default created sample.txt below the new module to something like SitePageErrorMsg.aspx. The content goes here (Code 1). Note the ContentPlaceHolderID’s value is not arbitrary.

<%@ Page Language="C#" MasterPageFile="~masterurl/default.master" %>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<h1>You nitwit!</h1>
<p>Please note the right answer is YES!</p> 
</asp:Content>

Code 1

Furthermore the original EventReceiver class has to be extended with one more override handling the synchronous ItemAdding event (Code 2). It is important to note the RedirectUrl property’s value, which is the path addressing our aspx Page. The string is composed of my site collection’s base address which is “/sites/basic/” followed by the module’s folder name followed by the page’s exact name. The difference here to the original solution from the book is, that my site collection I’m experimenting with is not the default one but rather an additionally created. The structure of my development machine is depicted in Picture 1 (created using Using SharePoint Manager 2010). So feel free to adjust according to your real environment.

public override void ItemAdding(SPItemEventProperties properties)
{
   base.ItemAdding(properties);
   string answer =
      properties.AfterProperties[“Is_x0020_Sahil_x0020_a_x0020_goo”]
     .ToString();

   if (answer == “No”)
   {
      properties.Cancel = true;
      properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
      properties.RedirectUrl = “/sites/basic/ModuleSitePageErrorMsg/SitePageErrorMsg.aspx”;
   } 
}

Code 2

image

Picture 1

Next step is to edit the EventReceiver’s Element.xml and add this new ItemAdding handler Type to the collection of the active Receivers (Code 3). This will allow us to handle both the old ItemAdded event and the freshly created ItemAdding event.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListUrl="/sites/basic/Lists/Sahil%20Feedback/">
      <Receiver>
        <Name>SahilSurveyEventReceiverItemAdding</Name>
        <Type>ItemAdding</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>EventReceivers.SahilSurveyEventReceiver.SahilSurveyEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>SahilSurveyEventReceiverItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>EventReceivers.SahilSurveyEventReceiver.SahilSurveyEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
  </Receivers>
</Elements>

Code 3

At this step the Visual Studio 2010 solution tree should look similarly like Picture 2 (except the feature event receiver). There is no more step needed to deploy and run the solution and subsequently getting the expected popup Dialog-Box when answering with “No” (Picture 3). Starting the solution in debugger mode you could make sure both event receivers are listening and triggering.  The ItemAdded will however this time active only while answering to the survey with “Yes”.

image

Picture 2

image

Picture 3

At the end let me note two optional tasks. Firstly I have added a feature cleanup within Feature1.EventReceiver  (Picture 2). This will remove the virtual folder along with the aspx Page after the Feature becomes deactivated (Code 4).

Secondly the aspx could be extended with C# scripts intended to run on the Server. This mode is however by default disabled. Here the extended version of the aspx Page which contains such simple script along with the hints in the comments concerning web.config (Code 5).

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
   SPWeb web = properties.Feature.Parent as SPWeb;
   if (web != null)
   {
      SPFolder folder = web.GetFolder(“/sites/basic/ModuleSitePageErrorMsg”);
      if (folder != null)
      folder.Delete();
   }
}

Code 4

<%@ Page Language="C#" MasterPageFile="~masterurl/default.master" %>

<!--  NOTE: In order to run the Script, please add into your web.config the <PageParserPath> element 
<SharePoint>
    <SafeMode MaxControls="200" CallStack="true" ...>
        <PageParserPaths>
	        <PageParserPath VirtualPath="/sites/basic/ModuleSitePageErrorMsg/*"
             CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />
        </PageParserPaths>
    </SafeMode>
    ...
</SharePoint>
-->
<script language="C#" runat="server">
    public void Page_Load(object sender, EventArgs e)
    {
      lblTime.Text = DateTime.Now.ToString();
    }
</script>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<h1>You nitwit!</h1>
<p>Please note the right answer is YES!</p>
<asp:Label ID="lblTime" runat="server"></asp:Label> 
</asp:Content>

Code 5

And finally here the complete solution to download:

http://cid-8d365142bc4869ab.office.live.com/self.aspx/.Documents/EventReceivers.zip

Advertisements
This entry was posted in SharePoint 2010. Bookmark the permalink.

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