"The day you stop learning SharePoint is the day you stop using it."

Return the Current Site in an Event Receiver; the site in which event occurred.

In an ItemUpdated Event Receiver we were accessing two custom lists for reading values and for that we required to return the current site in which the event occured.
Simply writing SPSite mySite = new SPSite("http://yoursitename"); does not make sense. Because you might don’t know where you need to deploy the Event Receiver, hence hard-coding the Site URL is a bad practice. The below code shows how to return current site URL.

namespace ItemUpdateFeatureReceiver
{
 public class Class1 : SPItemEventReceiver
 {
  private SPWeb _web;

  public SPWeb myWeb
  {
    get
    {
      return _web;
    }
    set
    {
      _web = value;
    }
  }

//Property that returns a custom list named “Document Type”
public SPList CustomList1
{
  get
  {
    SPList myList = myWeb.Lists["Document Type"];
    return myList;
  }
}

//Property that returns a custom list named “Document Sub-Type”
public SPList CustomList2
{
  get
  {
    SPList myList = myWeb.Lists["Document Sub-Type"];
    return myList;
  }
}


public override void ItemUpdated(SPItemEventProperties properties)
{
  //Returns the current site in which event occured
 
  myWeb = properties.OpenWeb();
  
  //your code
}
Share:

1 comments:

Karthika Shree said...

This blog is having the general information. Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog. This is very interesting and useful.
Sharepoint Training in Chennai

SharePoint-StackExchange Moderator

Author's Profile

My photo
India
A SharePoint Enthusiast working as a Lead Solution Architect for an IT Software & Consulting Company in Mumbai. I believe in giving back to the community through which I also learn and develop and eventually grow as an individual and professional. This blog is a small contribution to the community where I live in and may help someone who is seeking knowledge like me.

Popular Posts

Powered by Blogger.

Contact Form

Name

Email *

Message *

Total Pageviews