Thursday, May 16, 2013

Creating a simple Event Receiver in SharePoint 2013

Create an empty SharePoint 2013 project in Visual Studio 2012. In the project, select add new item and select Event Receiver


Select the type of event receiver you need to add, and select the events you need to handle.



In this sample I'm trying to update a SharePoint list based on file changes happening to a separate SharePoint library. Basically, the list will act like a log. So we need to create the library and a list. Here, I have created the Department library to add and maintain documents and also created DocumentLog list to log the changes happening to the library. 

In the list I have three columns, Title, Action & DateAndTime in order to catalog the changes happening to the library.


[The created document library and list]

Back to the SharePoint project. Now go to the event receiver .cs file and you'll get a bunch of methods base on your selection during event receiver creation. Edit the code as below to implement the logic. Note that the ItemAdded method is used instead of the ItemAdding method.


public override void ItemAdded(SPItemEventProperties properties)
        {
            //base.ItemAdded(properties);
            using (SPWeb web = properties.OpenWeb())
            {
                try
                {
                    SPList list = web.Lists["DocumentLog"];
                    SPListItem newItem = list.Items.Add();
                    newItem["Title"] = properties.ListItem.Name;
                    newItem["DateAndTime"] = System.DateTime.Now;
                    newItem["Action"] = "Item Added";
                    newItem.Update();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

public override void ItemUpdating(SPItemEventProperties properties)
        {
            //base.ItemUpdating(properties);
            using (SPWeb web = properties.OpenWeb())
            {
                try
                {
                    SPList list = web.Lists["DocumentLog"];
                    SPListItem newItem = list.Items.Add();
                    newItem["Title"] = properties.ListItem.Name;
                    newItem["DateAndTime"] = System.DateTime.Now;
                    newItem["Action"] = "Item Updated";
                    newItem.Update();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

public override void ItemDeleting(SPItemEventProperties properties)
        {
            //base.ItemDeleting(properties);
            using (SPWeb web = properties.OpenWeb())
            {
                try
                {
                    SPList list = web.Lists["DocumentLog"];
                    SPListItem newItem = list.Items.Add();
                    newItem["Title"] = properties.ListItem.Name;
                    newItem["DateAndTime"] = System.DateTime.Now;
                    newItem["Action"] = "Item Deleted";
                    newItem.Update();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }


As I am targeting to add the event receiver only to the Department document library, the Elements.xml file requires a change.



 Note that I have commented out the setting which points to all document libraries, instead pointed to Department document library. The edited Elements.xml file is as below:


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!--   <Receivers ListTemplateId="101"> -->
  <Receivers ListUrl="Department">

   <Receiver>
        <Name>DepartmentEventReceiverItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>MySharePointProject.ListEventReceiver.DepartmentEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>

      <Receiver>
        <Name>ListEventReceiverItemUpdating</Name>
        <Type>ItemUpdating</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>MySharePointProject.ListEventReceiver.DepartmentEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ListEventReceiverItemDeleting</Name>
        <Type>ItemDeleting</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>MySharePointProject.ListEventReceiver.DepartmentEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
</Receivers>
</Elements>

Compile and deploy the solution to your site. Now you may play around with the library and observe the changes happening to the list.... :)

I have added a document, uploaded, added, modified the 3 docs and then deleted one of the docs respectively.


And here's what I get in the DocumentLog list.


Monday, May 6, 2013

Installing SharePoint 2007

Back in the day SharePoint 2007 was a hot topic for SharePoint newbies. Seems SP 2007 is still around in use and thought of blogging on key points to note on the installation and configuration.

Installation

Choose the installation type you want. Here I choose Advanced.



Choose the installation server type. I have selected "Complete"

Once the installation is done you will be prompted. Then MOSS will fire up the "SharePoint Products and Technologies Configuration Wizard"

1) System prompts the services that would be restarted before starting the wizard.

2) Server farm option: Creating New vs. Connecting to existing. I'm creating new


3) Specify database settings

4) Specify Central Admin Web App settings

5) Before the wizard starts, the user specified details are displayed

6) Then the wizard runs

7) Once the configuration wizard completes user is prompted of the end result.

8) Then, user is directed to the Central Admin. The "Services on Server" section indicates that services have to be configured



9) Note that you have to start the below services started in the image. Service settings are pretty straight forward.


Creating a new Shared Service Provider

1) Navigate to "Application Management" to create a new SSP. Select the link as shown below:

2) Select "New SSP"

3) In the "New Shared Service Provider" page, specify the web application for SSP and MySite

I have not created any web application yet. Hence I am creating two now. Select the links on the page in order to create new web applications. You will be directed to "Create New Web Application" page. Once it's done system directs you back to the Create SSP page.

3.1) Creating a web application

A web application can be easily created using the following steps. Specify the IIS website and port to be used for the new web application.


Provide the application pool name and the security account to be used.


Provide the database server name, database name & the authentication


Once the two applications are created the SSP can be created. 

4) On completing SSP creation you get a success message


Now in the Shared Services Administration page the SSP is displayed.