Wednesday, October 27, 2010

Send Mails through Sharepoint 2007.

How to Send Mail from Sharepoint 2007

Mails can be send through a Sharepoint site as mentioned below.

string strTo = “To Address”;

string strFrom = “From Address”;

string strSubject = “Subject”;

StringDictionary headers = new StringDictionary();

headers.Add("to", strTo);

headers.Add("from", strFrom);

headers.Add("subject", strSubject);

headers.Add("content-type", "text/html"); //This is the default type, so isn't neccessary.

SPUtility.SendEmail(SPContext.Current.Web, headers, “Body of the Message”);

Before doing that, we need to set the Outgoing Email Settings in the Central Administration.


We need to give the Credentials in

Attach Event Receivers in Sharepoint 2007

How to Attach an Event Receiver with a List Programmatically

An event receiver can be attached with the list by 2 Ways.

1. By XML

2. By Code

1. By XML:

Any event receiver needs a Feature to Attach that Event Receiver with the List Template.

So the Feature.xml file will be like follows.

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

<Feature Id="GUID"

Title="Sample Title"

Description="Sample Description"

Version="12.0.0.0"

Hidden="FALSE"

Scope="Scope of the Feature"

ImageUrl="Optional URL"

xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>

<ElementManifest Location="Element.xml">ElementManifest>

ElementManifests>

Feature>

The last tag is the ElementManifests Tag. In the ElementManifests Tag, we will mention the Element file. In the ElementFile only we will Attach the EventReceiver with the List by using the ListTemplateID.

The Element.xml file will be as follows.

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

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

<Receivers ListTemplateId="101 – ListTemplate ID of the CustomList">

<Receiver>

<Class>Receiver Class Class>

<Assembly>Receiver AssemblyAssembly>

<Name>Name of the Event ReceiverName>

<Type>ItemAddingType>

<SequenceNumber>Any Unique Sequence NumberSequenceNumber>

Receiver>

Receivers>

Elements>

Once the Feature is Activated, then the EventReceiver “ItemAdding” is added with the Custom List. The Receiver Class will be executed when the Item is added in the List.

The EventReceiver Class needs to be inherited from the SPItemEventReceiver Class. Once this is Class is inherited, then the methods can be Overridden.

public override void ItemAdding(SPItemEventProperties properties)

{

}

2. Through Code:

The above mentioned method can be used for SPItemEventReceiver. In the Same Manner, SpWebEventReceiver Class can also be inherited and the Events Related to the Web can be triggered.

public class SearchCenterWebDeletingEventReceiver : SPWebEventReceiver

{

public override void WebDeleting(SPWebEventProperties properties)

{

try

{

}

catch (Exception ex)

{

}

}

}

This EventReceiver class needs to be Attached with the Web while creating the web itself. To accompalish that,

private void AttachWebDeletingEventReceiver(SPWeb web)

{

try

{

SPEventReceiverDefinitionCollection EventReceivers = web.EventReceivers;

SPEventReceiverDefinition EventReceiverDefinition = EventReceivers.Add();

EventReceiverDefinition.Name = EventReceiverName;

EventReceiverDefinition.Assembly = Assembly Name;

EventReceiverDefinition.Class = Class Name;

EventReceiverDefinition.Type = SPEventReceiverType.WebDeleting;

EventReceiverDefinition.SequenceNumber = Any Sequence Number;

EventReceiverDefinition.Update();

}

}

catch (Exception ex)

{

}

}

Provision a Site through Programmatically C#

How to Create a Sharepoint Site Programmatically using C#:

(or) Provisioning a Site using C#:

We can add a Site in a Site Collection by using the Method Add in the Class AllWebs.

For Example, If I want to add the Search Center with a site collection,

using (SPWeb newSite = currentWeb.Site.AllWebs.Add("Search", DisplayName,"Renault Search Center", currentWeb.Language, "SRCHCEN#0", false, false))

{

newSite.Navigation.UseShared = true;

PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(newSite);

pWeb.InheritCurrentNavigation = true;

pWeb.NavigationShowSiblings = false;

pWeb.IncludePagesInNavigation = false;

pWeb.IncludeSubSitesInNavigation = false;

pWeb.Update();

newSite.Update();

}

Activate and DeActivare Feature by Programmatically in Sharepoint 2007

How to Activate and DeActivate Features by programmatically (C#) in Sharepoint 2007?

Activate Feature:

A feature can be activated through Programmatically as follows.

private void ActivateFeature(Guid guid)

{

try

{

SPFeature spFeature = site.Features[guid];

if (spFeature == null)

{

site.Features.Add(guid);

}

}

catch (Exception ex)

{

MessageBox.Show("Activate : " + ex.Message);

}

}

DeActivate Feature:

A feature can be DeActivated programmatically as follows.

private void DeActivateFeature(Guid guid)

{

try

{

SPFeature spFeature = site.Features[guid];

if (spFeature != null)

{

site.Features.Remove(guid);

}

}

catch (Exception ex)

{

MessageBox.Show("DeActivate : " + ex.Message);

}

}